- 浏览: 748153 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (419)
- 杂软粉墨 (2)
- 创意灵感 (3)
- 经验记录 (137)
- 开源轨迹 (2)
- sip-communicator (2)
- 闲侃杂谈 (8)
- 问题交流 (24)
- 概念模式 (32)
- 难点备案 (5)
- JwChat (1)
- 中国象棋 (1)
- 教育探索 (6)
- 英语研究 (58)
- 星际争霸 (1)
- 电信知识 (1)
- 软件架构 (3)
- 哲学探索 (26)
- 算法灵魂 (8)
- 近视探索 (6)
- 数学数学 (3)
- 牛角钻尖 (23)
- 至强文言 (3)
- 数据结构 (1)
- 宇宙物理 (2)
- 网络架构 (3)
- 游戏领域 (4)
- 图形处理 (2)
- 修炼之路 (8)
- 读书天地 (20)
- 编解乱码 (2)
- 概念探索 (8)
- 格物致知 (1)
- 其它语言 (1)
- 测试领域 (3)
- 文化风流 (1)
- JQuery (1)
- 網頁領域 (1)
- Unix/Linux (1)
- Inside JVM (1)
- 异常分析 (1)
最新评论
-
suyujie:
引用
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
iamzhoug37:
您能说一下"局部变量不受文本顺序限制" 是 ...
声明前为什么能赋值却不能输出,都是使用
所谓RTTI,是Runtime Type Information
//: typeinfo/Shapes.java import java.util.*; abstract class Shape { void draw() { System.out.println(this + ".draw()"); } abstract public String toString(); } class Circle extends Shape { public String toString() { return "Circle"; } } class Square extends Shape { public String toString() { return "Square"; } } class Triangle extends Shape { public String toString() { return "Triangle"; } } public class Shapes { public static void main(String[] args) { List<Shape> shapeList = Arrays.asList(new Circle(), new Square(), new Triangle()); for (Shape shape : shapeList) shape.draw(); } }/* * Output: Circle.draw() Square.draw() Triangle.draw() */// :~
看以上类的设计,将toString设为abstract,是为了强制继承者覆盖该方法,同时也达到了阻止单纯Shape类的实例化的目的
The base class contains a draw( ) method that indirectly uses toString( ) to print an identifier for the class by passing this to System.out.println( ) (notice that toString( ) is declared abstract to force inheritors to override it, and to prevent the instantiation of a plain Shape). If an object appears in a string concatenation expression (involving ‘+’ and String objects), the toString( ) method is automatically called to produce a String representation for that object. Each of the derived classes overrides the toString( ) method (from Object) so that draw( ) ends up (polymorphically) printing something different in each case.
In this example, the upcast occurs when the shape is placed into the List<Shape>. During the upcast to Shape, the fact that the objects are specific types of Shape is lost. To the array, they are just Shapes.
At the point that you fetch an element out of the array, the container—which is actually holding everything as an Object—automatically casts the result back to a Shape. This is the most basic form of RTTI, because all casts are checked at run time for correctness. That’s what RTTI means: At run time, the type of an object is identified.
In this case, the RTTI cast is only partial: The Object is cast to a Shape, and not all the way to a Circle, Square, or Triangle. That’s because the only thing you know at this point is that the List<Shape> is full of Shapes. At compile time, this is enforced by the container and the Java generic system, but at run time the cast ensures it.
Now polymorphism takes over and the exact code that’s executed for the Shape is determined by whether the reference is for a Circle, Square, or Triangle. And in general, this is how it should be; you want the bulk of your code to know as little as possible about specific types of objects, and to just deal with the general representation of a family of objects (in this case, Shape). As a result, your code will be easier to write, read, and maintain, and your designs will be easier to implement, understand, and change. So polymorphism is a general goal in object-oriented programming.
But what if you have a special programming problem that’s easiest to solve if you know the exact type of a generic reference? For example, suppose you want to allow your users to highlight all the shapes of any particular type by turning them a special color. This way, they can find all the triangles on the screen by highlighting them. Or perhaps your method needs to "rotate" a list of shapes, but it makes no sense to rotate a circle so you’d like to skip the circles. With RTTI, you can ask a Shape reference the exact type that it’s referring to, and thus select and isolate special cases.
发表评论
-
java语言中classes分类
2012-10-22 13:09 1630local class的scope是local,所以在方法 ... -
URL definition
2012-08-14 08:29 7285http://reg.163.com/login.jsp?ty ... -
how to defend against serialization and reflection attack for eager singleton
2012-08-08 09:18 1301//为什么要定义构造方法 因为不定义它也会有默认构造方法,而 ... -
Finalizer Guardian Idiom
2012-05-30 11:55 1315Effective Java 2nd 中Item 7: A ... -
我對多態的理解
2012-05-24 09:03 1074多态(polymorphism)是以一种看待事物更细致的角度来 ... -
Bridge Pattern
2011-06-17 11:59 847Intent Decouple an abstracti ... -
Composite Pattern
2011-06-17 09:12 970http://userpages.umbc.edu/~ta ... -
Observer Pattern
2011-06-15 11:32 1093consistent communication model ... -
Factory Method
2011-05-27 23:46 1012how does the factory method pat ... -
Law of Demeter
2011-04-07 09:23 1064http://en.wikipedia.org/wiki/La ... -
Several thread-safe singleton collection
2011-02-24 10:19 10401. static inner class publi ... -
关于Arrays.sort()方法用到的设计模式
2011-02-18 09:12 2889/** * Src is the sour ... -
何时使用LSP
2011-01-24 09:12 1016当使用subclass无法满足LSP(Liskov Subst ... -
Command模式图详解
2011-01-12 16:53 972上图是command模式结构图,各个符号重点应该理解,符 ... -
IOC inversion体现在什么地方
2011-01-12 16:02 1070http://martinfowler.com/bliki/I ... -
什么时候需要synchronized
2010-12-02 15:32 1171当需要在某个方法上加synchronized关 ... -
多任务同时执行完毕后方执行后续任务
2010-12-02 10:20 1228多线程同时启动,执行完毕后启动新任务 1.传统join ... -
Apache Struts2 Architecture
2010-07-21 22:38 986Struts is a flexible control la ... -
开闭原则
2010-03-15 13:58 972Closed for Modification; Op ... -
26 Hints for Agile Software Development
2009-11-23 16:14 101626 Hints for Agile Software Dev ...
相关推荐
### 实验七:运行中的多态与虚函数 #### 实验背景 在面向对象编程中,多态是一种非常重要的概念,它允许我们使用一个接口来表示多种类型的行为。多态通常分为编译时多态(静态多态)和运行时多态(动态多态)。...
多态,作为面向对象编程的三大特性之一,是继抽象和继承之后的关键概念。它允许我们以统一的接口处理不同类型的对象,增强了代码的灵活性和可扩展性。在Java中,多态主要通过两种方式体现:**向上转型**和**方法重写...
- RTTI允许程序在运行时获取对象的类型信息,这对于多态机制至关重要。 - 使用`typeid`操作符可以获取类型的`type_info`对象,进而判断对象的实际类型。 #### 三、Java中的多态实现 Java中的多态也是通过虚方法...
2. **重写与覆盖**:通过`virtual`和`override`关键字来实现方法的重写,这是C#中最常见的多态形式之一。 3. **抽象类与接口**:抽象类和接口定义了必须由子类实现的方法签名,这为多态提供了一种结构化的框架。 4. ...
运行时类型识别(Runtime Type Information,简称RTTI)是C++语言中的一种特性,它允许在程序运行...通过研究这些内容,开发者可以提升在复杂系统设计中利用多态和类型信息的能力,从而编写出更加健壮和灵活的代码。
1. `dynamic_cast`:这是RTTI的核心操作之一,用于在运行时进行安全类型转换。例如,从基类指针或引用转换为派生类指针或引用,或者从指向void指针的转换。如果转换不成功,`dynamic_cast`会返回空指针(对于指针)...
运行时类型信息(Runtime Type Information,简称RTTI)是...学习RTTI有助于深入理解C++的多态机制,提高代码的灵活性和可维护性。但需要注意,过度依赖RTTI可能导致代码过于复杂,降低性能,因此在设计时应谨慎使用。
RTTI的一个常见用途是在多态基类中定义虚函数`virtual void whatAmI() const`,然后在派生类中重写它,利用`typeid(*this).name()`来输出当前对象的类型名称。这种方法在调试、日志记录或需要动态处理不同类型对象的...
在设计和编写多态的C++程序时,理解和利用RTTI以及相关的类型转换机制至关重要,它们可以提高代码的灵活性和安全性。正确使用这些工具可以帮助预防和解决运行时的类型错误,从而提升软件的健壮性和可靠性。然而,...
在C++中如果使用多态继承类,对于多态性的对象,在程序编译时可能会出现无法确定对象的类型的情况,这事就需要通过RTTI动态识别对象的类型。因此,本文探讨了RTTI的使用细节。同时,由于有些平台C++编译器(如...
本文将深入探讨如何通过反汇编和理解RTTI结构来获取多态类的继承链。 首先,让我们了解什么是RTTI。RTTI提供了一种在运行时查询对象类型的机制,这包括`typeid`操作符和`dynamic_cast`转换。在C++中,每个具有虚...
### C++ RTTI深度解析 ...通过这些介绍,我们可以更好地理解 C++ 如何在运行时确定对象类型,这对于开发支持多态的应用程序非常重要。此外,了解 RTTI 的底层实现也有助于优化代码性能和避免潜在的错误。
- **dynamic_cast运算符**:这是RTTI的主要使用场景之一,它可以在运行时安全地进行指针或引用的类型转换。例如,将基类指针转换为派生类指针,如果转换不成功,会返回空指针或者抛出`bad_cast`异常。 - **typeid...
《深入理解MFC的RTTI:Visual C++实践解析》 在C++编程中,运行时类型信息(Run-Time Type Information,简称RTTI)是一种强大的工具,它允许程序员在程序运行时获取对象的类型信息。在MFC(Microsoft Foundation ...
虽然一些面向对象的设计者建议优先使用虚拟成员函数来实现多态行为,但在某些场景下,如处理异类容器或需要对对象进行特定类型的检查时,RTTI 提供了必要的灵活性。 RTTI 包含两个主要的运算符:`typeid` 和 `...
多态是面向对象编程的核心概念之一,允许通过父类引用操作子类对象。RTTI在这种场景下起到关键作用,特别是在需要根据对象的实际类型执行特定操作时。例如,当需要对Vector中的Shape对象进行特殊处理时,可以通过...
比如:模板技术,RTTI技术,虚函数技术,要么是试图做到在编译时决议,要么试图做到运行时决议。 对C++ 了解的人都应该知道虚函数(Virtual Function)是通过一张虚函数表(Virtual Table)和一个指向虚函数表的指针...
RTTI的一个重要应用场景是在多态编程中,特别是与虚函数一起使用时。例如,当你有一个指向基类的指针,但实际指向的是派生类的实例,`typeid`可以用来确定对象的实际类型,从而实现动态绑定和类型检查。不过,需要...