关于面向对象封装函数后的 set 和 get 一个设置的和一个取值的是不是可以和在一起写
class Demo0707 {
public static void main(String[] args) {
for (int i = 0;i<10 ;i++ ) {
method(new Car());
}
}
public static void method(Car cc){
cc.setColor ( "red");
//cc.run();
cc.setNum(8);
System.out.println(cc.getNum());
System.out.println(cc.getColor());
}
}
class Car {
private String color = "blue";
private int num =10;
public void run(){
System.out.println(color+"..."+num);
}
public void setNum(int a){
if (a>0&&a<150) {
num = a;
}else{
System.out.println("火星人");
}
}
public int getNum(){
return num;
}
public void setColor(String b){
color = b;
}
public String getColor(){
return color;
}
}
|
|