abstract class Shape{ //定义一个形状的抽象类
private String color;
Shape(String c){color=c;}
abstract double getArea();
public String toString(){
return "颜色是"+color;
}
}
class JuXing extends Shape{ //定义一个矩形的类继承形状的抽象类
private double c,k;
JuXing(String s,double c,double k){
super(s); //运用super
this.c=c;
this.k=k;
}
public double getArea(){
return c*k;
}
public String toString(){ //算出矩形的面积并返回
return "这个矩形的面积为"+getArea()+super.toString();
}}
class SanJiaoXing extends Shape{ //定义三角形类并继承
private double d,g;
SanJiaoXing(String s,double d,double g){
super(s);
this.d=d;
this.g=g;
}
public double getArea(){
return d*g;
}
public String toString(){
return "这个三角形的面积是"+getArea()+super.toString();
}
}
private String color;
Shape(String c){color=c;}
abstract double getArea();
public String toString(){
return "颜色是"+color;
}
}
class JuXing extends Shape{ //定义一个矩形的类继承形状的抽象类
private double c,k;
JuXing(String s,double c,double k){
super(s); //运用super
this.c=c;
this.k=k;
}
public double getArea(){
return c*k;
}
public String toString(){ //算出矩形的面积并返回
return "这个矩形的面积为"+getArea()+super.toString();
}}
class SanJiaoXing extends Shape{ //定义三角形类并继承
private double d,g;
SanJiaoXing(String s,double d,double g){
super(s);
this.d=d;
this.g=g;
}
public double getArea(){
return d*g;
}
public String toString(){
return "这个三角形的面积是"+getArea()+super.toString();
}
}
public class Di164 {
public static void main(String[] args) {
// TODO Auto-generated method stub
JuXing a=new JuXing("蓝色",2,2); //初始化矩形
System.out.println(a.toString());
SanJiaoXing b=new SanJiaoXing("绿色",2,2);
System.out.println(b.toString());
// TODO Auto-generated method stub
JuXing a=new JuXing("蓝色",2,2); //初始化矩形
System.out.println(a.toString());
SanJiaoXing b=new SanJiaoXing("绿色",2,2);
System.out.println(b.toString());
}
}
程序结果
这个矩形的面积为4.0颜色是蓝色
这个三角形的面积是4.0颜色是绿色
这个三角形的面积是4.0颜色是绿色