Java基础知识强化11:多态的两道基础题

1.第1题

 1 class  Base {
 2     public void method() {
 3         System.out.print("Base method");
 4     }
 5 }
 6 
 7 class  Child extends  Base {
 8     public void methodB() {
 9         System.out.print("Child methodB");
10     }
11 }
12 
13 class Sample {
14     public static void main(String[]  args) {
15         Base base = new Child();
16         base.methodB();
17     }
18 }

A. Base  method

B. Child methodB

C. Base method Child MethodB

D. 编译错误

2.第2题

 1 class Person {
 2     int a;
 3     public int change(int m) {
 4         return m;
 5     }
 6 }
 7 
 8 public class Teacher extends Person {
 9     public int b;
10     public static void main(String[] args) {
11         Person p = new Person();
12         Teacher t = new Teacher();
13         int i;
14         ______//此处填写代码
15     }
16 }

A.i=b;

B.i=a;

C.i=p.change(30);

D.i=t.b;

3.

注意:

(1)对于成员变量:编译看左边运行看左边

(2)对于成员方法:编译看左边运行看右边

(3)第1题:父类引用子类只能调用子类从父类继承过来的方法或者子类重写继承自父类的方法

         第2题:静态方法不能调用非静态成员,静态成员属于类的,main属于子类中,它也是方法,但是而且是static 的静态方法,只能调用静态的变量。因此,int a和 int b

均是不能直接在main中直接调用的

原文地址:https://www.cnblogs.com/hebao0514/p/4772197.html