继承
继承:
方便改写,减少代码重叠量。
继承的概念:
在两个类中抽取重复性代码,集合到一个公共类中此类为父类(super class),继承父类的类则为子类(sub class)。
继承特性:
单根性。在java中,一个类只能有一个直接父类。
传递性。C继承于B,B继承于A,C具有A的特性和行为。
类B继承于类A,使用关键字extends,B拥有了A中非私有的属性和方法。
如果要访问父类属性,通过super;如果要访问子类属性,通过this。
System.out.println("价格:"+super.price);
不能访问父类的私有属性
System.out.println("颜色:"+super.color);
访问和父类同名的变量weight
System.out.println("重量:"+weight);
System.out.println("重量:"+this.weight);
System.out.println("重量:"+super.weight);
public Dog(String name,int health,int love,String strain){
this.setName(name);
this.setHealth(health);
this.setLove(love);
super(name,health,love);
this.setStrain(strain);

浙公网安备 33010602011771号