/*
匿名对象可以当作实际参数传递
*/
class Demo5_Test_Class {
public static void main(String[] args) {
//System.out.println("Hello World!");
//Car c1 = new Car();
/*
c1.color = "red";
c1.num = 8;
c1.run();
*/
//method(c1); //有名对象
method(new Car()); //匿名对象
//Car c2 = new Car();
/*
c2.color = "red";
c2.num = 8;
c2.run();
*/
//method(c2);
method(new Car()); //匿名对象可以当作实际参数传递
}
//抽取方法,提高代码的复用性
public static void method(Car cc) {
cc.color = "red";
cc.num = 8;
cc.run();
}
}
class Car{
String color; //颜色
int num; //轮胎数
public void run() {
System.out.println(color + "车运行" + num);
}
}
|
|