For faster navigation, this Iframe is preloading the Wikiwand page for 内省 (电脑科学).

内省 (电脑科学)

电脑科学中,内省是指电脑程序运行时Runtime)检查对象Object)类型的一种能力,通常也可以称作“运行时类型检查"。一些编程语言C++JavaRubyPHPObjective-CPerl等等具有这种特性。

不应该将内省和反射混淆。相对于内省,反射更进一步,是指电脑程序运行时Runtime)可以访问、检测和修改它本身状态或行为的一种能力。一些编程语言比如Java具有反射特性,而C++不具有反射特性只具有内省特性。

例子

[编辑]

C++

[编辑]

C++通过执行期类型消息(RTTI)的typeiddynamic_cast关键字支持类型内省。dynamic_cast的例子:

Person* p = dynamic_cast<Person *>(obj);
if (p != nullptr) {
  p->walk();
}

typeid的例子:

if (typeid(Person) == typeid(*obj)) {
  serialize_person( obj );
}

Java

[编辑]

Java中最简单的类型自省的例子是instanceof算符[1]。例如:

if (obj instanceof Person) {
    Person p = (Person)obj;
    p.walk();
}

java.lang.Class类是更高级自省的基础[2]。例如:

System.out.println(obj.getClass().getName());

Python

[编辑]

Python中最常用的自省的方法是dir函数,它详细的列出一个对象的特性。例如:

>>> class Foo:
...     def __init__(self, val):
...         self.x = val
... 
>>>     def bar(self):
...         return self.x
...
>>> dir(Foo(5))
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__str__', '__weakref__', 'bar', 'x']

还可以使用内建函数typeisinstance来确定一个对象是什么,用hasattr确定一个对象做什么。例如:

>>> a = Foo(10)
>>> b = Bar(11)
>>> type(a)
<type 'Foo'>
>>> isinstance(a, Foo)
True
>>> isinstance(a, type(a))
True
>>> isinstance(a, type(b))
False
>>> hasattr(a, 'bar')
True

在Python 2而非Python 3中,声明class Foo而非class Foo(object)将导致type返回通用的instance类型而非这个类[3]

参见

[编辑]

引用

[编辑]
  1. ^ Java Language Specification: instanceof. [2020-09-27]. (原始内容存档于2012-02-13). 
  2. ^ Java API: java.lang.Class. [2020-09-27]. (原始内容存档于2009-09-11). 
  3. ^ Unifying types and classes in Python 2.2. [2020-09-27]. (原始内容存档于2020-11-30). 

外部链接

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
内省 (电脑科学)
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?