zoukankan      html  css  js  c++  java
  • 编写一个Shape类,具有属性:周长和面积; 定义其子类三角形和矩形,分别具有求周长的方法。 定义主类E,在其main方法中创建三角形和矩形类的对象, 并赋给Shape类的对象a、b,使用对象a、b来测试其特性。

    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));
    
        }
    
    }

  • 相关阅读:
    FSL
    64位MicrosoftOfficeWord加载EndnoteX7
    Lobes of the brain
    Anterior and posterior commissures
    Broadmann area (wiki)
    Broadmann分区
    matlab FDR校正
    AI图片剪切
    DPABI advanced edition 文件夹组织形式
    Frequently Asked Questions
  • 原文地址:https://www.cnblogs.com/HRZJ/p/5897818.html
Copyright © 2011-2022 走看看