关于继承的练习(thinking in java),汽车

这篇博客探讨了Java中的继承概念,通过一系列练习展示了如何构建和初始化类,包括创建构造函数、类的组合与继承,以及static final与final的区别。在汽车类的上下文中,讨论了不同组件类的设计和向上转型的运用。最后,通过具体的代码示例解释了从beetle类继承的特定类型beetle的行为和输出解析。

练习1:为C编写一个构造函数,并在C的构造函数中执行所有初始化。

class A{
    A(char c , int i){
        System.out.println("A(char , int)");
    }
}
class B extends A{
    B(String s , float f){
        super(' ',0);
        System.out.println("B(String , float)");
    }
}
public class C extends A{
    private char c;
    private int i;
    C(char a , int j){
        super(a,j);
        c = a;
        i = j;
    }
    B b = new B("hi" , 1f);//先构造A,再构造B
    public static void main(String[] args){
        C c = new C('b' , 2);//先构造一个A
    }

}

输出结果

A(char , int)//先生成A
//先生成A再生成B
A(char , int)
B(String , float)

练习二:创建一个名为Root的类,它包含一个名为Component1、Component2和Component3的类(您也创建了这个类)的实例。从Root派生一个类Stem,它也包含每个“Com”的一个实例。所有类都应该有默认的构造函数来打印关于该类的消息

class Component1{
    Component1(byte b){
        System.out.println("Com1(byte)");
    }
}
class Component2{
    Component2(short s) {
        System.out.println("Com2(short)");
    }
}
class Component3{
    Component3(int i){
        System.out.println("Com3(int)");
    }
}
class Root{
    Component1 c1root;
    Component2 c2root;
    Component3 c3root;
    Root(float f){
        c1root = new Component1((byte)0);
        c2root = new Component2((short)0);
        c3root = new Component3(0);
        System.out.println("Root(float)");
    }
}
public class Stem extends Root{
    Component1 c1stem;
    Component2 c2stem;
    Component3 c3stem;
    Stem(double d){
        super(2.78f);
        c1stem = new Component1((byte)1);
        c2stem = new Component2((short)1);
        c3stem = new Component3(1);
        System.out.println("Stem(double)");
    }
    public static void main(String[] args){
        Stem s = new Stem(10.78);
    }
}

输出结果:

Com1(byte)
Com2(short)
Com3(int)
Root(float)
Com1(byte)
Com2(short)
Com3(int)
Stem(double)

练习3:完全委托和不完全委托

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 cleanser = new Cleanser();
        System.out.println(cleanser);
    }
}
public class DetergentDelegation {
    private String s = "DetergentDelegation";
    Cleanser c = new Cleanser();
    public void append(String a){
        s += a;
    }
    //两个方法完全委托给 c
    public void dilute(){
        c.dilute();
    }
    public void apply(){
        c.apply();
    }
    //部分委托c
    public void scrub(){
        append(" DetergentDelegation.scrub()");
        c.scrub();
    }
    public void foam(){
        append(" foam()");
    }
    public String toString(){
        return s + "\n" + c;
    }
    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("检验基类");
        Cleanser.main(args);
    }
}

结果:

DetergentDelegation DetergentDelegation.scrub() foam()
Cleanser dilute() apply() scrub()
检验基类
Cleanser

练习4:汽车

class Engine{
    public void start(){}
    public void rev(){}
    public void stop(){}
    public void service(){
        System.out.println("service engine");
    }
}
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();
    public 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.right.window.rolldown();
        car.wheels[3].inflate(74);
        car.engine.service();
    }
}

练习5:向上转型

class Amphibian{
    protected void swim(){
        System.out.println("Amphibian swim()");
    }
    protected void speak(){
        System.out.println("Amphibian speak()");
    }
    void eat(){
        System.out.println("Amphibian eat()");
    }
    static void grow(Amphibian a){
        System.out.println("Amphibian grow");
        a.eat();
    }
}

public class Frog extends Amphibian{
    public static void main(String[] args){
        Frog f = new Frog();
        //基类方法
        f.swim();
        f.speak();
        f.eat();
        //向上转型
        Amphibian.grow(f);
    }
}

练习6:static final 和 final 的差别

import java.util.Random;

class Test {
    Test() {
        System.out.println("Test()");
    }
}

public class Difference {
    private String name;
    public Difference(String s) { name = s;  }
    static final Test sft = new Test(); // constant reference address
    private final Test ft = new Test();
    static final String SFS = "static final"; // class constant
    private final String fs = "final";
    private static Random rand = new Random();
    static final int SFI = rand.nextInt(); // class constant
    private final int fi = rand.nextInt();
    public String toString() {
        return (name + ": " + sft + ", " + ft + ", " + SFS + ", "
         + fs + ", " + SFI + ", " + fi);
    }
    public static void main(String[] args) {
        Difference d1 = new Difference("d1");
        Difference d2 = new Difference("d2");
        Difference d3 = new Difference("d3");
        System.out.println(d1);
        System.out.println(d2);
        System.out.println(d3);
    }
}

输出结果:

Test()
Test()
Test()
Test()
d1: Test@74a14482, Test@1540e19d, static final, final, 450926229, -2025517821
d2: Test@74a14482, Test@677327b6, static final, final, 450926229, 1208676960
d3: Test@74a14482, Test@14ae5a5, static final, final, 450926229, 2041195552

练习:在bettle.java,从类beetle中继承特定类型beetle,遵循与现有类相同的格式。跟踪并解释输出

class insert{
    private int i = 9;
    protected int j;
    insert(){
        System.out.println("i = " + i + " , " + " j = " + j);
        j = 39;
    }
    private static int x1 = printInit("static insert.x1 init");
    static int printInit(String s){
        System.out.println(s);
        return 47;
    }
}
class Beetle extends insert{
    private int k = printInit("bettle.k init");
    public Beetle(){
        System.out.println("k =" + k);
        System.out.println("j =" + j);
    }
    private static int x2 = printInit("static bettle.x2 init");
}
//在甲虫。java,从类beetle中继承特定类型beetle,遵循与现有类相同的格式*。跟踪并解释输出。
public class Scarab extends Beetle{
    private int n = printInit("scarb.n init");
    public Scarab(){
        System.out.println("n = " + n);
        System.out.println("j = " + j);
    }
    private static int x3 = printInit("static scarab.x3 init");
    public static void main(String[] args){
        System.out.println("Scarab constructor");
        Scarab scarab = new Scarab();
    }
}

输出结果:

static insert.x1 init
static bettle.x2 init
static scarab.x3 init
Scarab constructor
i = 9 ,  j = 0
bettle.k init
k =47
j =39
scarb.n init
n = 47
j = 39
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值