其他分享
首页 > 其他分享> > Code1.1.2

Code1.1.2

作者:互联网

代码

多态
向上转型,向下转型
static,final

public class Nineteen {
    public static void main(String[] args) {
        Human h1 = new Superman();
        h1.setName("Clark");
        h1.setHeight(180.0);
        h1.show();
        Human.communication();
        h1.move();
        Superman s1 = (Superman) h1;
        s1.duty();

        System.out.println("--------");

        Superman h2 = new Superman("David", 179.0);
        h2.show();
        Human.communication();
        h2.move();
        h2.duty();
    }
}

class Human {
    private String name;
    static private double height;
    final static private String COUNTRY = "American";

    public Human() {
    }

    public Human(String name, double height) {
        this.name = name;
        Human.height = height;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setHeight(double height) {
        Human.height = height;
    }

    public double getHeight() {
        return height;
    }

    public void show() {
        System.out.println(" " + COUNTRY);
    }

    public static void communication() {
        System.out.println("speak " + COUNTRY + " language");
    }

    public void move() {
        System.out.println("walk");
    }

}

class Superman extends Human {
    public Superman() {
    }

    public Superman(String name, double height) {
        super(name, height);
    }

    public void show() {
        System.out.print(getName() + " " + getHeight());
        super.show();
    }

    public void move() {
        System.out.println("fly");
    }

    public void duty() {
        System.out.println("save lives");
    }
}

标签:name,void,height,Code1.1,Human,public,Superman
来源: https://blog.csdn.net/weixin_54231215/article/details/112462476