父类
package Zuoye; public class Car { private String name; private String color; private int price; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public Car(String name,String color,int price) { System.out.println("车的构造方法"); this.name =name; this.color=color; this.price=price; } public void run() { System.out.println("我可以跑"); } public void xiaohao() { System.out.println("我能消耗油"); } }
子类
package Zuoye; public class Carddezilei extends Car{ public Carddezilei() { super("法拉利", "红色", 1000); System.out.println("车的子类的构造方法"); } public void run() { System.out.println("我可以跑的很快"); } public void zaizhong() { System.out.println("我不能载重"); } }
测试类
package Zuoye; public class Cardeceshi { public static void main(String[] args) { Car c1=new Car("奇瑞", "红色", 1); System.out.println("我是"+c1.getName()+",颜色是"+c1.getColor()+",售价是"+c1.getPrice()+"万元"); c1.run(); c1.xiaohao(); System.out.println(); Carddezilei c2=new Carddezilei(); System.out.println("我是"+c2.getName()+",颜色是"+c2.getColor()+",售价"+c2.getPrice()+"万元"); c2.run(); c2.zaizhong(); c2.xiaohao(); System.out.println(); Car c3=new Carddezilei(); System.out.println("我是"+c3.getName()+",颜色是"+c3.getColor()+",售价"+c3.getPrice()); c3.run(); c3.xiaohao(); } }