欢迎加群: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. 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问

    检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问 已重装office2 ...

  2. IDM和ODM

    DM (Integrated Data Multiplexer):综合数据复用器[1]  综合数据复用器是一种数据复用设备,它可以将多路RS232.RS485及数字语音等多种数据复用到E1传输通道或光 ...

  3. 多设备官方教程(6)控制多版本API

    Supporting Different Platform Versions While the latest versions of Android often provide great APIs ...

  4. 加载网络映射盘中的assembly失败的处理办法

    错误症状: 1.{"未能加载文件或程序集“file://*****”或它的某一个依赖项.不支持操作. (异常来自 HRESULT:0x80131515)":"file:/ ...

  5. INV Close Period & GL Import Journal > DML tables

    1.       (N) Inventory > Accounting Close Cycle > Inventory Accounting Periods     Insert data ...

  6. [Swustoj 24] Max Area

    Max Area 题目描述: 又是这道题,请不要惊讶,也许你已经见过了,那就请你再来做一遍吧.这可是wolf最骄傲的题目哦.在笛卡尔坐标系正半轴(x>=0,y>=0)上有n个点,给出了这些 ...

  7. Xcode7工程改名

    0: 先把原工程压缩个zip包备份一份. 1:在Project navigator中, 选择旧工程名字, 再次点击, 修改. 会弹出提示框. 直接点击确认. (Xcode7.1.1中经常导致Xcode ...

  8. iphone4s丢失如何找回

    iphone4s丢失如何找回 iphone4s手机丢了怎么办,其实苹果手机自带找回功能,但是前提你得打开了icloud这款软件. 方法/步骤 1 在手机的设置里找到icloud设置,如图. 2 点击进 ...

  9. win8系统开发者预览版安装中文软件报错怎么办

    相信大家看到这边博客时,应该也是遇到类似的问题,这个可能是win8的一个bug.最直接的解决方法是,将目前语言环境由中文改为英文,再安装运行软件应该就不会有问题了. 但是,我们下次在安装应用程序时依然 ...

  10. 基于WebForm+EasyUI的业务管理系统形成之旅 -- 数据统计(Ⅳ)

    上篇<基于WebForm+EasyUI的业务管理系统形成之旅 -- 首页快捷方式>,主要介绍通过添加首页快捷方式,快速进入各个应用菜单功能. 将常用的菜单功能作为快捷方式,避免由于寻找诸多 ...