Java编程思想第7章习题答案(2)

14、

//Override 重写 @Override必须重写,否则会报错。 Overload重载
14、
package ch7;
class Engine{
	public void start() {}
	public void rev() {		
	}
	public void stop() {		
	}
}
class Wheel{
	public void inflate(int psi) {
	}
}
class Window {
	public void rollup() {		
	}
	public void rolldown() {		
	}
}
class Door{
	public Window window=new Window();
	public void open() {		
	}
	public void close() {
	}
}
public class Car extends Engine{
public Engine engine=new Engine();
public Wheel[] wheel=new Wheel[4];
public Door
left=new Door(),
right=new Door();
void service() {
	System.out.println("service");
}
public Car() {
	for(int i=0;i<4;i++)
		wheel[i]=new Wheel();
}
public static void main(String[] args) {
	Car car=new Car();
	car.left.window.rollup();
	car.wheel[0].inflate(72);
	car.service();
}
}

15、

package ch7;
class W{
static protected void hello() {
		System.out.println("Hello");
	}	
}
public class What {
public static void main(String[] args) {
W.hello();
}
}

16、

package ch7;

class Amphibian {
void tune(Amphibian x) {
	System.out.println("Amphibian was get");
}
}
public class Frog extends Amphibian{
public static void main(String[] args) {
	Amphibian a=new Frog();
	a.tune(a);
}
}

17、

package ch7;

class Amphibian {
void tune(Amphibian x) {
	System.out.println("Amphibian was get");
}
}
public class Frog extends Amphibian{
void tune(Amphibian x) {
	System.out.println("Frog was get");
}
public static void main(String[] args) {
	Amphibian a=new Frog();
	a.tune(a);
}
}

18、

package ch7;
import java.util.*;
public class Final {
static Random rand=new Random(47);
static final int i=rand.nextInt(20);
final int j=rand.nextInt(20);
public static void main(String[] args) {
	Final f1=new Final();
	System.out.println(f1.i);
	System.out.println(f1.j);
	Final f2=new Final();
	System.out.println(f2.i);
	System.out.println(f2.j);
}
}

19、

package ch7;
import java.util.*;
class Ha{
	private int j;
	Ha(int j){
		j=j;
	}
}
public class Final {
private final Ha h;
private final int i;
Final(){
i=1;
h=new Ha(1);
}
Final(String s){
i=3;
h=new Ha(i);
}
public static void main(String[] args) {
Final f=new Final();
//f.h=new Ha(2);
Final f2=new Final("wo");
//f2.h=new Ha(3);
}
}

20、不知道在问什么
21、

package ch7;
import java.util.*;
class Ha{
final void Hi() {
	System.out.println("Hi");
}
}
public class Final extends Ha{
void Hi(int i) {
	System.out.println("Hi from Final");
}
public static void main(String[] args) {

}
}

22、

package ch7;
final class Hi{
int i;
void show() {
	System.out.println(i);
}
}
//public class Jesus extends Hi 报错
public class Jesus{
public static void main(String[] args) {
	Hi hi=new Hi();
	hi.i=3;
	hi.i=4;
	hi.show();
}
}

23、

//加载是指static变量及方法的初始化,子类实体创建和静态变量访问都会引发加载
//当子类对象创建时,不只是会加载,父类的变量定义赋值和构造方法都会被调用。
package ch7;
class A2{
static int i=printInit("hello");
int l=printInit("what");
A2(){
	System.out.println("A was used");
}
static int printInit(String s) {
	System.out.println(s);
	return 48;
}
}
class B2 extends A2{
static int j=printInit("hi");
B2(){
	System.out.println("B was used");
}
}
public class Wo {
static int k;
public static void main(String[] args) {
	B2 b=new B2();
	//k=B2.j;
}
}

24、

package ch7;
class Insect{
	private int i=9;
	protected int j;
	Insect(){
		System.out.println("i="+i+",j"+j);
		j=39;
	}
	private static int x1=printInit("static Insect.x1 initialized");
	static int printInit(String s) {
		System.out.println(s);
		return 47;
	}
}
class Beetle extends Insect{
	protected int k=printInit("Beetle.k initialized");
	public Beetle() {
		System.out.println("k="+k);
		System.out.println("j="+j);
	}
	private static int x2=printInit("static Beetle.x2 initialized");
}
public class SmallBeetle extends Beetle{
	private int l=printInit("SmallBeetle.l initialized");
	public SmallBeetle() {
		System.out.println(k);
		System.out.println(j);
		System.out.println(l);
	}
	private static int x3=printInit("static SmallBeetle.x3 initialized");
public static void main(String[] args) {
	System.out.println("SmallBeetle constructed");
	SmallBeetle sb=new SmallBeetle();
}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

returnadsss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值