类的多态接口与抽象类的练习
interface Tiji{
public abstract void meng();
}
abstract class Chenchen {
abstract double getS();
abstract double getV();
void printSV(){
System.out.println("表面积:"+getS());
}
}
class Diandian extends Chenchen implements Tiji{
double r,h;
Diandian(double x,double y){r=x;h=y;}
double getS(){return 2*(Math.PI*r*r)+(2*Math.PI*r*h);}
double getV(){return Math.PI*r*r*h;}
public void meng(){
System.out.println("圆柱体积:"+getV());
}
}
class Chen extends Chenchen implements Tiji{
double a,b,h;
public Chen(double x,double y,double z){
a=x;
b=y;
h=z;
}
double getS(){return 2*(a*b+a*h+h*b);}
double getV(){return a*b*h;}
public void meng(){
System.out.println("长方体体积:"+getV());
}
}
public class homework{
public static void main (String[] args) {
Chenchen s=new Diandian(3,4);
s.printSV();
Tiji s1=new Diandian(3,4);
s1.meng();
Chenchen c1=new Chen(5,6,7);
c1.printSV();
Tiji c2=new Chen(5,6,7);
c2.meng();
}
}