欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java继承重写super关键字用法

程序员文章站 2024-01-03 23:52:28
父类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){th...

父类
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();
}
}

结果

java继承重写super关键字用法

本文地址:https://blog.csdn.net/m0_51186260/article/details/109575437

上一篇:

下一篇: