• Java-继承,多态练习0922-06


    编写一个Shape类,具有属性:周长和面积;

    定义其子类三角形和矩形,分别具有求周长的方法。

    定义主类E,在其main方法中创建三角形和矩形类的对象,

    并赋给Shape类的对象a、b,使用对象a、b来测试其特性。

    父类:

    package com.lianxi5;
    
    //抽象类
    public abstract class Shape {
        private double mianJi;
        private double zhouchang;
        public double getMianJi() {
            return mianJi;
        }
        public void setMianJi(double mianJi) {
            this.mianJi = mianJi;
        }
        public double getZhouchang() {
            return zhouchang;
        }
        public void setZhouchang(double zhouchang) {
            this.zhouchang = zhouchang;
        }
        
        public abstract double zhouchang();
        
        
        
    
    }

    子类;

    package com.lianxi5;
    
    public class Zc extends Shape {
        private double x;
        private double y;
        private double z;
        
    
        public double getX() {
            return x;
        }
    
        public void setX(double x) {
            this.x = x;
        }
    
        public double getY() {
            return y;
        }
    
        public void setY(double y) {
            this.y = y;
        }
    
        public double getZ() {
            return z;
        }
    
        public void setZ(double z) {
            this.z = z;
        }
    
        @Override
        public double zhouchang() {
            if((x+y)>z&&(x+z)>y&&(y+z)>x)
            {
                return x+y+z;
            }
            else
            {
            return 0;
            }
            
        
        }
    
    
    }
    package com.lianxi5;
    
    public class Jxzc extends Shape {
        private double x;
        private double y;
        
    
        public double getX() {
            return x;
        }
    
    
        public void setX(double x) {
            this.x = x;
        }
    
    
        public double getY() {
            return y;
        }
    
    
        public void setY(double y) {
            this.y = y;
        }
    
    
        @Override
        public double zhouchang() {
            
            return (x+y)*2;
        }
    
    }

    测试;

    package com.lianxi5;
    
    public class Test {
    
        public static void main(String[] args) {
            Zc z=new Zc();
            Jxzc j=new Jxzc();
            z.setX(7);
            z.setY(8);
            z.setZ(9);
            System.out.println(z.zhouchang());
            
            
    
        }
    
    }

    结果:

  • 相关阅读:
    【KMP】Period
    【KMP】Cyclic Nacklace
    【KMP】Oulipo
    【KMP】Number Sequence
    二分
    Stock 贪心经典 Zoj2921
    D. Block Tower
    烦人的dp
    搜索
    hdu 一个人的旅行
  • 原文地址:https://www.cnblogs.com/tfl-511/p/5898663.html
Copyright © 2020-2023  润新知