- (略)
- (略)
- (略)
- (略)
- 创建两个带有默认构造器(空参数列表)的类A和类B。从A中继承产生一个名为C的新,并在C内创建一个B类的成员。不要给C编写构造器。创建一个C类的对象并观察其结果。
package net.mindview.reusing; public class A { public A(){ System.out.println("A"); } } package net.mindview.reusing; public class B { public B(){ System.out.println("B"); } } package net.mindview.reusing; public class C extends A { B b = new B(); public static void main(String[] args) { C c = new C(); } }
6.(略)
7.(略)
8.(略)
9.创建一个Root类,令其含有名为Component1、Component 2、Component3的类的各一个实例(这些也由你写)。从Root中派生一个类Stem,也含有上述各“组成部分”。所有的类都应带有可打印出类的相关信息的默认构造器
package net.mindview.reusing; public class Root { private Component1 c1 = new Component1(); private Component2 c2 = new Component2(); private Component3 c3 = new Component3(); public Root() { System.out.println("Root"); System.out.println("Root->c1"); System.out.println("Root->c2"); System.out.println("Root->c3"); } } package net.mindview.reusing; public class Component1 { public Component1() { System.out.println("Component1"); } } package net.mindview.reusing; public class Component2 { public Component2() { System.out.println("Component2"); } } package net.mindview.reusing; public class Component3 { public Component3() { System.out.println("Component3"); } } package net.mindview.reusing; public class Stem extends Root { private Component1 c1 = new Component1(); private Component2 c2 = new Component2(); private Component3 c3 = new Component3(); public Stem() { System.out.println("RootChild"); System.out.println("Stem->c1"); System.out.println("Stem->c2"); System.out.println("Stem->c3"); } public static void main(String[] args) { Root t = new Stem(); } }
10.(略)可参考总结中的第5点,使用代理模式。
11.(略)
12.(略)
13.(略)
14.在Car.java中给Engine添加一个service(),并在main()中调用该方法。
package net.mindview.reusing; class Engine { public void start(){} public void rev(){} public void stop(){} public void Service() { System.out.println("Engine.Service"); } } class Wheel { public void inflate(int psi){} } class Window{ public void rollup(){} public void rolldown(){} } class Door{ public Window window = new Window(); public void open(){} public void close(){} } public class Car { public Engine engine = new Engine(); public Wheel[] wheels = new Wheel[4]; public Door left = new Door(),right = new Door(); public Car(){ for(int i=0;i<4; i++){ wheels[i] = new Wheel(); } } public static void main(String[] args) { Car car = new Car(); car.left.window.rollup(); car.wheels[0].inflate(72); car.engine.Service(); } }
15.(略)
16.创建一个名为Amphibian的类,由此继承产生一个成为Frog的类,在基类中设置适当的方法,在main()中,创建一个Frog向上转型至Amphibian, 然后说明所有方法都可工作
package net.mindview.reusing; //两栖动物 class Amphibian{ //交配 public void mating(Amphibian amphibian){ System.out.println("两栖动物交配"); } } //青蛙 public class Frog extends Amphibian{ public static void main(String[] args) { Frog frog = new Frog(); frog.mating(frog); } }
17.修改练习16,使Frog覆盖基类中方法的定义。请留心main中都发生了什么
package net.mindview.reusing; //两栖动物 class Amphibian{ //交配 public void mating(Amphibian amphibian){ System.out.println("两栖动物交配"); } } //青蛙 public class Frog extends Amphibian{ public void mating(Frog frog){ System.out.println("青蛙交配"); } public static void main(String[] args) { Frog frog = new Frog(); frog.mating(frog); } }
18.(略)
19.(略)