标签:
编写一个Shape类,具有属性:周长和面积;
定义其子类三角形和矩形,分别具有求周长的方法。
定义主类E,在其main方法中创建三角形和矩形类的对象,
并赋给Shape类的对象a、b,使用对象a、b来测试其特性。
package T092208; public class Shape { private int bian; private double zhouchang; private double mianji; public int getBian() { return bian; } public void setBian(int bian) { this.bian = bian; } public double getZhouchang() { return zhouchang; } public void setZhouchang(double zhouchang) { this.zhouchang = zhouchang; } public double getMianji() { return mianji; } public void setMianji(double mianji) { this.mianji = mianji; } public void bian(){ System.out.println("您创建了一个"+bian+"边形!"); } } package T092208; public class San extends Shape { double[] bianchang=new double[3]; San(){ } San(double a,double b, double c){ bianchang[0]=a; bianchang[1]=b; bianchang[2]=c; } public void zhouchang(){ if(getBian()==3&&bianchang[0]+bianchang[1]>bianchang[2]&&bianchang[0]-bianchang[1]<bianchang[2]){ double sum=0; for(int i=0;i<bianchang.length;i++){ sum+=bianchang[i]; System.out.print(" 边长"+(i+1)+"是"+ bianchang[i]); } System.out.println(" 三角形的周长是:"+sum); } else{ System.out.println("这不是一个三角形"); } } } package T092208; public class juxing extends Shape { double[] bianchang=new double[4]; juxing(){} juxing(double a,double b,double c,double d){ bianchang[0]=a; bianchang[1]=b; bianchang[2]=c; bianchang[3]=d; } public void zhouchang(){ if(getBian()==4 && bianchang[0]==bianchang[2]&&bianchang[1]==bianchang[3]){ double sum=0; for(int i=0;i<bianchang.length;i++){ sum+=bianchang[i]; System.out.print(" 边长"+(i+1)+"是"+bianchang[i]); } System.out.println(" 矩形的周长是:"+sum); } else{ System.out.println("这不是一个矩形"); } } } package T092208; public class Test { public static void main(String[] args){ Shape s=new Shape(); s.setBian(5); s.bian(); San sa=new San(15,14,12); sa.setBian(3); sa.zhouchang(); juxing ju=new juxing(5,10,5,10); ju.setBian(4); ju.zhouchang(); } }
标签:
原文地址:http://www.cnblogs.com/wangguoning/p/5901684.html