package bwk; public class Fulld { public static void main(String[] args) { // 编写一个类,类中定义一个静态方法,用于求两个整数的和。 System.out.println("俩个整数的和为"+getSum(9,23)); } public static int getSum(int a , int b) { return a+b; } }
package bwk; public class Fulld { public static void main(String[] args) { //使用java类描述一个车类,车都具备名字、颜色两个属性,还具备跑的功能。 请设计一个汽车类Car, Fulld c = new Fulld(); c.setName("法拉利"); c.getName(); c.setColor("黄色"); c.getColor(); c.running(); System.out.println(c); } String name; String color; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public void running() { System.out.println("The car is running!"); } public String toString() { return "Car [name=" + name + ", color=" + color + "]"; } }
package bwk; public class Fulld { public static void main(String[] args) { //设计学生类 Fulld s1 = new Fulld(); s1.setName("李四"); s1.getName(); s1.setScore(88); s1.getScore(); System.out.println(s1); Fulld s2 = new Fulld("张三", 86); System.out.println(s2); } String name; int score; public Fulld() { } public Fulld(String name, int score) { this.name = name; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public String toString() { return "Student [name=" + name + ", score=" + score + "]"; } }
package bwk; public class Fulld { public Fulld() { System.out.println("无参的构造方法被调用了..."); } }
package bwk; public class Ghkb { public static void main(String[] args) { // TODO Auto-generated method stub Fulld p = new Fulld(); } }
5.说一下什么是封装, 使用封装的好处。什么是get,set访问器
封装的好处:使代码具有独立的作用域,不被外界所访问,加强了使用代码的安全性
get访问器:将变量变成私有化进行获取该变量的值
set访问器:将变量变成私有化进行设置该变量的值