• 继承练习


    练习一:

    ManKind:

    package com.aff.ex1;
    
    //定义一个ManKind类,包括成员变量int sex, int salary
    //方法 void manOrWoman();   根据sex的值显示man: sex == 1 , woman:sex == 0
    //方法 void employees(),  根据salary的值显示no  job: salary == 0 , job:salary != 0;
    public class ManKind {
        private int sex;
        private int salary;
    
        public int getSex() {
            return sex;
        }
    
        public void setSex(int sex) {
            this.sex = sex;
        }
    
        public int getSalary() {
            return salary;
        }
    
        public void setSalary(int salary) {
            this.salary = salary;
        }
    
        public void manOrWoman() {
            if (sex == 1) {
                System.out.println("Man");
            } else if (sex == 0) {
                System.out.println("Woman");
            } else {
                System.out.println("输入有误");
            }
        }
    
        public void employees() {
            if (salary == 0) {
                System.out.println("no job");
            } else if (sex > 0) {
                System.out.println("job");
            }
        }
    }

    Kids:

    package com.aff.ex1;
    
    //定义类Kids继承ManKind,并包括成员变量int  yearsOld;
    //方法printAge()打印yearsOld的值
    public class Kids extends ManKind {
        private int yearsOld;
    
        public int getYearsOld() {
            return yearsOld;
        }
    
        public void setYearsOld(int yearsOld) {
            this.yearsOld = yearsOld;
        }
    
        public void printAge() {
            System.out.println(this.yearsOld + "years old");
        }
    
    }

    TestKids:

    package com.aff.ex1;
    
    //在kid类的main方法中实例化Kid的对象someKid,
    //用该对象访问其父类的成员变量及方法
    public class TestKids {
        public static void main(String[] args) {
    
            Kids someKid = new Kids();
            someKid.setSalary(0);
            someKid.setSex(1);
            someKid.setYearsOld(13);
    
            someKid.employees();
            someKid.manOrWoman();
            someKid.printAge();
        }
    }

    输出结果:

    no job
    Man
    13years old

     

    练习二:

    Circle:

    package com.aff.ex1;
    
    public class Circle {
        private double radius;
    
        public Circle() {
            this.radius = 1;
        }
    
        public double getRadius() {
            return radius;
        }
    
        public void setRadius(double radius) {
            this.radius = radius;
        }
    
        // 计算圆的面积
        public double findArea() {
            return Math.PI * radius * radius;
        }
    
    }

    Cylinder:

    package com.aff.ex1;
    
    //圆柱:是对圆的扩展
    public class Cylinder extends Circle {
        private double length;// 圆柱的高
    
        public Cylinder() {
            length = 1;
        }
    
        public double getLength() {
            return length;
        }
    
        public void setLength(double length) {
            this.length = length;
        }
    
        // 圆柱的体积
        public double findVolum() {
            return findArea() * length;
            // return Math.PI * this.getRadius() * this.getRadius() * length;
        }
    }

    TestCylinder:

    package com.aff.ex1;
    
    public class TestCylinder {
        public static void main(String[] args) {
    
            Cylinder c = new Cylinder();
            System.out.println(c.findVolum());
    
            c.setLength(1.2);
            c.setRadius(2.3);
            System.out.println(c.findVolum());
        }
    }

    输出结果:

    3.141592653589793
    19.942830164988003

     
    All that work will definitely pay off
  • 相关阅读:
    这是另外一篇
    使用客户端写博客
    vim编码相关配置
    给eclipse装一些插件
    手机型号收集
    解决黑苹果与windows时区不一致
    记录一些在VPS上折腾的东西
    一个获取文件绝对路径的sh
    python批量GBK转UTF-8
    用NDK编译lua库
  • 原文地址:https://www.cnblogs.com/afangfang/p/12515745.html
Copyright © 2020-2023  润新知