A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

public class Demo9_Car {
//我想创建n多辆车,匿名对象是这样做的,那用循环该怎么做?
        public static void main(String[] args) {
       
                method(new Car());
                method(new Car());
                method(new Car());                //匿名对象可以当作实际参数传递
               
        }
        //抽取方法提高代码的复用性
        public static void method(Car c) {
                c.color = "黑色";
                c.num = 4;
                c.run();
        }
}

class Car {
        String color;                                //颜色
        int num;                                        //轮胎数
       
        public void run() {                        //车运行
                System.out.println(color + "..." + num);
        }
}

2 个回复

倒序浏览
直接加个For循环不就行了么

评分

参与人数 1黑马币 +10 收起 理由
cxl1694095035 + 10 赞一个!

查看全部评分

回复 使用道具 举报
求具体步骤!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马