package com.hanqi.test; public class Car { //构造一个汽车的属性 private String name; //创建getter和setter方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public void drive() { } }
package com.hanqi.test; public class Aodi extends Car { public double price;//定义私有属性价格和型号 public String style; //构造方法 Aodi(double price,String style) { this.price=price; this.style=style; } //成员方法 public void Speed(double aos ) { System.out.println("速度是:"+aos); } }
package com.hanqi.test; public class Benchi extends Car { public double price; public String style; //构造方法 Benchi(double price,String style) { this.price=price; this.style=style; } //成员方法 public void Speed(double bensp) { System.out.println("速度是:"+bensp); } }
package com.hanqi.test; public class Test1 { public static void main(String[] args) { Aodi ad=new Aodi(350000,"A4L"); ad.setName("奥迪"); System.out.println("Aodi的name: "+ad.getName()+",型号是:"+ad.style+",价格是:"+ad.price); ad.Speed(120); Benchi bc=new Benchi(400000,"s300L"); bc.setName("奔驰"); System.out.println("benchi的name:"+bc.getName()+",型号是;"+bc.style+",价格是:"+bc.price); bc.Speed(150); } }