java继承重写super关键字用法
父类
package 继承2;
//植物
public class Botany {
public double height;
public int age;
public Double getHeight() {
return height;
}
public void setHeight(double height){
this.height = height;
}
public int getAge(){
return age;
}
public void setage(int age){
this.age = age;
}
public Botany(double height,int age){
this.height=height;
this.age=age;
}
public void eat(){
System.out.println(“牡丹花很香很香”);
}
}
子类
package 继承2;
public class Peony extends Botany {
public void color(){
System.out.println(“牡丹花的颜色:红色”);
}
public void behavior(){
System.out.println("牡丹花在光合作用");
}
public void fragrance(){
System.out.println("很香很香的花");
}
public Peony(double height,int age){
super(height,age);
}
}
测试类
package 继承2;
public class TestPeony {
public static void main(String[] args){
Peony d= new Peony(150,3);
System.out.println(“牡丹花的高度为”+d.getHeight()+“cm”);
System.out.println(“牡丹花的年龄为:”+d.getAge()+“岁”);
d.behavior();
d.color();
d.fragrance();
}
}
结果
本文地址:https://blog.csdn.net/m0_51186260/article/details/109575437