转载

java入门第二季--面向对象实现人类说话,中国人说汉语,美国人说英语

人类说话,中国人说汉语,美国人说英语

java入门第二季--面向对象实现人类说话,中国人说汉语,美国人说英语

继承实现

/javaDemo6/src/com/imooc/Person.java

package com.imooc;

public class Person {
    public void say() {
        System.out.println("人类具有说话的能力");
    }
}

/javaDemo6/src/com/imooc/Chinese.java

package com.imooc;

public class Chinese extends Person{
    public void say() {
        System.out.println("中国人说汉语");
    }
}

/javaDemo6/src/com/imooc/American.java

package com.imooc;

public class American extends Person{
    public void say() {
        System.out.println("美国人说英语");
    }
}

/javaDemo6/src/com/imooc/Test.java

package com.imooc;


public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Chinese p1 = new Chinese();
        American p2 = new American();
        p1.say();
        p2.say();
    }

}

java入门第二季--面向对象实现人类说话,中国人说汉语,美国人说英语

抽象类实现

/javaDemo6/src/com/imooc/APerson.java

java入门第二季--面向对象实现人类说话,中国人说汉语,美国人说英语

package com.imooc;

public abstract class APerson {
    public abstract void say();
}

/javaDemo6/src/com/imooc/Chinese.java

package com.imooc;

public class Chinese extends APerson{
    public void say() {
        System.out.println("中国人说汉语");
    }
}

/javaDemo6/src/com/imooc/American.java

package com.imooc;

public class American extends APerson{
    public void say() {
        System.out.println("美国人说英语");
    }
}

/javaDemo6/src/com/imooc/Test.java

package com.imooc;


public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        APerson p1 = new Chinese();
        APerson p2 = new American();
        p1.say();
        p2.say();
    }

}

java入门第二季--面向对象实现人类说话,中国人说汉语,美国人说英语

原文  https://segmentfault.com/a/1190000017993835
正文到此结束
Loading...