1 package 汽车; 2 class Car{ 3 String name; 4 int wheel; 5 String color; 6 //构造方法// 7 public Car(String name,int wheel,String color)//成员变量// 8 { 9 this.name=name; 10 this.wheel=wheel; 11 this.color=color; 12 } 13 //成员方法// 14 public void run() { 15 if(wheel>=4){ 16 System.out.println(name+wheel+"个轮子飞快跑起来.."); 17 }else{ 18 System.out.println(name+"不够4个轮子了,赶快去修理"); 19 } 20 } 21 } 22 class CarFactory{ 23 String name; 24 String address; 25 String tell; 26 public void repair(Car c) { 27 if(c.wheel>=4) { 28 System.out.println("车子没问题不要修嘞!"); 29 } 30 else 31 { 32 c.wheel = 4; 33 System.out.println("修好了,给钱!!"); 34 } 35 } 36 } 37 public class DemoX { 38 public static void main(String args[]) { 39 Car c = new Car("陆虎",4,"黑色"); 40 System.out.println(c.name+"是"+c.color+c.wheel+"轮胎"); 41 //给车对象赋予属性值 42 43 for(int i = 0 ; i<100 ; i++){ 44 c.run(); 45 } 46 c.wheel = 3; 47 c.run(); 48 CarFactory f = new CarFactory(); 49 f.name = "集群宝修车厂"; 50 f.address = "韵泰商业广场一楼"; 51 f.tell = "020-1234567"; 52 System.out.println(f.name+"在"+f.address+"!!!"+"联系电话是"+f.tell); 53 f.repair(c); 54 c.run(); 55 56 } 57 58 }
原文:https://www.cnblogs.com/0929-luoyang/p/11312723.html