1 package h_m5_14; 2 /* 3 * 汽车类 父类 4 */ 5 public class Vehicle { 6 private String brand; 7 private String color; 8 private double speed; 9 public Vehicle() { 10 // TODO Auto-generated constructor stub 11 } 12 public Vehicle( final String brand, String color) { 13 //品牌初始化不能修改 14 super(); 15 this.brand = brand; 16 this.color = color; 17 this.speed=0; 18 } 19 public String getBrand() { 20 return brand; 21 } 22 23 public String getColor() { 24 return color; 25 } 26 public void setColor(String color) { 27 this.color = color; 28 } 29 public double getSpeed() { 30 return speed; 31 } 32 public void setSpeed(double speed) { 33 this.speed = speed; 34 } 35 public void run(){ 36 System.out.println("runing!!!!!"); 37 } 38 @Override 39 public String toString() { 40 return "Vehicle [brand=" + brand + ", color=" + color + ", speed=" + speed + "]"; 41 } 42 43 } 44 package h_m5_14; 45 /* 46 * 继承Vehicle类 47 */ 48 public class Car extends Vehicle{ 49 private int loader; 50 public Car() { 51 // TODO Auto-generated constructor stub 52 } 53 54 public Car(String brand, String color,int loader) { 55 super(brand, color); 56 this.loader=loader; 57 // TODO Auto-generated constructor stub 58 } 59 60 @Override 61 public String toString() { 62 return "Car [loader=" + loader + ", Brand()=" + getBrand() + ", Color()=" + getColor() + ", Speed()=" 63 + getSpeed() +"]"; 64 } 65 66 67 68 } 69 package h_m5_14; 70 71 public class VehicleTest { 72 /* 73 * 测试类 74 */ 75 public static void main(String[] args) { 76 Vehicle v= new Vehicle("奥迪", "黑色"); 77 v.setSpeed(50.0); 78 System.out.println(v); 79 v.run(); 80 } 81 82 } 83 package h_m5_14; 84 85 public class test { 86 /* 87 * 测试Car类 88 */ 89 public static void main(String[] args) { 90 Car c = new Car("奔驰", "黑色", 2); 91 c.setSpeed(60); 92 System.out.println(c); 93 } 94 95 } 96 package h_m5_14; 97 /* 98 * 图形类 99 */ 100 public abstract class Shape { 101 private int area; 102 private int per; 103 private String color; 104 public Shape() { 105 // TODO Auto-generated constructor stub 106 } 107 public Shape(String color) { 108 super(); 109 this.color = color; 110 } 111 public String getColor() { 112 //求颜色方法 113 return color; 114 } 115 /* 116 * 三个抽象方法 117 */ 118 public abstract int getArea(); 119 public abstract int getPer(); 120 public abstract void getAll(); 121 } 122 package h_m5_14; 123 /* 124 * 矩形类 125 */ 126 public class Rectangle extends Shape{ 127 private int width; 128 private int height; 129 130 public Rectangle(String color,int width,int height) { 131 132 super(color); 133 this.width=width; 134 this.height=height; 135 // TODO Auto-generated constructor stub 136 } 137 138 @Override 139 public int getArea() { 140 // TODO Auto-generated method stub 141 return width*height; 142 } 143 144 @Override 145 public int getPer() { 146 // TODO Auto-generated method stub 147 return (width+height)*2; 148 } 149 150 @Override 151 public void getAll() { 152 // TODO Auto-generated method stub 153 System.out.println(getArea()+","+getPer()+","+getColor()); 154 } 155 156 157 } 158 package h_m5_14; 159 160 public class Circle extends Shape { 161 private int radius; 162 public Circle() { 163 // TODO Auto-generated constructor stub 164 } 165 166 public Circle(String color,int radius) { 167 super(color); 168 this.radius=radius; 169 // TODO Auto-generated constructor stub 170 } 171 172 public int getArea() { 173 // TODO Auto-generated method stub 174 return (int)Math.PI*radius*radius; 175 } 176 177 @Override 178 public int getPer() { 179 // TODO Auto-generated method stub 180 return (int)Math.PI*radius*2; 181 } 182 183 @Override 184 public void getAll() { 185 System.out.println(getArea()+","+getPer()+","+getColor()); 186 } 187 188 189 } 190 package h_m5_14; 191 192 public class ShapeTest { 193 194 public static void main(String[] args) { 195 Rectangle re = new Rectangle("黑色", 20, 30); 196 re.getAll(); 197 Circle c= new Circle("红色", 5); 198 c.getAll(); 199 } 200 201 }