多态Polymorphism

向上转换Upcasting

分析题

Point.java

1 2 3 4
public class Point { public int x; public int y;}

Shape.java

1 2 3
public class Shape { public Point origin;}

Rect.java

1 2 3 4
public class Rect extends Shape { public int width; public int height;}

Circle.java

1 2 3
public class Circle extends Shape { public int radius;}

基于上面的代码,分析下面每段代码是否可以正常运行

1.
1 2 3
public void run() { Point rect = new Rect();}
A

可以正常运行

B

会报错

2.
1 2 3
public void run() { Shape rect = new Rect();}
A

可以正常运行

B

会报错

3.
1 2 3
public void run() { Circle rect = new Rect();}
A

可以正常运行

B

会报错

4.
1 2 3 4 5 6 7
public void run() { draw(new Rect());} public void draw(Shape shape) { ...}
A

可以正常运行

B

会报错

5.
1 2 3 4 5 6 7
public void run() { draw(new Shape());} public void draw(Shape shape) { ...}
A

可以正常运行

B

会报错

变量类型 与 调用保障

分析题

Point.java

1 2 3 4
public class Point { public int x; public int y;}

Shape.java

1 2 3 4 5 6 7
public class Shape { public Point origin; public void func1() { ... }}

Rect.java

1 2 3 4 5 6 7 8
public class Rect extends Shape { public int width; public int height; public void func2() { }}

基于上面的代码,分析下面每段代码是否可以正常运行

1.
1 2 3 4
public void run() { Rect rect = new Rect(); rect.func1();}
A

可以正常运行

B

会报错

2.
1 2 3 4
public void run() { Rect rect = new Rect(); rect.func2();}
A

可以正常运行

B

会报错

3.
1 2 3 4
public void run() { Shape shape = new Rect(); shape.func1();}
A

可以正常运行

B

会报错

4.
1 2 3 4
public void run() { Shape shape = new Rect(); shape.func2();}
A

可以正常运行

B

会报错

向下转换Downcasting

分析题

Point.java

1 2 3 4
public class Point { public int x; public int y;}

Shape.java

1 2 3 4 5 6 7
public class Shape { public Point origin; public void func1() { ... }}

Rect.java

1 2 3 4 5 6 7 8
public class Rect extends Shape { public int width; public int height; public void func2() { }}

Circle.java

1 2 3 4 5 6 7
public class Circle extends Shape { public int radius; public void func2() { }}

基于上面的代码,分析下面每段代码是否可以正常运行

1.
1 2 3 4
public void run() { Shape shape = new Rect(); Rect rect = shape;}
A

可以正常运行

B

编译时报错

C

运行时报错

2.
1 2 3 4
public void run() { Shape shape = new Rect(); Rect rect = (Rect)shape;}
A

可以正常运行

B

编译时报错

C

运行时报错

3.
1 2 3 4
public void run() { Shape shape = new Shape(); Rect rect = (Rect)shape;}
A

可以正常运行

B

编译时报错

C

运行时报错

4.
1 2 3 4
public void run() { Shape shape = new Rect(); Circle circle = (Circle)shape;}
A

可以正常运行

B

编译时报错

C

运行时报错

5.
1 2 3
public void run() { Circle circle = new Rect();}
A

可以正常运行

B

编译时报错

C

运行时报错

类型检测

分析题

Point.java

1 2 3 4
public class Point { public int x; public int y;}

Shape.java

1 2 3 4 5 6 7
public class Shape { public Point origin; public void func1() { ... }}

Rect.java

1 2 3 4 5 6 7 8
public class Rect extends Shape { public int width; public int height; public void func2() { }}

Circle.java

1 2 3 4 5 6 7
public class Circle extends Shape { public int radius; public void func2() { }}

基于上面的代码,分析下面每段代码的运行结果是什么

1.
1 2 3 4
public void run() { Rect rect = new Rect(); Console.println(rect instanceof Rect);}
A

True

B

False

2.
1 2 3 4
public void run() { Rect rect = new Rect(); Console.println(rect instanceof Shape);}
A

True

B

False

3.
1 2 3 4
public void run() { Shape shape = new Rect(); Console.println(shape instanceof Rect);}
A

True

B

False

4.
1 2 3 4
public void run() { Shape shape = new Rect(); Console.println(shape instanceof Shape);}
A

True

B

False

5.
1 2 3 4
public void run() { Rect rect = new Rect(); Console.println(rect.getClass() == Rect.class);}
A

True

B

False

6.
1 2 3 4
public void run() { Rect rect = new Rect(); Console.println(rect.getClass() == Shape.class);}
A

True

B

False

7.
1 2 3 4
public void run() { Shape shape = new Rect(); Console.println(shape.getClass() == Rect.class);}
A

True

B

False

8.
1 2 3 4
public void run() { Shape shape = new Rect(); Console.println(shape.getClass() == Shape.class);}
A

True

B

False

方法覆盖Method Overriding

分析题

Point.java

1 2 3 4
public class Point { public int x; public int y;}

Shape.java

1 2 3 4 5 6 7
public class Shape { public Point origin; public void draw() { Console.println("Shape draw"); }}

基于上面的代码,分析下面每段代码的运行结果是什么

1.

Rect.java

1 2 3 4
public class Rect extends Shape { public int width; public int height;}

Main.java

1 2 3 4 5 6
public class Main { public void run() { Rect rect = new Rect(); rect.draw(); }}
Shape draw
2.

Rect.java

1 2 3 4 5 6 7 8
public class Rect extends Shape { public int width; public int height; public void draw() { Console.println("Rect draw"); }}

Main.java

1 2 3 4 5 6
public class Main { public void run() { Rect rect = new Rect(); rect.draw(); }}
Rect draw
3.

Rect.java

1 2 3 4 5 6 7 8
public class Rect extends Shape { public int width; public int height; public void draw() { Console.println("Rect draw"); }}

Main.java

1 2 3 4 5 6
public class Main { public void run() { Shape rect = new Rect(); rect.draw(); }}
Rect draw
4.

Rect.java

1 2 3 4 5 6 7 8 9
public class Rect extends Shape { public int width; public int height; public void draw() { Console.println("Rect draw"); super.draw(); }}

Main.java

1 2 3 4 5 6
public class Main { public void run() { Rect rect = new Rect(); rect.draw(); }}
Rect draw
Shape draw

继承 x 多态Inheritance x Polymorphism

代码题

Graph 继承提升

Q36 E06

ZZAX 微信公众

文档一更新,立刻告诉你