Why did the creators of Java use the name "class" for the classes in Java? Where does this term come from?
问题:
回答1:
Java didn't invent the name class - it was used in languages prior, like C++.
I think the name "class" refers to a class of objects, as in a classification (or a type). And then an object is an instance of that type.
Here is the first definition of "class" from dictionary.com:
- a number of persons or things regarded as forming a group by reason of common attributes, characteristics, qualities, or traits; kind; sort.
So this is right in line with what we know to be a class in computer science - the "characteristics/qualities/traits" being fields and methods.
回答2:
Blame Ole-Johan Dahl and Kristen Nygaard, who apparently originated the concept for Simula; and also Tony Hoare, who gave them the inspiration.
According to the history of Simula:
As this pursuit proceeded throughout the summer and autumn of 1966, they became more and more preoccupied with the opportunities embedded in Tony Hoare's record class construct, first presented in ALGOL bulletin no. 21, 1965. After having carefully examined Hoare's record proposal they eventually came to the conclusion that, even though it obviously had a number of very useful properties, it failed to fully meet their requirements. What they were really looking for was some kind of generalized process concept with record class properties.
The answer to their problem suddenly appeared in December 1966, when the idea of prefixing was introduced. A process, later called an object, could now be regarded as consisting of two layers: A prefix layer containing references to its predecessor and successor along with a number of other properties, and a main layer containing the attributes of the object in question. In addition to this important new feature, they also introduced the class concept, which can roughly be described as a highly refined version of SIMULA I's activity concept. This powerful new concept made it possible to establish class and subclass hierarchies of concatenated objects.
Of course, the idea of classifying objects far predates any programming language.
回答3:
I think the origin of the word "class" predates the realm of Computer Science, in fact. Taxonomy has been around for a long time, and I would venture to say its roots are somewhere in Philosophy or perhaps less abstractly in Biology. Programming Language folks just adopted an analogy from another field :).
回答4:
The class is the definition, the metadata; the object is the instance. This is normal OOP language use.
回答5:
Because they are used to "classify" objects of the same nature.
Employee objects are different than Account objects.
All these concepts are the base of Object Oriented Technology. Java happens to be an Object Oriented programming, That's why they use all these terms in first place.
回答6:
Note that the Class class in Java is brought in by the reflection classes, not by sheer accident.
Here there is both Class and Method objects to allow you to do stuff like:
Class cls = java.lang.String.class;
// By obtaining a list of all declared methods.
Method[] methods = cls.getDeclaredMethods();
// By obtaining a list of all public methods, both declared and inherited.
methods = cls.getMethods();
for (int i=0; i<methods.length; i++) {
Class returnType = methods[i].getReturnType();
Class[] paramTypes = methods[i].getParameterTypes();
process(methods[i]);
}
// By obtaining a particular Method object.
// This example retrieves String.substring(int).
try {
Method method = cls.getMethod("substring", new Class[] {int.class});
process(method);
} catch (NoSuchMethodException e) {
}
http://www.exampledepot.com/egs/java.lang.reflect/Methods.html
In other words, invoke code determined at compile time. Very handy for invoking optional functionality.