(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{
//            System.out.println("job");
//        }
        //或
        String jobInfo = (salary == 0)? "no job" : "job";
        System.out.println(jobInfo);
    }

    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 class Kids extends ManKind{
    private int yearsOld;
    
    public Kids() {
    }

    public Kids(int yearsOld) {
        this.yearsOld = yearsOld;
    }

    public void printAge(){
        System.out.println("I am " + yearsOld + " years old.");
        
    }

    public int getYearsOld() {
        return yearsOld;
    }

    public void setYearsOld(int yearsOld) {
        this.yearsOld = yearsOld;
    }
    
    public void employeed() {
        System.out.println("Kids should study and no job.");
    }
}
public class KidsTest {
    public static void main(String[] args) {
        
        Kids someKid = new Kids(12);
        
        someKid.printAge();
        
        someKid.setSalary(0);
        someKid.setSex(1);
        
        someKid.employeed();
        someKid.manOrWoman();
        
        
    }
}

(4)根据下图实现类。在CylinderTest类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积。

public class Circle {
    
    private double radius;//半径
    
    public Circle(){
        radius = 1.0;
    }
    
    public Circle(double radius){
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }
    
    //返回圆的面积
    public double findArea(){
        return Math.PI * radius * radius;
    }
    
}
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 Math.PI * getRadius() * getRadius() * getLength();
        return super.findArea() * getLength();
    }
    
    @Override
    public double findArea() {//返回圆柱的表面积
        return Math.PI * getRadius() * getRadius() * 2 + 
                2 * Math.PI * getRadius() * getLength();
    }

}
public class CylinderTest {
    public static void main(String[] args) {
        
        Cylinder cy = new Cylinder();
        
        cy.setRadius(2.1);
        cy.setLength(3.4);
        double volume = cy.findVolume();
        System.out.println("圆柱的体积为:" + volume);
        
        //没有重写findArea()时:
        //double area = cy.findArea();
        //System.out.println("底面圆的面积:" + area);
        //重写findArea()以后:
        double area = cy.findArea();
        System.out.println("圆柱的表面积:" + area);
        
        System.out.println("******************");
        Cylinder cy1 = new Cylinder();
        double volume1 = cy1.findVolume();
        System.out.println("圆柱的体积为:" + volume1);
    }
}
最后修改:2021 年 08 月 15 日 08 : 29 PM
如果觉得我的文章对你有用,请随意赞赏