《Java入门第二季》第五章 阶段练习

/**
 *         ┏┓   ┏┓
 *        ┏┛┻━━━┛┻┓
 *        ┃       ┃  
 *        ┃   ━   ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ... ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃ 神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃       
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
public class Test {
    public static void main(String[] args) {
        Person p1 = new Chinese();
        Person p2 = new American();
        p1.say();
        p2.say();
    }
}
/* Output:
你好
Hello
 */
public abstract class Person {
    abstract void say();
}
public class Chinese extends Person {
    public void say() {
        System.out.println("你好");
    }
}
public class American extends Person {
    public void say() {
        System.out.println("Hello");
    }
}

 也可以实现一个Sayable接口。

原文地址:https://www.cnblogs.com/xkxf/p/6536666.html