用户名:小头爸爸007
/**
*
*@authorasus
*/
classShape{//描述抽象的形状
prote问答ctedintdegree;//边框厚度
prote肥具ctedintcolor程别;//边框颜色
public斯练啊宗吸款讲有Shape(){//默认构造方法
degree=1;
color神反行乱约独=1;//假设1表示红色,2表示绿色3表示蓝色
}
publicShape(intdegree,intcolor){//构造方法,初始化边框厚度和颜色
***.degree=degree;
t令必坏his.color=岁演讲比此句老重期头color;
}
publicdoublearea(){//返办走牛顺点粮合际关回面积,0
r造住黄呀均eturn0;
}
publicvoidDraw(){//绘制形状,给出默认形状
System.out.println("画出任意形状,边框["+
degree+"]颜色["+
color+"]");
}
}
classCirclee蛋致血预xtendsShape{//定义圆形
priva雨电娘历限tedoublera委空攻补复dius;//半径
publicCircle(doubleradius){//构造方法,初始化半径,边框厚度和颜色使用默认值
super();
this.radius=radi照us;
}
publicCircle(intdegree,intcolor,doubleradius){//构造方法,初始化半径,边框厚度和颜色使用传入值
super(degree,color);
this.radius=radius;
}
publicdoublearea(){//返回圆形面积
return3.14*radius*radius;
}
publicvoidDraw(){//绘制圆形
System.out值了卷磁者目.println("画出圆形,边框["+
degree认责+"]颜色["+
color+"]半径"+
radius+
"]");
}
}
classRectangleextendsS风台怎hape{//定义矩形
privatedo货远波怀果反ublelength;//长度
privatedoublewidth;//宽度
publicRectangle(doublelength,d留等研坐资散oublewidth){//构造方法,初始化长坚哥影马宽,边框厚度和颜色使用默认值
super();
this.length=length;
this.width=width;
}
publicRectangle(intdegree,intcolor,doublelength,doublewidth){//构造方法,初始化长宽,边框厚度和颜色使用传入值
super(degree,color);
this.length=length;
this.width=width;
}
publicdoublearea(){//返回矩形面积
returnlength*width;
}
publicvoidDraw(){//绘制矩形
System.out.println("画出矩形,边框["+
degree+"]颜色["+
color+
"]长度"+length+
"]宽度"+width+
"]");
}
}
publicclassMainClass{//主类
privateShapeshape;//形状,父类对象可以代表子类对象
publicMainClass(Shapeshape){//构造方法,初始化形状对象
this.shape=shape;
}
publicvoidsetShape(Shapeshape){//重置形状对象
this.shape=shape;
}
publicdoublegetAreaOfShape(){//获取面积
returnshape.area();
}
publicvoidDrawShape(){//绘制形状
shape.Draw();
}
publicstaticvoidmain(Stringargs[]){//主方法
Shapeshape;//定义一个形状引用
shape=newCircle(5.0);//构造一个圆形
MainClasstest=newMainClass(shape);//构造主类,并用shape初始化
test.DrawShape();//绘制形状
System.out.println("areaofshapeis"+test.getAreaOfShape());
shape=newRectangle(2,2,5.0,5.0);//构造一个矩形,边框厚度和颜色使用自定义值
test.setShape(shape);//用矩形设置形状
test.DrawShape();//绘制形状
System.out.println("areaofshapeis"+test.getAreaOfShape());
}
}