Java继承

在计划继承时一个比较好的规则是:将所有类的成员字段都设为private,并将所有的方法都设置为public(protected成员也与允许衍生出来的类访问它)

Java提供了一个super关键字用来引用父类中的相关方法。

进行继承时,我们并不限于只能使用基础类的方法。亦可在衍生出来的类里加入自己的新方法。

创建衍生类(继承类)的一个对象时,它在其中包含了一个基础类(父类)的一个“子对象”。

基础类子对象应该正确地初始化,而且只有一种方法能保证这一点:在构造函数中执行初始化,通过调用基础类的构造函数,后者有足够的能力和权限来执行对基础类的初始化。在衍生类(继承类)的构建器中,Java 会自动插入对基础类构建器的调用。

/**
 * Created by xfyou on 2016/11/1.
 */
class Cleanser {
    private String s = new String("Cleanser");

    public void append(String a) {
        s += a;
    }

    public void dilute() {
        append(" dilute()");
    }

    public void apply() {
        append(" apply()");
    }

    public void scrub() {
        append(" scrub()");
    }

    public void print() {
        System.out.println(s);
    }

    public static void main(String[] args) {
        Cleanser x = new Cleanser();
        x.dilute();
        x.apply();
        x.scrub();
        x.print();
    }
}

public class Detergent extends Cleanser {
    // Change a method
    public void scrub() {
        append("Detergent.scrub()");
    }

    // Add methods to the interface:
    public void foam(){
        append("foam()");
    }

    // Test the new class
    public static void main(String[] args) {
        Detergent x = new Detergent();
        x.dilute();
        x.apply();
        x.scrub();
        x.foam();
        x.print();
        System.out.println("Testing base class:");
        Cleanser.main(args);
    }
}

  当基础类中用含有自定义了一个含有自变量的构造函数时(覆盖了默认的构造函数),继承类的构造函数中必须明确地显示的对父类构造函数的调用

public class Chess extends BoardGame {
    public Chess() {
        super(11);
        System.out.println("Chess constuctor");
    }

    public static void main(String[] args) {
        Chess x = new Chess();
    }
}

class Game{
    public Game(int i) {
        System.out.println("Game constructor");
    }
}

class BoardGame extends Game{
    public BoardGame(int i) {
        super(i);
        System.out.println("BoardGame constructor");
    }
}

 继承与合成结合起来使用:

public class PlaceSetting extends Custom {
    Spoon sp;
    Fork frk;
    Knife kn;
    DinnerPlate pl;

    public PlaceSetting(int i) {
        super(i + 1);
        sp = new Spoon(i + 2);
        frk = new Fork(i + 3);
        kn = new Knife(i + 4);
        pl = new DinnerPlate(i + 5);
        System.out.println("PlaceSetting constructor");
    }

    public static void main(String[] args) {
        PlaceSetting x = new PlaceSetting(9);
    }
}

class Plate {
    public Plate(int i) {
        System.out.println("Plate constructor");
    }
}

class DinnerPlate extends Plate {
    public DinnerPlate(int i) {
        super(i);
        System.out.println("DinnerPlate constructor");
    }
}

class Utensil {
    public Utensil(int i) {
        System.out.println("Utensil constructor");
    }
}

class Spoon extends Utensil {
    public Spoon(int i) {
        super(i);
        System.out.println("Spoon constructor");
    }
}

class Fork extends Utensil {
    public Fork(int i) {
        super(i);
        System.out.println("Fork constructor");
    }
}

class Knife extends Utensil {
    public Knife(int i) {
        super(i);
        System.out.println("Knife constructor");
    }
}

class Custom {
    public Custom(int i) {
        System.out.println("Custom constructor");
    }
}

 

posted @ 2016-11-01 16:48  FrankYou  阅读(417)  评论(0编辑  收藏  举报