重写父类方法后,可以实现功能极大扩展,你可以试着调试一下下面的代码- public class ExtendsTest {
- public static void main(String[] args) {
- Person p1 = new Chinese();
- Person p2 = new Japanese();
- Person p3 = new American();
- Person[] plist = new Person[]{p1,p2,p3};
- for(int i=0;i<3;i++){
- plist[i].sayHello();
- }
- }
- }
- abstract class Person{
- abstract void sayHello();
- }
- class Chinese extends Person{
- @Override
- void sayHello() {
- System.out.println("Hello,I am Chinese");
- }
-
- }
- class Japanese extends Person{
- @Override
- void sayHello() {
- System.out.println("Hello,I am Japanese");
- }
-
- }
- class American extends Person{
- @Override
- void sayHello() {
- System.out.println("Hello,I am American");
- }
-
- }
复制代码 运行结果是:
Hello,I am Chinese
Hello,I am Japanese
Hello,I am American
重写父类方法后,可以实现功能极大扩展,由此可见一斑
|