Java-继承,多态0922-05
28.按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,
和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、
宽、高,输出其底面积和体积。
父类:
package com.lianxi4;public class Juxing {
private double chang;
private double kuan;
public double getChang() {
return chang;
}
public void setChang(double chang) {
this.chang = chang;
}
public Juxing(double chang, double kuan) {
super();
this.chang = chang;
this.kuan = kuan;
}
public Juxing() {
super();
}
public double mianJi()
{
return chang*kuan;
}
}
子类:
package com.lianxi4;public class Tiji extends Juxing {
private double gao;
public Tiji(double chang, double kuan, double gao) {
super(chang, kuan);
this.gao = gao;
}
public double tiJi()
{
return super.mianJi()*gao;
}
}
测试:
package com.lianxi4;public class Test {
public static void main(String[] args) {
Tiji ti=new Tiji(5,6,7);
System.out.println("面积:"+ti.mianJi());
System.out.println("体积:"+ti.tiJi());
}
}
结果:
以上是 Java-继承,多态0922-05 的全部内容, 来源链接: utcz.com/z/392902.html