欢迎加群:239063848

成团的笔记:该组仅用于技术共享和交流,问题和答案公布

潘基聊天、禁止广告、禁止招聘……

练习1:(2)创建一个简单的类。第二个类中,将一个引用定义为第一个类的对象。运用惰性初始化来实例化 这个对象。

package test;

public class Manager {

	public static void main(String args[]){
Second s=new Second();
s.getFirst();
}
/**
* 打印结果: */
}
class First{
}
class Second{ First f; Second(){
System.out.println("Creating Second");
} First lazy(){
if(f==null){
System.out.println("Creating First");
f=new First();
}
return f;
} public First getFirst(){
return lazy();
}
}

练习2:(2)从Detergent中继承产生一个新的类。覆盖scrub()并加入一个名为sterilize()的新方法。

package test;

public class Manager {

	public static void main(String args[]){
Sub s=new Sub();
s.apply();s.dilute();s.foam();s.scrub();s.sterilize();
new print(s);
} /**
* 打印结果:
Cleanser apply() dilute() foam() sub.scrub Detergent.scrub()sub.sterilize()
*/
}
class print{
print(Object obj){
System.out.println(obj);
}
}
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 static void main(String[] args){
Cleanser x=new Cleanser();
x.dilute();x.apply();x.scrub();
new print(x);
}
}
class Detergent extends Cleanser{
public void scrub(){
append(" Detergent.scrub()");
}
public void foam(){
append(" foam()");
} }
class Sub extends Detergent{
public void scrub(){
append(" sub.scrub");
super.scrub();
}
public void sterilize(){
append("sub.sterilize()");
}
}

练习3(2)证明前面两句话(即使你不为Cartoon创建构造器,编译器也为会你合成一个默认的构造器,该构造器将调用基类的构造器)

package test;

public class Manager {

	public static void main(String args[]){
new Cartoon();
} /**
* 打印结果:
Art constructor
Drawing constructor
*/
}
class print{
print(Object obj){
System.out.println(obj);
}
}
class Art{
Art(){
new print("Art constructor ");
}
}
class Drawing extends Art{
Drawing(){
new print("Drawing constructor ");
}
}
class Cartoon extends Drawing{
}

练习4(2)证明基类构造器总是会被调用。在导出类构造器之前被调用。

package test;

public class Manager {

	public static void main(String args[]){
new Child();
} /**
* 打印结果:
父类构造器输出
子类构造器输出
*/
}
class print{
print(Object obj){
System.out.println(obj);
}
}
class Parent{
Parent(){
new print("基类构造器输出");
}
}
class Child extends Parent{
Child(){
new print("子类构造器输出");
}
}

练习5:(1)创建两个带有默认构造器(空參数列表)的类A和类B。

从A中继承产生一个名为C的新。并在C内创建一个B类的成员。不要给C编写构造器。

创建一个C类的对象并观察其结果。

package test;

public class Manager {

	public static void main(String args[]){
new C();
} /**
* 打印结果:
A()……
B()……
B()……
C()……
*/
}
class print{
print(Object obj){
System.out.println(obj);
}
}
class A{
A(){
new print("A()……");
}
}
class B{
B(){
new print("B()……");
} }
class C extends A{
private B b=new B();
C(){
new print("C()……");
}
private B b2=new B();
}

练习6:(1)用Chess证明前面两名话

package test;

public class Manager {

	public static void main(String args[]){
new Chess();
} /**
* 打印结果:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Implicit super constructor BordGame() is undefined. Must explicitly invoke another constructor at test.Chess.<init>(Manager.java:32)
at test.Manager.main(Manager.java:6)
*/
}
class print{
print(Object obj){
System.out.println(obj);
}
}
class Game{
Game(int i){
new print("Game constructor");
}
}
class BordGame extends Game{
BordGame(int i){
super(i);
new print("BordGame constructor");
} }
class Chess extends BordGame{ Chess(){//Implicit super constructor BordGame() is undefined. Must explicitly invoke another constructor
new print("Chess constructor");
} }

练习7:(1)改动练习5,使A和B以带參数的构造器代替默认的构造器。为C写一个构造器,并在当中运行全部初始化。

package test;

public class Manager {

	public static void main(String args[]){
new C();
} /**
* 打印结果:
A()……
B()……
B()……
C()……
*/
}
class print{
print(Object obj){
System.out.println(obj);
}
}
class A{
A(int i){
new print("A()……");
}
}
class B{
B(){
new print("B()……");
} }
class C extends A{
private B b=new B();
C(){
super(1);//必须
new print("C()……");
}
private B b2=new B();
}

练习8:(1)创建一个基类,它仅有一个非默认构造器;再创建一个导出类,它带有默认构造器和非默认构造器。在导出类的构造器中调用基类的构造器。

package test;
public class Test { public static void main(String[] args) { }
} class A{ /** 非默认构造器 **/
A(int i){
System.out.println("基类");
}
}
class B extends A{ B(){
super(1);/** 调用基类构造函数 **/
}
B(int i){
super(i);/** 调用基类构造函数 **/
System.out.println("导出类");
}
}

练习9:(2)创建一个Root类。令其含有名为Component1、Component 2、Component3的类的各一个实例(这些也由你写)。

从Root中派生一个类Stem,也含有上述各“组成部分”。

全部的类都应带有可打印出类的相关信息的默认构造器。

package test;
public class Test { public static void main(String[] args) {
new Stem();
}
/**
* 输出
Component1 constructor
Component2 constructor
Component3 constructor
Root constructor
Component1 constructor
Component2 constructor
Component3 constructor
Stem constructor
*/
} class Root{ private Component1 component1=new Component1(); private Component2 component2=new Component2(); private Component3 component3=new Component3(); Root(){
System.out.println("Root constructor");
}
}
class Stem extends Root{ private Component1 component1=new Component1(); private Component2 component2=new Component2(); private Component3 component3=new Component3(); Stem(){
System.out.println("Stem constructor");
}
}
class Component1{ Component1(){
System.out.println("Component1 constructor");
}
}
class Component2{ Component2(){
System.out.println("Component2 constructor");
}
}
class Component3{ Component3(){
System.out.println("Component3 constructor");
}
}

练习10:(1)改动练习9,使每一个类都仅具有非默认的构造器。

package test;
public class Test { public static void main(String[] args) {
new Stem(1);
}
/**
* 输出
Component1 constructor 1
Component2 constructor 2
Component3 constructor 3
Root constructor
Component1 constructor 1
Component2 constructor 2
Component3 constructor 3
Stem constructor
*/
} class Root{ private Component1 component1=new Component1(1); private Component2 component2=new Component2(2); private Component3 component3=new Component3(3); Root(int i){
System.out.println("Root constructor");
}
}
class Stem extends Root{ private Component1 component1=new Component1(1); private Component2 component2=new Component2(2); private Component3 component3=new Component3(3); Stem(int i){
super(i);
System.out.println("Stem constructor");
}
}
class Component1{ Component1(int i){
System.out.println("Component1 constructor "+i);
}
}
class Component2{ Component2(int i){
System.out.println("Component2 constructor "+i);
}
}
class Component3{ Component3(int i){
System.out.println("Component3 constructor"+i);
}
}

练习11:(3)改动Detergent.java。让它使用代理。

package test;

public class Test {

	public static void main(String args[]){
Sub s=new Sub();
s.apply();s.dilute();s.foam();s.scrub();s.sterilize();
new print(s);
} /**
* 打印结果:
Cleanser apply() dilute() foam() sub.scrub Detergent.scrub()sub.sterilize()
*/
}
class print{
print(Object obj){
System.out.println(obj);
}
}
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 static void main(String[] args){
Cleanser x=new Cleanser();
x.dilute();x.apply();x.scrub();
new print(x);
}
}
class Detergent{ Cleanser Cleanser=new Cleanser(); public void append(String str){
Cleanser.append(str);
} public void dilute(){
append(" dilute()");
}
public void apply(){
append(" apply()");
}
public String toString(){
return Cleanser.toString();
} public void scrub(){
append(" Detergent.scrub()");
}
public void foam(){
append(" foam()");
} }
class Sub extends Detergent{
public void scrub(){
append(" sub.scrub");
super.scrub();
}
public void sterilize(){
append("sub.sterilize()");
}
}

练习12:(3)将一个适当的dispose()方法的层次结构加入到练习9的全部类中。

package test;
public class Test { public static void main(String[] args) {
Stem s=new Stem();
try{
s.toString();
}finally{
s.dispose();
}
}
/**
* 输出
Component1 constructor
Component2 constructor
Component3 constructor
Root constructor
Component1 constructor
Component2 constructor
Component3 constructor
Stem constructor
Stem dispose
Root dispose
*/
} class Root{ private Component1 component1=new Component1(); private Component2 component2=new Component2(); private Component3 component3=new Component3(); Root(){
System.out.println("Root constructor");
} void dispose(){
System.out.println("Root dispose");
}
}
class Stem extends Root{ private Component1 component1=new Component1(); private Component2 component2=new Component2(); private Component3 component3=new Component3(); Stem(){
System.out.println("Stem constructor");
} void dispose(){
System.out.println("Stem dispose");
super.dispose();
}
}
class Component1{ Component1(){
System.out.println("Component1 constructor");
} void dispose(){
System.out.println("Component1 dispose");
}
}
class Component2{ Component2(){
System.out.println("Component2 constructor");
} void dispose(){
System.out.println("Component2 dispose");
}
}
class Component3{ Component3(){
System.out.println("Component3 constructor");
} void dispose(){
System.out.println("Component3 dispose");
}
}

练习13:(2)创建一个类。它应带有一个被重载了三次的方法。

继承产生一个新类,并加入一个该方法的新的重载定义,展示这四个方法在导出类中都是能够使用的。

package test;
public class Test { public static void main(String[] args) {
Stem s=new Stem();
s.a();
int i=10;
s.a(i);
float f=10f;
s.a(f);
double d=10d;
s.a(d);
}
/**
* 输出
a()
a(int)10
a(float)10.0
a(double)10.0
*/
} class Root{ void a(){
System.out.println("a()");
}
void a(int i){
System.out.println("a(int)"+ i);
}
void a(float i){
System.out.println("a(float)"+ i);
}
}
class Stem extends Root{
void a(double i){
System.out.println("a(double)"+ i);
} }

行使14:(1)在Car.java在以Engine加入一service(),和main()调用此方法。

Java编程思想第四版*第七章*个人练习的更多相关文章

  1. java编程思想第四版第七章习题

    (略) (略) (略) (略) 创建两个带有默认构造器(空参数列表)的类A和类B.从A中继承产生一个名为C的新,并在C内创建一个B类的成员.不要给C编写构造器.创建一个C类的对象并观察其结果. pac ...

  2. java编程思想第四版第七章总结

    1. 实现类的复用通常有两种方式 组合:在新的类中产生现有类的对象 继承:按照现有类的类型来创造新类 2. 一个特殊的方法toString() 在非基本类型的对象中, 都有toString()方法 当 ...

  3. java编程思想第四版第十三章字符串 习题

    fas 第二题 package net.mindview.strings; import java.util.ArrayList; import java.util.List; /** * 无限循环 ...

  4. java编程思想第四版第十一章习题

    第一题 package net.mindview.holding.test1; import java.util.ArrayList; import java.util.List; /** * 沙鼠 ...

  5. java编程思想第四版第六章习题

    (略) (略) 创建两个包:debug和debugoff,他们都包含一个相同的类,该类有一个debug()方法,第一个版本显示发送给控制台的String参数,而第二版本什么也不做,使用静态import ...

  6. java编程思想第四版第六章总结

    1. 代码重构 为什么f要代码重构 第一次代码不一定是完美的, 总会发现更优雅的写法. 代码重构需要考虑的问题 类库的修改不会破坏客户端程序员的代码. 源程序方便扩展和优化 2. 包 创建一个独一无二 ...

  7. java编程思想第四版第五章习题

    创建一个类, 它包含一个未初始化的String引用.验证该引用被Java初始化成了null package net.mindview.initialization; public class Test ...

  8. java编程思想 第四版 第六章 个人练习

    欢迎加群:239063848 进群须知:本群仅用于技术分享与交流.问题公布与解答 禁止闲聊.非诚勿扰 练习1:(1)在某个包中创建一个类,在这个类所处的包的外部创建该类的一个实例. import mi ...

  9. java编程思想第四版第十三章字符串 总结

    1. String和StringBulider的使用 通过书中介绍, 我们得知如下结论: 当使用+连接符将字符串进行拼接的时候, 编译器会进行自动优化为使用StringBuilder连接字符串. 当在 ...

随机推荐

  1. javascript的族家族史

    JavaScript 实现 完整的 JavaScript 实现是由以下 3 个不同部分组成的:ECMAScript.文档对象模型.浏览器对象模型.这也就是说 cocos2d-js 中 其实我们用的是 ...

  2. !! python 之半年总结

    http://blog.chinaunix.net/uid-26443921-id-3481357.html 半年前开始系统完整深入的了解学习 python 读书篇: <python 核心编程2 ...

  3. Android:WebView深入使用

    webView = (WebView) findViewById(R.id.info_detail_webview); WebSettings webSettings = webView.getSet ...

  4. Visual Studio中一个解决方案设置多个启动项目

    在解决方案上右键,选择属性. 这样设置之后,点击开始运行之后,会同时启动2个项目. 适合一个项目既包含客户端也包含服务端,方便调试

  5. Trigger Execution Sequence in Oracle Forms

    Introduction ------------ This document lists the order in which triggers fire in Oracle Forms 4.5: ...

  6. C#编程实现Excel文档中搜索文本

    有了在Word文档中编程实现搜索文本的经验,在Excel中实现这个功能也并非难事. 打开Excel的VBA帮助,查看Excel的对象模型,很容易找到完成这个功能需要的几个集合和对象:Applicati ...

  7. BZOJ_1601_[Usaco2008_Oct]_灌水_(最小生成树_Kruskal)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1601 有\(n\)个田地需要灌溉,每个田地可以自己引水,花费为\(w[i]\),或者连接其他被 ...

  8. NOI 2014 感想

    NOI2014结束了,我卡线登上了领奖台... 这是我第一次NOI,我觉得我收获了很多东西: 1.考前心态不重要,重要的是实力 真正考试的时候是顾不得想其他事情的 2.测试数据是人出的!不是随机的!不 ...

  9. Using the Task Parallel Library (TPL) for Events

    Using the Task Parallel Library (TPL) for Events The parallel tasks library was introduced with the ...

  10. HDU-1700 Points on Cycle

    这题的俩种方法都是看别人的代码,方法可以学习学习,要多看看.. 几何题用到向量.. Points on Cycle Time Limit: 1000/1000 MS (Java/Others)     ...