是使用面向对象语言编写的对象吗? [英] Is class an object in object oriented language

查看:54
本文介绍了是使用面向对象语言编写的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用面向对象的语言对对象进行分类吗?如何仅通过 class.method name 的名称访问Class方法?(内部工作).这和 object.method 一样吗?

Is class an object in object oriented language? How are Class methods accessed by just name of the class.method name? (internal working). Is this same as a object.method?

如果类与对象相同(属于对象类,对象类是OO中所有事物的超类),我们将其实例化(使其成为对象),我们可以使该类的实例成为实例吗?除了对象类.

And If the Class is same as object (belong to object class which is super class of every thing in OO) and we instantiate it (make object of it), can we make instance of an instance of an class other than Object class.

(即使实际上几乎不需要,也对理论观点感兴趣)

(Mainly interested in the theoretical perspective even if practically not required ever)

推荐答案

好吧,这取决于您使用的语言.在一切都是对象的纯OO语言中(例如 Smalltalk ),类也不例外,并且也是对象.在其他不将班级视为头等公民的语言中,它们只是特殊的语言构造或原始类型.从现在开始,由于它的反射支持和同质风格,我将使用Smalltalk作为目标语言.

Well, it depends on the language that you are using; in pure OO languages where everything is an object (e.g. Smalltalk) classes are no exception and are objects too. In other languages where classes are not considered as first class citizens they are just special language constructs or primitive types. From now on I'll use Smalltalk as a target language, due to its reflection support and homogeneous style.

如何仅通过class.method名称访问Class方法?(内部工作).这和object.method一样吗?

How Class methods are accessed by just name of the class.method name? (internal working). Is this same as as object.method?

由于类是对象,因此它们又是类的实例(元类).因此,向类发送消息只是向对象的消息发送消息,该对象的作用是表示类的行为.有很多文献,您可以在此处查看和此处进行一些介绍.

Since classes are objects, they are in turn instances of a class (a metaclass). Thus, sending a message to the class is just sending a message to an object whose role is to represent how classes behave. There is a lot of literature out there, you can take a look for example here and here for some introduction.

如果Class与object相同(属于object class,则为OO中所有事物的超类),我们实例化它(使对象),我们是否可以使一个类的实例的实例不同于对象类.

And If the Class is same as object (belong to object class which is super class of every thing in OO) and we instantiate it (make object of it), can we make instance of an instance of an class other than Object class.

我不确定是否在这里关注您,但是只是为了说明问题,并非总是 Object 是所有类的超类.问题是,如果您开始关注类与元类之间的关系,则可能会遇到无限循环.不同的语言以不同的方式解决此问题,例如,在VisualWorks Smalltalk中, Object nil 的子类.问题是 nil 也是一个对象(请记住,所有内容都是一个对象),它实际上表示无".如您所料, nil 是一个类的实例( UndefinedObject ),它还实现了一些类协议.结果,它可以用来表示没有继承任何东西的类形式:).

I'm not sure I follow you here, but just for clarification it is not always the case that Object is the superclass of all the classes. The thing is that If you start following the relationships between classes and metaclasses, you may reach a sort of infinite loop. Different languages work this out in different ways and for example, in VisualWorks Smalltalk, Object is a subclass of nil. The thing is that nil is also an object (remember, everything is an object) and it actually represents "nothing". As you may expect, nil is an instance of a class (UndefinedObject) and it also implements some of the class protocol. As a result it can be used to represent a class form where nothing is inherited :).

最后,我不知道这是否能回答您的问题,但是是的,您可以使用完全的反射功能来完成许多很酷的事情,例如动态创建新类或重塑现有类.我将在此处给您留下一些您可能会对该主题感兴趣的文档:

Finally, I don't know if this answers your question, but yes, you can do many cool things with full reflective capabilities, like creating new classes on the fly or reshaping existing ones. I'll leave you here some documents that you may find interesting regarding this topic:

这篇关于是使用面向对象语言编写的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆