Java编程思想(八)

Java编程思想(八)

多态

多态是继数据抽象和继承之后的第三种基本特征


绑定

将一个方法调用同一个方法主体关联起来被称为绑定

前期绑定:在程序执行前进行绑定

后期绑定:又称动态绑定与运行时绑定,就是在运行时根据对象的类型进行绑定

Java汇总除了static方法与final方法之外,其他所有的方法都是后期绑定


多态与向上转型

书中此处结合代码讲的很好,但是我看到了一个更加简单的介绍

https://blog.csdn.net/kuishao1314aa/article/details/80842813


构造器与多态

基类构造器总在导出类的构造过程中被调用,并且是按照继承层次逐渐向上链接,使得每个基类构造器都能得到调用。这样做科研保证正确构造完整的对象。

import com.summer0811.Chess;

class Meal{
    Meal(){
        System.out.println("Meal()");
    }
}
class Bread{
    Bread(){
        System.out.println("Bread()");
    }
}
class Cheese{
    Cheese(){
        System.out.println("Cheese()");
    }
}

class Lunch extends Meal{
    Lunch(){
        System.out.println("Lunch()");
    }
}
class PortableLunch extends Lunch{
    PortableLunch(){
        System.out.println("PortableLunch");
    }
}

public class Sandwich extends PortableLunch {
    private Bread b = new Bread();
    private Cheese c = new Cheese();
    public Sandwich(){
        System.out.println("Sandwich()");
    }

    public static void main(String[] args) {
        new Sandwich();
    }
}

要深刻理解上面代码中有关于调用的层次关系


继承与清理

通过组合与继承创建新类时,不必担心对象的清理问题,子对象通常会留给垃圾回收器处理。

class Characteristic{
    private String s;
    Characteristic(String s){
        this.s = s;
        System.out.println("Creating Characteristic");
    }
    protected void dispose(){
        System.out.println("disposing Characteristic");
    }
}

class Description{
    private String s;
    Description(String s){
        this.s = s;
        System.out.println("Creating Description");
    }
    protected void dispose(){
        System.out.println("disposing Description");
    }
}
class LivingCreature{
    private Characteristic p = new Characteristic("is alive");
    private Description t = new Description("Basic Living Creature");
    LivingCreature(){
        System.out.println("LivingCreature()");
    }
    protected void dispose(){
        System.out.println("LivingCreature dispose");
        t.dispose();
        p.dispose();
    }
}

class Animal extends LivingCreature{
    private Characteristic p = new Characteristic("has heart");
    private Description t = new Description("Animal not Vegetable");
    Animal(){
        System.out.println("Animal()");
    }
    protected void dispose(){
        System.out.println("Animal dispose");
        t.dispose();
        p.dispose();
        super.dispose();
    }
}

class Amphibian extends Animal{
    private Characteristic p = new Characteristic("can live in water");
    private Description t = new Description("Both water and land");
    Amphibian(){
        System.out.println("Amphibian()");
    }
    protected void dispose(){
        System.out.println("Amphibian dispose");
        t.dispose();
        p.dispose();
        super.dispose();
    }

}


public class Frog extends Amphibian{
    private Characteristic p = new Characteristic("Croaks");
    private Description t = new Description("Eats Bugs");
    public Frog(){
        System.out.println("Frog()");
    }
    protected void dispose(){
        System.out.println("Frog dispose");
        t.dispose();
        p.dispose();
        super.dispose();
    }

    public static void main(String[] args) {
        Frog frog = new Frog();
        System.out.println("Bye!");
        frog.dispose();
    }
}

存在数组情况下的清理

class Shared{
    private int refcount = 0;
    private static long counter = 0;
    private final long id = counter++;
    public Shared(){
        System.out.println("Creating "+this);
    }
    public void addRef(){
        refcount++;
    }
    protected void dispose(){
        if(--refcount == 0)
            System.out.println("Disposing"+this);
    }
    public String toString(){ return "Shared" + id;}

}
class Composing{
    private Shared shared;
    private static long counter = 0;
    private final long id = counter++;
    public Composing(Shared shared){
        System.out.println("Creating"+this);
        this.shared = shared;
        this.shared.addRef();
    }
    protected void dispose(){
        System.out.println("disposing"+this);
        shared.dispose();
    }
    public String toString(){
        return "Composing"+id;
    }
}

public class ReferenceCounting {
    public static void main(String[] args) {
        Shared shared = new Shared();
        Composing[] composings = {
                new Composing(shared),
                new Composing(shared),
                new Composing(shared),
                new Composing(shared),
                new Composing(shared)
        };
        for(Composing c:composings)
            c.dispose();
    }
}


向下转型与运行时的类型识别

我们知道,对于向上转型来说,是安全的

但是对于向下转型来说,却不一定是安全的,比如我们知道一个“几何形状”,但是我们无法确定这是个圆还是三角形还是其他类型

所以需要引入检查机制“运行时类型识别”(RTTI)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值