zoukankan      html  css  js  c++  java
  • 继承练习

    练习一:

    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
  • 相关阅读:
    PHP观察者模式
    php减少损耗的方法之一 缓存对象
    php迭代器模式
    数据库安全措施的改进依据------未实践
    mysql利用phpmyadmin导入数据出现#1044错误 的可能原因
    两列布局的基本思路
    less1.5中的减错误
    ie63像素bug原因及解决办法不使用hack
    镜像翻转二叉树
    判断一个整数是否是 2 的幂次方
  • 原文地址:https://www.cnblogs.com/afangfang/p/12515745.html
Copyright © 2011-2022 走看看