"일등석"개체 란 무엇입니까?
주어진 프로그래밍 언어에서 객체 나 다른 것들이 언제 "일등"이라고 말합니까? 다른 언어와 다른 점은 무엇입니까?
편집하다. 파이썬에서와 같이 "모든 것이 객체"라고 말할 때 실제로 "모든 것이 일류"라는 의미입니까?
즉, 객체 사용에 제한이 없음을 의미합니다. 다른 객체와 동일합니다.
첫 번째 클래스 객체는 동적으로 생성, 소멸, 함수에 전달, 값으로 반환되고 프로그래밍 언어의 다른 변수와 같은 모든 권한을 가질 수있는 엔터티입니다.
언어에 따라 다음을 의미 할 수 있습니다.
- 익명의 리터럴 값으로 표현 가능
- 변수에 저장 가능
- 데이터 구조에 저장 가능
- 고유 한 정체성을 가진 사람 (명칭에 관계없이)
- 다른 단체와의 평등에 필적하는 것
- 프로 시저 / 함수에 매개 변수로 전달 가능
- 절차 / 기능의 결과로 반환 가능
- 런타임에 구성 가능
- 인쇄 가능
- 읽을 수있는
- 분산 프로세스간에 전송 가능
- 실행중인 프로세스 외부에 저장 가능
소스 .
그러나 C ++ 함수 자체는 일류 객체가 아닙니다.
- '()'연산자를 재정 의하여 첫 번째 클래스 인 객체 함수를 사용할 수 있습니다.
- 함수 포인터는 일류입니다.
- 부스트 바인드, 람다 및 함수는 일급 함수를 제공합니다.
C ++에서 클래스는 첫 번째 클래스 객체가 아니라 해당 클래스의 인스턴스입니다. 파이썬에서 클래스 와 객체는 모두 일류 객체입니다. (참조 이 대답 객체로 클래스에 대한 자세한 내용을).
다음은 Javascript 일급 함수의 예입니다.
// f: function that takes a number and returns a number
// deltaX: small positive number
// returns a function that is an approximate derivative of f
function makeDerivative( f, deltaX )
{
var deriv = function(x)
{
return ( f(x + deltaX) - f(x) )/ deltaX;
}
return deriv;
}
var cos = makeDerivative( Math.sin, 0.000001);
// cos(0) ~> 1
// cos(pi/2) ~> 0
소스 .
1 급 객체가 아닌 엔티티를 2 급 객체라고합니다. C ++의 함수는 동적으로 만들 수 없기 때문에 두 번째 클래스입니다.
편집 관련 :
편집하다. 파이썬에서와 같이 "모든 것이 객체"라고 말할 때 실제로 "모든 것이 일류"라는 의미입니까?
객체라는 용어는 느슨하게 사용할 수 있으며 일등석을 의미하지는 않습니다. 그리고 전체 개념을 '일등 실체'라고 부르는 것이 더 합리적 일 것입니다. 그러나 파이썬에서는 모든 것을 일등으로 만드는 것을 목표로합니다. 나는 당신의 진술을 한 사람의 의도가 일등을 의미한다고 생각합니다.
“일등석”은 평소와 같이 업무를 수행 할 수 있음을 의미합니다. 대부분의 경우 이는 단지 일류 시민을 함수의 인수로 전달하거나 함수에서 반환 할 수 있음을 의미합니다.
이것은 객체에 대해 자명하지만 기능이나 클래스에 대해 항상 분명하지는 않습니다.
void f(int n) { return n * 2; }
void g(Action<int> a, int n) { return a(n); }
// Now call g and pass f:
g(f, 10); // = 20
This is an example in C# where functions actually aren't first-class objects. The above code therefore uses a small workaround (namely a generic delegate called Action<>
) to pass a function as an argument. Other languages, such as Ruby, allow treating even classes and code blocks as normal variables (or in the case of Ruby, constants).
From a slide in Structure and Interpretation of Computer Programs, lecture 2A (1986), which in turns quotes Christopher Stracey:
The rights and privileges of first-class citizens:
- To be named by variables.
- To be passed as arguments to procedures.
- To be returned as values of procedures.
- To be incorporated into data structures
"When one says "everything is an object" (like in Python), does he indeed mean that "everything is first-class"?"
Yes.
Everything in Python is a proper object. Even things that are "primitive types" in other languages.
You find that an object like 2
actually has a fairly rich and sophisticated interface.
>>> dir(2)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']
Because everything's a first-class object in Python, there are relatively few obscure special cases.
In Java, for example, there are primitive types (int, bool, double, char) that aren't proper objects. That's why Java has to introduce Integer, Boolean, Double, and Character as first-class types. This can be hard to teach to beginners -- it isn't obvious why both a primitive type and a class have to exist side-by-side.
It also means that an object's class is -- itself -- an object. This is different from C++, where the classes don't always have a distinct existence at run-time.
The type of 2
is the type 'int'
object, which has methods, attributes, and a type.
>>> type(2)
<class 'int'>
The type of a built-in type like int
is the type 'type'
object. This has methods and attributes, also.
>>> type(type(2))
<class 'type'>
IMO this is one of those metaphors used to describe things in a natural language. The term is essentially used in context of describing functions as first class objects.
If you consider a object oriented language, we can impart various features to objects for eg: inheritance, class definition, ability to pass to other sections of code(method arguments), ability to store in a data structure etc. If we can do the same with an entity which is not normally considered as a object, like functions in the case of java script, such entities are considered to be first class objects.
First class essentially here means, not handled as second class (with degraded behaviour). Essentially the mocking is perfect or indistinguishable.
참고URL : https://stackoverflow.com/questions/245192/what-are-first-class-objects
'Programming' 카테고리의 다른 글
서브 클립 스 svn : ignore (0) | 2020.05.24 |
---|---|
Ruby on Rails에서 setter 메소드를 재정의하는 올바른 방법은 무엇입니까? (0) | 2020.05.24 |
Eclipse 용 Bash 스크립트 플러그인? (0) | 2020.05.23 |
Java에서 컴퓨터의 CPU, 메모리 및 디스크 사용량을 어떻게 모니터링합니까? (0) | 2020.05.23 |
Internet Explorer의 입력 자리 표시 자 (0) | 2020.05.23 |