ex10.java

来自「JAVA编程思想第四版英文原版习题答案. pdf原版的」· Java 代码 · 共 32 行

JAVA
32
字号
// polymorphism/Ex10.java
// TIJ4 Chapter Polymorphism, Exercise 10, page 290
/* Create a base class with two methods. In the first method, call the second
* method. Inherit a class and override the second method. Create an object of
* the derived class, upcast it to the base type, and call the first method. 
* Explain what happens.
*/

class A {
	protected void f() { 
		System.out.println("A.f()");
		this.g(); 
	}
	protected void g() {
		System.out.println("A.g()"); 
	}
}

class B extends A {
	@Override protected void g() {
		System.out.println("B.g()");
	}
}

public class Ex10 {
	public static void main(String[] args) {
		B b = new B();
		// automatic upgrade to base-class to call base-class method A.f()
		// which,by polymorphism, will call the derived-class method B.g():
		b.f();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?