练习5:(1)创建两个带有默认构造器的类A、B。从A中继承产生一个名为C的新类,并在C内创建B类的成员。C中无构造器。创建一个C类的对象并观察其结果。
package com.gzy.think;
public class P05 {
public static void main(String[] args) {
new C();
}
}
class A{
public A() {
System.out.println("我是A()");
}
}
class B{
public B() {
System.out.println("我是B()");
}
}
class C extends A {
B b = new B();
}
output:
我是A()
我是B()
练习6:用代码证明“调用基类构造器必须是你在导出类构造器中要做的第一件事”
package com.gzy.think;
public class P06 extends BoardGame {
P06() {
super(1);
System.out.println("Chess constructor");
}
public static void main(String[] args) {
new P06();
}
}
class Game {
Game(int i) {
System.out.println("Game constructor");
}
}
class BoardGame extends Game {
BoardGame(int i) {
super(i);
System.out.println("BoardGame constructor");
}
}
output:
Game constructor
BoardGame constructor
Chess constructor
但是如果没有调用基类构造器,也即注释掉super(i),会出现如下错误:
noUse.png练习7:修改练习5,使A和B以带参数的构造器取代默认的构造器。为C写一个构造器并在其中执行所有的初始化。
package com.gzy.think;
class A2{
public A2(int i) {
System.out.println("我是A2()" + i);
}
}
class B2{
public B2(String s) {
System.out.println("我是B2()"+ s );
}
}
class C2 extends A2 {
public C2(int i,String s) {
super(i);
new B2(s);
}
}
public class P07 {
public static void main(String[] args) {
new C2(1,"Init String");
}
}
output:
我是A2()1
我是B2()Init String
练习8:创建一个基类,仅有一个非默认构造器;再创建一个导出类,它带有默认构造器和非默认构造器。在导出类的构造器中调用基类的构造器。
package com.gzy.think;
class NonDefaultBase {
public NonDefaultBase(int i) {
}
}
class DerivedTwoConstructors extends NonDefaultBase {
public DerivedTwoConstructors() {
super(1);
System.out.println("默认构造器");
}
public DerivedTwoConstructors(int i) {
super(i);
System.out.println("非默认构造器 " + i);
}
}
public class P08 {
public static void main(String[] args) {
new DerivedTwoConstructors();
new DerivedTwoConstructors(11);
}
}
output:
默认构造器
非默认构造器 11
练习9:创建一个Root类,令其含有名为Component1、Component2、Component3的类的各一个实例。从Root中派生一个Stem,也含有上述各“组成部分”。所有的类应带有可打印出类的相关信息的默认构造器。
package com.gzy.think;
class Component1 {
public Component1() {
System.out.println("Component1");
}
}
class Component2 {
public Component2() {
System.out.println("Component2");
}
}
class Component3 {
public Component3() {
System.out.println("Component3");
}
}
class Root {
Component1 c1 = new Component1();
Component2 c2 = new Component2();
Component3 c3 = new Component3();
public Root() {
System.out.println("Root");
}
}
class Stem extends Root {
Component1 c1 = new Component1();
Component2 c2 = new Component2();
Component3 c3 = new Component3();
public Stem() {
System.out.println("Stem");
}
}
public class P09 {
public static void main(String[] args) {
new Stem();
}
}
output:
Component1
Component2
Component3
Root
Component1
Component2
Component3
Stem
练习11:修改Detergent.java,让它使用代理。
package com.gzy.think;
class Cleanser {
private String s = "Cleanser";
public void append(String a) {
s += a;
}
public void dilute() {
append(" diulte()");
}
public void apply() {
append(" apply()");
}
public void scrub() {
append(" scrub()");
}
public String toString() {
return s;
}
}
public class DetergentDelegation {
private Cleanser cleanser = new Cleanser();
public void append(String a) {
cleanser.append(a);
}
public void dilute() {
cleanser.dilute();
}
public void apply() {
cleanser.apply();
}
public String toString() {
return cleanser.toString();
}
public void scrub() {
append(" DetergentDelegation.scrub()");
cleanser.scrub();
}
public void foam() {
append(" foam()");
}
public static void main(String[] args) {
DetergentDelegation x = new DetergentDelegation();
x.dilute();
x.apply();
x.scrub();
x.foam();
System.out.println(x);
System.out.println("Testing base class");
}
}
output
Cleanser diulte() apply() DetergentDelegation.scrub() scrub() foam()
Testing base class
练习13:创建一个类,带有一个被重载了三次的方法。继承产生一个新类,并添加一个该方法的新的重载定义,展示这四个方法在导出类中都可使用。
package com.gzy.think;
class ThreeOverloads {
public void f(int i) {
System.out.println("f(int i)");
}
public void f(double d) {
System.out.println("f(double d)");
}
public void f(char c) {
System.out.println("f(char c)");
}
}
class FourOverloads extends ThreeOverloads {
public void f(String s){
System.out.println("f(String s)");
}
}
public class P13 {
public static void main(String[] args) {
FourOverloads fo = new FourOverloads();
fo.f(1);
fo.f(1.111);
fo.f('x');
fo.f("sdd");
}
}
output
f(int i)
f(double d)
f(char c)
f(String s)
网友评论