⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frog17.java

📁 JAVA编程思想第四版英文原版习题答案. pdf原版的
💻 JAVA
字号:
// reusing/Frog17.java
// TIJ4, Chapter Reusing, Exercise 17, page 262
/* Modify Exercise 16 so that Frog overrides the method definitions from 
* the base class (provides new definitions using the same method 
* signatures). Note what happesn in main(). 
* ( Exersice 16:Create a class called Amphibian. From this, inherit a class
* called Frog. Put appropriate methods in the base class. In main(), create a
* Frog and upcast it to Amphibian, and demonstrate that all the methods still 
* work.)
*/
import static org.greggordon.tools.Print.*;

class Amphibian {
	protected void swim() {
		println("Amphibian swim");		
	}
	protected void speak() {
		println("Amphibian speak");
	}
	void eat() {
		println("Amphibian eat");
	}
	static void grow(Amphibian a) {
		println("Amphibian grow");
		a.eat();
	}
}

public class Frog17 extends Amphibian {
	@Override protected void swim() {
		println("Frog swim");		
	}
	@Override protected void speak() {
		println("Frog speak");
	}
	@Override void eat() {
		println("Frog eat");
	}
	static void grow(Amphibian a) {
		println("Frog grow");
		a.eat();
	}
	public static void main(String[] args) {
		Frog17 f = new Frog17();
		// call overridden base-class methods:
		f.swim();
		f.speak();
		f.eat();
		// upcast Frog17 to Amphibian argument:
		f.grow(f);
		// upcast Frog17 to Amphibian and call Amphibian method:
		Amphibian.grow(f);
	}
}

⌨️ 快捷键说明

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