类的继承day01
1. 以下代码的执行结果是 (C)
1 public class Person { 2 String name="Somebody"; 3 String say(){ 4 return "It's Somebody speaking"; 5 } 6 } 7 8 9 public class Man extends Person{ 10 String name="Mike"; 11 String say(){ 12 return "It's Mike speaking"; 13 } 14 } 15 16 public class Company { 17 public static void main(String[] args) { 18 new Company().sayhi(); 19 } 20 21 void sayhi(){ 22 Person m=new Man(); 23 System.out.println(m.name+" "+m.say()); 24 } 25 A Somebody It's Somebody speaking 26 B Mike It's Somebody speaking 27 C Somebody It's Mike speaking 28 D Mike It's Mike speaking 29 }
考察的知识点
1)父类声明指向子类对象,该引用只能父类中定义的方法和属性。
2) 如果子类中重写了父类的方法,那么在调用这个方法的时候,将会调用子类中的方法。