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

    结果:

  • 相关阅读:
    被隐藏的文件更改为可见
    Selenium WebDriver多层表单切换
    for循环
    Java课程设计二次大作业
    Java-DAO模式代码阅读及应用
    编辑器、编译器、文件、IDE等常见概念辨析
    树、二叉树和查找等知识点的总结
    二叉树的实现
    二叉树顺序结构和链式结构的相互转换
    使用k-近邻算法改进约会网站的配对效果
  • 原文地址:https://www.cnblogs.com/tfl-511/p/5898663.html
Copyright © 2020-2023  润新知