Java

Java class

2019-02-08  本文已影响0人  JaedenKil
public class Dog {

    String speciesName;

    public Dog (String speciesName) {
        this.speciesName = speciesName;
    }

    public void show() {
        System.out.println("This is Dog class.");
    }

}
public class Husky extends Dog {

    String speciesName;
    boolean couchTerminator;

    public Husky(String speciesName, boolean couchTerminator) {
        super(speciesName);
        this.couchTerminator = couchTerminator;
    }

    @Override
    public void show() {
        System.out.println("This is Husky class which extends Dog class.");
    }

    public void terminateCouch() {
        if (couchTerminator) {
            System.out.println("This Hasky is a couch terminator.");
        } else {
            System.out.println("This Hasky is not a couch terminator.");
        }
    }
}
public class Doggy {
    public static void main(String[] args) {
        Dog dog01 = new Dog("Dog");
        Dog dog02 = new Husky("Dog", true);
        // Husky dog03 = new Dog("Dog"); // Required Husky, found Dog
        Husky dog04 = new Husky("Dog", false);

        dog01.show(); // dog01 has no "terminateCouch" method.
        dog02.show(); // dog02 has no "terminateCouch" method.
        dog04.show();
        dog04.terminateCouch();
    }
}
This is Dog class.
This is Husky class which extends Dog class.
This is Husky class which extends Dog class.
This Hasky is not a couch terminator.
上一篇下一篇

猜你喜欢

热点阅读