1.编程定义一形状Shape接口,包含计算面积computerArea()抽象方法; 2编程定义一个矩形类实现形状接口,矩形类包含成员变量:长、宽和成员方法:computerArea ()计算面积和计算周长; 3.编写一个图形界面(GUI)应用程序,要求窗口界面使用2个文本框和一个文本区为矩形对象中 的数据提供视图,其中2个文本框用来接受矩形对象的属性值;文本区用来显示矩形对象的面积和 周长。窗口中有一个计算按钮,用户单击该按钮后,程序用2个文本框中的数据分别作为矩形对象 的长和宽创建矩形对象,并将该对象的面积和周长显示在文本区中。 4,要求处理输入数据格式异常。
/*Shap 接口*/
public interface Shap {
public int getArea();
public String toString();
}
/*Rectangle 抽象类*/
public abstract class Rectangle implements Shap{
public int width,length;
public Rectangle(int w,int l)
{
width = w;
length = l;
}
public Rectangle(){}
public int getArea()
{
return width*length;
}
public String toString()
{
return "长为:"+length+"\t宽为:"+width;
}
public abstract int getGirth();
}
/*Square具体类*/
public class Square extends Rectangle{
public Square(int l) {
super(l,l);
}
public int getGirth()
{
return 2*(width + length);
}
}
/*Test测试类*/
public class Test {
public static void main(String[] args) {
Square sq = new Square(5);//创建子类对象
Rectangle re = (Rectangle)new Square(3);//通过引用子类对象来创建父类对象
System.out.println (re+"\n面积为:"+re.getArea()+"\n");
System.out.println (sq+"\n面积为:"+sq.getArea()+"\t周长为:"+sq.getGirth());
}
}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
我可以帮你做。