一 RTTI概念 认识Claa对象之前,先来了解一个概念,RTTI(Run-Time Type Identification)运行时类型识别,对于这个词一直是 C++ 中的概念,至于Java中出现RTTI的说法则是源于<Thinking in Java>一书,其作用是在运行时识别一个对象的类型和类的信息,这里分两种: 传统的”RTTI”:它假定我们在编译期已知道了所有类型(在没有反射机制创建和使用类对象时,一般都是编译期已确定其类型,如new对象时该类必须已定义好): 反射机制,它允许我们在运…
除了 new 之外的创建对象的方法 通过 new 创建对象,会使得程序面向实现编程,先举个例子,某个果园里现在有两种水果,一种是苹果,一种是香蕉,有客户想采摘园子里的水果,要求用get()方法表示即可 一般情况下,最直接的写法为: public class Apple { public void get() { System.out.println("得到苹果"); } } public class Banana { public void get() { System.out.p…
工厂模式属于创建型模式,它提供了一种创建对象的最佳方式. 它的特点是:客户端的程序类不直接牵扯到对象的实例化管理,只与接口发生关联,通过工厂类获取指定接口的实例化对象. 简单工厂模式如下: interface Car { public void Name(); } class Audi implements Car { public void Name() { System.out.println("Audi"); } } class Factory{ private Factory(…
一.先演示 “简单工厂”: package org; interface Fruit { public void eat(); } class Apple implements Fruit { public void eat() { System.out.println("吃苹果."); } } class Orange implements Fruit { public void eat() { System.out.println("吃橘子"); } } cla…