Java编程思想第7章答案(1)

1、

package ch7;
class A{
}
public class Instance {
public static void main(String[] args) {
	A a;
	a=new A();
}
}

2、

package ch7;
class Cleanser{
	private String s="Cleanser";
	public void append(String a) {
		s+=a;
	}
	public void dilute() {
		append("dilute()");
	}
	public void apply() {
		append("apply()");
	}
	public void scrub() {
		append("scrub()");
	}
	public String toString() {
		return s;
	}
}
class Detergent extends Cleanser{
public void scrub() {
	append("Detergent.scrub()");
	super.scrub();
}
public void foam() {
	append("foam()");
}
}
public class Detergent2 extends Detergent{
public void scrub() {
	append("Detergent2.scrub");
	super.scrub();
}
public void sterilize() {
	append("sterilize()");
}
public static void main(String[] args) {
	Detergent2 y=new Detergent2();
	y.sterilize();
	y.scrub();
	System.out.println(y);
}
}

3、

package ch7;
class Art {
	Art(){
		System.out.println("Art constructor");
	}
}
class Drawing extends Art {
	Drawing(){
		System.out.println("Drawing constructor");
	}
}
public class Cartoon extends Drawing{
public static void main(String[] args) {
	Cartoon x=new Cartoon();
}
}

4、第二题和第三题
5、

package ch7;
class A{
	A() {
		System.out.println("A");
	}
}
class B{
	B() {
		System.out.println("B");
	}
}
public class C extends A{
B b=new B();
public static void main(String[] args) {
	C c=new C();
}
}

7、

package ch7;
class A{
	A(int i) {
		System.out.println("A");
	}
}
class B{
	B(int i) {
		System.out.println("B");
	}
}
public class C extends A{
B b=new B(3);
C(int i){
	super(i);
	System.out.println("C");
}
public static void main(String[] args) {
	C c=new C(4);
}
}

8、
package ch7;
class A1{
A1(int i){
System.out.println(“A1”);
}
}
public class A3 extends A1{
A3(){
super(3);
System.out.println(“a3”);
}
A3(int i){
super(i);
System.out.println(“A3”);
}
public static void main(String[] args) {
A3 a1=new A3();
A3 a2=new A3(4);
}
}
9、

package ch7;
class Component1{
Component1(){
	System.out.println("Component1");
}
}
class Component2{
Component2(){
	System.out.println("Component2");
}
}
class Component3{
Component3(){
	System.out.println("Component3");
}
public String toString() {
	return("Component3 is used");
}
}
class Root{
Root(){
	System.out.println("Root");
}
Component1 a=new Component1();
Component2 b=new Component2();
static Component3 c=new Component3();
}
public class Stem extends Root{
Stem(){
	System.out.println("Stem");
}
public static void main(String[] args) {
	Stem s=new Stem();
	System.out.println(c);
}
}

10、
package ch7;
class Component1{
Component1(){
System.out.println(“Component1”);
}
}
class Component2{
Component2(){
System.out.println(“Component2”);
}
}
class Component3{
Component3(){
System.out.println(“Component3”);
}
public String toString() {
return(“Component3 is used”);
}
}
class Root{
Root(){
System.out.println(“Root”);
}
Component1 a=new Component1();
Component2 b=new Component2();
static Component3 c=new Component3();
}
public class Stem extends Root{
Stem(){
System.out.println(“Stem”);
}
public static void main(String[] args) {
Stem s=new Stem();
System.out.println©;
}
}
11、

package ch7;
class Cleanser{
	private String s="Cleanser";
	public void append(String a) {
		s+=a;
	}
	public void dilute() {
		append("dilute()");
	}
	public void apply() {
		append("apply()");
	}
	public void scrub() {
		append("scrub()");
	}
	public String toString() {
		return s;
	}
}
public class Detergent2{
private Cleanser control=new Cleanser();
public void scrub() {
	control.append("Detergent2.scrub");
	control.scrub();
}
public void sterilize() {
	control.append("sterilize()");
}
public static void main(String[] args) {
	Detergent2 y=new Detergent2();
	y.sterilize();
	y.scrub();
	System.out.println(y.control);
}
}

12、
package ch7;
class Component1 extends Root{
Component1(int i){
super(i);
System.out.println(“Component1”);
}
void dispose() {
System.out.println(“Component1 dispose”);
super.dispose();
}
}
class Component2 extends Root{
Component2(int i){
super(i);
System.out.println(“Component2”);
}
void dispose() {
System.out.println(“Component2 dispose”);
super.dispose();
}
}
class Component3 extends Root{
Component3(int i){
super(i);
System.out.println(“Component3”);
}
void dispose() {
System.out.println(“Component3 dispose”);
super.dispose();
}
public String toString() {
return(“Component3 is used”);
}
}
class Root{
Root(int i){
System.out.println(“Root”);
}
void dispose() {
System.out.println(“Root dispose”);
}

}
public class Stem extends Root{
Component1 a=new Component1(3);
Component2 b=new Component2(4);
static Component3 c=new Component3(5);
Stem(int i){
super(i);//或super(3),super(4)都是可以的
System.out.println(“Stem”);
}
void dispose() {
a.dispose();
b.dispose();
c.dispose();
System.out.println(“Stem dispose”);
super.dispose();
}
public static void main(String[] args) {
Stem s=new Stem(3);
System.out.println©;
try {

}finally {
	s.dispose();
}

}
}
这题如果把a或者b或者c的定义语句移到Root类中就会产生java.lang.StackOverflowError,是因为循环调用引起的,子类定义语句调用超类,超类定义变量又调用子类定义语句,导致报错。
13、

package ch7;
class B1{
void b1(){
	System.out.println("None");
}
void b1(int i) {
	System.out.println(i);
}
void b1(char c) {
	System.out.println(c);
}
}
public class B extends B1{
void b1(String s) {
	System.out.println(s);
}
public static void main(String[] args) {
	B b=new B();
	b.b1();
	b.b1(3);
	b.b1('x');
	b.b1("xyz");
}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. Bruce Eckel. "Thinking in Java" (4th Edition). Prentice Hall, 2006. 2. James Gosling, Bill Joy, Guy Steele, and Gilad Bracha. "The Java Language Specification" (3rd Edition). Addison-Wesley Professional, 2005. 3. Herbert Schildt. "Java: A Beginner's Guide" (6th Edition). McGraw-Hill Education, 2014. 4. Cay S. Horstmann and Gary Cornell. "Core Java" (Volume 1 and 2). Prentice Hall, 2013. 5. Joshua Bloch. "Effective Java" (2nd Edition). Addison-Wesley Professional, 2008. 6. Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea. "Java Concurrency in Practice". Addison-Wesley Professional, 2006. 7. Elliotte Rusty Harold. "Java I/O" (2nd Edition). O'Reilly Media, 2006. 8. Doug Lea. "Concurrent Programming in Java: Design Principles and Pattern" (2nd Edition). Addison-Wesley Professional, 2000. 9. Martin Fowler. "Refactoring: Improving the Design of Existing Code" (2nd Edition). Addison-Wesley Professional, 2018. 10. Bruce Tate, Justin Gehtland, and Erik Hatcher. "Better, Faster, Lighter Java" (1st Edition). O'Reilly Media, 2004. 中文翻译: 1. Bruce Eckel. "Java编程思想" (第4版). 机械工业出版社, 2008. 2. James Gosling, Bill Joy, Guy Steele, and Gilad Bracha. "Java语言规范" (第3版). 机械工业出版社, 2006. 3. Herbert Schildt. "Java程序设计教程" (第6版). 机械工业出版社, 2015. 4. Cay S. Horstmann and Gary Cornell. "Java核心技术" (卷1和2). 机械工业出版社, 2013. 5. Joshua Bloch. "Effective Java" (第2版). 机械工业出版社, 2009. 6. Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea. "Java并发编程实践". 机械工业出版社, 2009. 7. Elliotte Rusty Harold. "Java I/O" (第2版). 中国电力出版社, 2014. 8. Doug Lea. "Java并发编程:设计原则与模式" (第2版). 机械工业出版社, 2009. 9. Martin Fowler. "重构:改善既有代码的设计" (第2版). 人民邮电出版社, 2019. 10. Bruce Tate, Justin Gehtland, and Erik Hatcher. "轻量级Java企业应用开发" (第1版). 机械工业出版社, 2005.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

returnadsss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值