• 5-1继承的练习


    练习1基本操作

    (1)定义一个ManKind类,包括

    成员变量int sex和int salary;
    方法void manOrWoman():根据sex的值显示“man”(sex ==1)或者“woman”(sex ==0);
    方法void employeed():根据salary的值显示“no job”(salary ==0)或者“ job”(salary!=0)。

    (2)定义类Kids继承ManKind,并包括

    成员变量int yearsOld;
    方法printAge()打印yearsOld的值。

    (3)定义类KidsTest,在类的main方法中实例化Kids的对象someKid,用该对象访问

    其父类的成员变量及方法。

    /*
    * (1)定义一个ManKind类,包括
    成员变量int sex和int salary;
    方法void manOrWoman():根据sex的值显示“man”(sex==1)或者“woman”(sex==0);
    方法void employeed():根据salary的值显示“no job”(salary==0)或者“ job”(salary!=0)。
    (2)定义类Kids继承ManKind,并包括
    成员变量int yearsOld;
    方法printAge()打印yearsOld的值。
    (3)定义类KidsTest,在类的main方法中实例化Kids的对象someKid,用该对象访问
    其父类的成员变量及方法。
    * */
    public class ManKind {
    
        private int sex;	//性别
        private int salary;	//薪资
    
        public ManKind() {
    
        }
    
        public ManKind(int sex, int salary) {
            this.sex = sex;
            this.salary = salary;
        }
    
        public void manOrWoman(){
            if(sex==1){
                System.out.println("man");
            }else if(sex==0){
                System.out.println("woman");
            }
        }
    
        public void employeed(){
            if(salary==0){
                System.out.println("no job");
            }else if(salary!=0){
                System.out.println("job");
            }
        }
    
        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;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 定义类Kids继承ManKind,并包括
    • 成员变量int yearsOld;
    • 方法printAge()打印yearsOld的值
    /*
     * 定义类Kids继承ManKind,并包括
     * 成员变量int yearsOld;
     * 方法printAge()打印yearsOld的值
     *
     */
    public class Kids extends ManKind{
    
        private int yearsOld;	//年限
    
        public Kids() {
    
    
        }
    
        public Kids(int yearsOld) {
            this.yearsOld = yearsOld;
        }
    
        public int getYearsOld() {
            return yearsOld;
        }
    
        public void setYearsOld(int yearsOld) {
            this.yearsOld = yearsOld;
        }
    
        public void printAge(){
            System.out.println("I am " + yearsOld);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 定义类KidsTest,在类的main方法中实例化Kids的对象someKid,
    • 用该对象访问其父类的成员变量及方法。
    /*
     * 定义类KidsTest,在类的main方法中实例化Kids的对象someKid,
     * 用该对象访问其父类的成员变量及方法。
     *
     */
    public class KidsTest {
        public static void main(String[] args) {
    
            Kids someKid = new Kids(12);
    
            someKid.printAge();
    
            someKid.setYearsOld(15);
            someKid.setSalary(0);
            someKid.setSex(1);
    
            someKid.employeed();
            someKid.manOrWoman();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    练习2圆的面积

    Circle(圆)

    -radius :double
    Circle(): 构造器,将radius属性初始化为1
    +setRadius(double radius) : void
    +getRadius(): double
    +findArea():double 计算圆的面积

    /*
            Circle(圆)
    * -radius :double
    Circle(): 构造器,将radius属性初始化为1
    +setRadius(double radius) : void
    +getRadius(): double
    +findArea():double 计算圆的面积
    * */
    public class Circle {
    
        public double radius;	//半径
    
        public Circle(){
            radius = 1.0;
        }
    
        public double getRadius() {
            return radius;
        }
    
        public void setRadius(double radius) {
            this.radius = radius;
        }
    
        public double findArea(){	//计算圆的面积
            return Math.PI * radius * radius;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    cylinder(圆柱)

    -length:double
    Cylinder(): 构造器,将length属性初始化为1
    +setLength(double length):void
    +getLength():double
    +findVolume() :double 计算圆柱体积*/

    /*
            cylinder(圆柱)
    * -length:double
    Cylinder(): 构造器,将length属性初始化为1
    +setLength(double length):void
    +getLength():double
    +findVolume() :double 计算圆柱体积*/
    public class Cylinder extends Circle{
    
        private double length;
    
        public Cylinder(){
            length = 1.0;
        }
    
        public double getLength() {
            return length;
        }
    
        public void setLength(double length) {
            this.length = length;
        }
    
        public double findVolume(){	//计算圆柱体积
            return findArea() * length;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    测试类

    public class CylinderTest {
        public static void main(String[] args) {
    
            Cylinder cy = new Cylinder();
    
            cy.setRadius(2.1);
            cy.setLength(3.4);
            double volues = cy.findVolume();
            System.out.println("圆柱的体积:" + volues);
    
    
            double area = cy.findArea();
            System.out.println("圆的面积: " + area);
            System.out.println("-----------------------");
    
            //java保留两位小数操作
            //方法一:String.format()
            String format = String.format("%.2f", volues);
            System.out.println(format);
            //string类型转double
            Double aDouble = Double.valueOf(format);
            System.out.println(aDouble);
            //方法二:DecimalFormat
            DecimalFormat df = new DecimalFormat("#.00");
            System.out.println(df.format(volues));
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    设计一个简单的通讯录
    Spring MVC拦截器和跨域请求
    NPDP产品经理知识(市场调研-文化,团队,领导力)
    函数组件也可以通过connect获取react-redux的数据(reducer)
    测试饱和了? 大数据测试就业薪资和前景究竟怎么样?
    Java:Java与Node.js对比学习必备
    毕业了校园卡怎么改套餐?
    面试被吊打!正确打开Redis分布式锁的七种方案,涨见识了
    23种设计模式——工厂模式
    OpenCV图像处理——停车场车位识别
  • 原文地址:https://blog.csdn.net/qq_44774198/article/details/125563794