Java编程思想第四版*第七章*个人练习
欢迎加群: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("导出类");
}
}
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编程思想第四版*第七章*个人练习的更多相关文章
- java编程思想第四版第七章习题
(略) (略) (略) (略) 创建两个带有默认构造器(空参数列表)的类A和类B.从A中继承产生一个名为C的新,并在C内创建一个B类的成员.不要给C编写构造器.创建一个C类的对象并观察其结果. pac ...
- java编程思想第四版第七章总结
1. 实现类的复用通常有两种方式 组合:在新的类中产生现有类的对象 继承:按照现有类的类型来创造新类 2. 一个特殊的方法toString() 在非基本类型的对象中, 都有toString()方法 当 ...
- java编程思想第四版第十三章字符串 习题
fas 第二题 package net.mindview.strings; import java.util.ArrayList; import java.util.List; /** * 无限循环 ...
- java编程思想第四版第十一章习题
第一题 package net.mindview.holding.test1; import java.util.ArrayList; import java.util.List; /** * 沙鼠 ...
- java编程思想第四版第六章习题
(略) (略) 创建两个包:debug和debugoff,他们都包含一个相同的类,该类有一个debug()方法,第一个版本显示发送给控制台的String参数,而第二版本什么也不做,使用静态import ...
- java编程思想第四版第六章总结
1. 代码重构 为什么f要代码重构 第一次代码不一定是完美的, 总会发现更优雅的写法. 代码重构需要考虑的问题 类库的修改不会破坏客户端程序员的代码. 源程序方便扩展和优化 2. 包 创建一个独一无二 ...
- java编程思想第四版第五章习题
创建一个类, 它包含一个未初始化的String引用.验证该引用被Java初始化成了null package net.mindview.initialization; public class Test ...
- java编程思想 第四版 第六章 个人练习
欢迎加群:239063848 进群须知:本群仅用于技术分享与交流.问题公布与解答 禁止闲聊.非诚勿扰 练习1:(1)在某个包中创建一个类,在这个类所处的包的外部创建该类的一个实例. import mi ...
- java编程思想第四版第十三章字符串 总结
1. String和StringBulider的使用 通过书中介绍, 我们得知如下结论: 当使用+连接符将字符串进行拼接的时候, 编译器会进行自动优化为使用StringBuilder连接字符串. 当在 ...
随机推荐
- Cocos2d-x 3.2编译Android程序错误的解决方案
最近的升级Cocos2d-x 3.2正式版.iOS不管是什么程序编译问题,使用结果cocos compile -p android编译APK计划.结果悲剧,出现以下错误. Android NDK: I ...
- 157. Read N Characters Given Read4
题目: The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the ...
- 解决“重新安装vmware-tools”灰色而无法安装的问题
前几天重装系统,之后虚拟机需要重新装,装好后要使用vmware-tools实现文件共享,却发现虚拟机那里显示为灰色的,无法安装vmware-tools,在共享文件夹那里设置好共享的文件夹猴也没有用,/ ...
- Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()
问题: 写了一个sdk给其他人用,提供一个回调函数,函数使用了handler处理消息 // handler监听网络请求,完成后操作回调函数 final Handler trigerGfHandler ...
- 查看wtmp文件内容
1./var/log/wtmp文件的作用 /var/log/wtmp也是一个二进制文件,记录每个用户的登录次数和持续时间等信息! 2.查看方法:可以用last命令输出当中内容 1 2 3 ...
- 清理Xcode中无用的 Provisioning Profile.
Xcode中如果添加过多个开发者账号, 慢慢就会发现 Provisioning Profile 有很多, 无用的 Provisioning Profile Xcode也不会自动删除, 想要清理的话, ...
- MVC View基础
View主要用于呈现数据.由于Controller和相关的Service已经处理完业务逻辑并将结果打包成model实体,View只需要怎么去获得model并将其转为Html 1选择需要渲染的视图 在上 ...
- hdu-5364 Distribution money
http://acm.hdu.edu.cn/showproblem.php?pid=536 Distribution money Time Limit: 2000/1000 MS (Java/Othe ...
- json包的loads dumps区分
符合json格式的字符串 --(json.laods)--> json(字典形式或是列表形式) --(json.dumps)--> 符合json格式的字符串
- Select the best path in a matrix
Amazon interview question: Given a 2-dimensional array with arbitrary sizes and contains random posi ...