将类作为第一类对象 [英] Treating Classes as First Class Objects

查看:123
本文介绍了将类作为第一类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读GoF书,在原型部分的开头我准备好了:

I was reading the GoF book and in the beginning of the prototype section I ready this:


这个好处主要适用于
语言,如C ++,不将
类当作第一类对象。

This benefit applies primarily to languages like C++ that don't treat classes as first class objects.

我从来没有使用C ++,我对OO编程有很好的理解,但是,这对我来说没有任何意义。任何人都可以阐述这一点(我已经使用了\use:C,Python,Java,SQL如果这有帮助。)

I've never used C++ but I do have a pretty good understanding of OO programming, yet, this doesn't really make any sense to me. Can anyone out there elaborate on this (I have used\use: C, Python, Java, SQL if that helps.)

推荐答案

对于要作为第一类对象的类,语言需要支持执行以下操作:允许函数将类(而不是实例)作为参数,能够在容器中保存类,以及能够从函数返回类。

For a class to be a first class object, the language needs to support doing things like allowing functions to take classes (not instances) as parameters, be able to hold classes in containers, and be able to return classes from functions.

有关使用第一类类的语言示例,请考虑Java。任何对象都是其类的实例。该类本身就是 java.lang.Class 的一个实例。

For an example of a language with first class classes, consider Java. Any object is an instance of its class. That class is itself an instance of java.lang.Class.

这篇关于将类作为第一类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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