package shape; public class Shape { //定义成员变量 private double zhouchang; private double mianji; 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; } }
package shape; public class Sanjiao extends Shape { //定义新的成员方法用来求周长 public String qiuZC(double bian1,double bian2,double bian3) { if((bian1+bian2)>bian3&&(bian2+bian3)>bian1&&(bian1+bian3)>bian2) return "三角形的周长为:"+(bian1+bian2+bian3); else return "该三角形的边长不合法,两边之和必须大于第三边"; } }
package shape; public class Jvxing extends Shape { //定义新的成员方法用来求周长 public String qiuZC(double chang, double kuan) { if(chang>0&&kuan>0) return "矩形的周长为:"+2*(chang+kuan); else return "该矩形的长和宽不合法,长和宽必须大于0"; } }
package shape; public class E { public static void main(String[] args) { //实例化三角形对象 Sanjiao sj = new Sanjiao(); System.out.println(sj.qiuZC(5, 5.6, 8)); System.out.println(sj.qiuZC(5, 5.6, 25.2)); //实例化矩形对象 Jvxing jx= new Jvxing(); System.out.println(jx.qiuZC(5, 5.6)); System.out.println(jx.qiuZC(28, 0)); } }