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

37次阅读

共计 1191 个字符,预计需要花费 3 分钟才能阅读完成。

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

继承实现
/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();
}

}

抽象类实现
/javaDemo6/src/com/imooc/APerson.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();
}

}

正文完
 0