继承

继承:
方便改写,减少代码重叠量。

 

 

继承的概念:

在两个类中抽取重复性代码,集合到一个公共类中此类为父类(super class),继承父类的类则为子类(sub class)。

继承特性:

单根性。在java一个类只能有一个直接父类。

传递性。C继承于BB继承于AC具有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);

 

posted @ 2019-04-26 20:34  hitter  阅读(184)  评论(0)    收藏  举报