Java - 什么是原型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18157064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - what is a a prototype?
提问by davidjhp
In a lecture on Java, a computer science professor states that Java interfaces of a class are prototypesfor public methods, plus descriptions of their behaviors.
在关于 Java 的讲座中,一位计算机科学教授指出,类的 Java 接口是公共方法的原型,以及对其行为的描述。
(Source https://www.youtube.com/watch?v=-c4I3gFYe3w@8:47)
(来源https://www.youtube.com/watch?v=-c4I3gFYe3w@8:47)
And at 8:13 in the video he says go to discussion section with teaching assistants to learn what he means by prototype.
在视频中的 8 点 13 分,他说与助教一起进入讨论部分,了解他所说的原型是什么意思。
What does "prototype" mean in Java in the above context?
在上述上下文中,“原型”在 Java 中是什么意思?
回答by sjas
"prototype" is not the the best/right terminus to be used. interfaces
are more like "contracts", that implementing classes have to fulfill.
“原型”不是要使用的最佳/正确的终点。interfaces
更像是“合同”,实现类必须履行。
The method's heads/definitions will have to be implemented in the implementing class (using implements
keyword in the class head/class definition/public class xy implements ...
).
方法的头部/定义必须在实现类中实现(implements
在类 head/class definition/ 中使用关键字public class xy implements ...
)。
I guess this naming conventions leave much room for many ideological debates.
我想这种命名约定为许多意识形态辩论留下了很大的空间。
Or the author had some sort of a mental lapsus and mapped the construct of prototypical inheritance from javascript into java in his mind somehow.
或者作者有某种心理上的失误,并以某种方式将 javascript 原型继承的构造映射到了他的脑海中。
回答by BeWarned
I think the use of the word prototype in this context is unfortunate, some languages like JavaScript use something called prototypical inheritance which is totally different than what is being discussed in the lecture. I think the word 'contract' would be more appropriate. A Java interface is a language feature that allows the author of a class to declare that any concrete implementations of that class will provide implementations of all methods declared in any interfaces they implement.
我认为在这种情况下使用原型这个词是不幸的,像 JavaScript 这样的一些语言使用了一种叫做原型继承的东西,这与讲座中讨论的完全不同。我认为“合同”一词更合适。Java 接口是一种语言特性,它允许类的作者声明该类的任何具体实现将提供在它们实现的任何接口中声明的所有方法的实现。
It is used to allow Java classes to form several is-a relationships without resorting to multiple inheritance (not allowed in Java). You could have a Car class the inherits from a Vehicle class but implements a Product interface, therefor the Car is both a Vehicle and a Product.
它用于允许 Java 类形成多个 is-a 关系,而无需诉诸多重继承(Java 中不允许)。您可以有一个 Car 类继承自 Vehicle 类但实现了 Product 接口,因此 Car 既是 Vehicle 又是 Product。
回答by Vishnuprasad R
Interfaces are not prototypes for classes in Java.
接口不是 Java 类的原型。
In languages like C & C++, which compiles to machine code sirectly, compiler should be aware of the nature of any identifier (variable/class/functions) before they are references anywhere in the program. That mean those languages require to know the nature of the identifier to generate a machine code output that is related to it.
在像 C 和 C++ 这样直接编译为机器代码的语言中,编译器应该在任何标识符(变量/类/函数)在程序中的任何位置被引用之前知道它们的性质。这意味着这些语言需要知道标识符的性质才能生成与之相关的机器代码输出。
In simple words, C++ compiler should be aware of methods and member of a class before that class is used anywhere in the code. To accomplish that, you should define the class before the code line where it is used, or you should at least declare its nature. Declaring only the nature of a function or a class creates a 'prototype'.
简而言之,C++ 编译器应该在代码中的任何地方使用该类之前知道该类的方法和成员。为此,您应该在使用它的代码行之前定义该类,或者至少应该声明它的性质。仅声明函数或类的性质会创建“原型”。
In Java, an 'interface' is something like description of a class. This defines what all methods a particular kind of class should mandatory have. You can then create classes that implements those interface. Main purpose that interfaces serve in java is the possibility that a Variable declared as of a particular interface type can hold objects of any class that implements the object.
在 Java 中,“接口”类似于对类的描述。这定义了特定类型的类应该具有的所有方法。然后,您可以创建实现这些接口的类。接口在 java 中服务的主要目的是声明为特定接口类型的变量可以保存实现该对象的任何类的对象的可能性。
回答by Onur A.
He tells it in C/C++
way, let me explain, in C++
you can define prototypes for methods at the header files of classes so that other classes can recognize these methods, also in C
where there is no class concept, you can define prototypes at the beginning of file and then at somewhere in same file you can implement these prototypes, so that methods can be used even before their implementation is provided. So in Java
interfaces provide pretty much same way, you can define prototypes for methods(method headers) that will be implemented by classes that implement this interface.
他说得很C/C++
有道理,我解释一下,在C++
你可以在类的头文件中定义方法的原型,以便其他类可以识别这些方法,同样在C
没有类概念的地方,你可以在文件的开头定义原型然后在同一个文件的某个地方你可以实现这些原型,这样即使在提供它们的实现之前也可以使用这些方法。因此,在Java
接口提供几乎相同的方式中,您可以定义将由实现此接口的类实现的方法(方法头)的原型。
回答by Stephen C
What does "prototype" mean in Java in the above context?
在上述上下文中,“原型”在 Java 中是什么意思?
The word "prototype" is not standard Java terminology. It is not used in the JLS, and it is not mentioned in the Java Tutorial Glossary. In short there is no Java specific meaning.
“原型”这个词不是标准的 Java 术语。JLS 中没有使用它,Java 教程词汇表中也没有提到它。总之没有Java特定的含义。
Your lecturer is using this word in a broader sense rather than a Java-specific sense. In fact, his usage matches "function prototype" as described in this Wikipedia page.
您的讲师是在更广泛的意义上使用这个词,而不是在特定于 Java 的意义上使用这个词。事实上,他的用法与此维基百科页面中描述的“函数原型”相匹配。
Unfortunately, the "IT English" language is full of examples where a word or phrase means different (and sometimes contradictory) things in different contexts. There are othermeanings for "template" that you will come across in IT. For instance:
不幸的是,“IT 英语”语言充满了这样的例子,在这些例子中,一个词或短语在不同的上下文中意味着不同的(有时是矛盾的)事物。您将在 IT 中遇到的“模板”还有其他含义。例如:
In C++ "template" refers to what Java calls a generic class or method.
In Javascript, an object has a "template" attribute that gives the objects methods.
More generally, template-based typing is an alternative (more dynamic) way of doing OO typing.
在 C++ 中,“模板”指的是 Java 所谓的泛型类或方法。
在 Javascript 中,一个对象有一个“模板”属性,它给出了对象的方法。
更一般地说,基于模板的类型是进行 OO 类型的另一种(更动态的)方式。
But the fact that these meanings exist does not mean that your lecturer was wrong to refer to interface method signatures as "templates".
但是这些含义存在的事实并不意味着您的讲师将接口方法签名称为“模板”是错误的。
回答by Stephen C
In a lecture on Java, a computer science professor states that:
在关于 Java 的讲座中,一位计算机科学教授指出:
Java interfaces of a classare:
1. are prototypes for public methods,
2. plus descriptions of their behaviors.
类的 Java接口是:
1. 是公共方法的原型,
2. 加上它们的行为描述。
For 1. Is ok: - yes, they are prototypes for implemented public methods of a class.
For 2. This part could be a little bit tricky. :)
对于 1. 可以: - 是的,它们是类的已实现公共方法的原型。
对于 2. 这部分可能有点棘手。:)
- why?
- we know: interface definition (contain prototypes), but doesn't define (describe) methods behavior.
- computer science professor states: "... plus descriptions of their behaviors.". This is correct only if we look inside class that implements that interface (interface implementation = prototype definitions or descriptions).
- 为什么?
- 我们知道:接口定义(包含原型),但不定义(描述)方法行为。
- 计算机科学教授说:“......加上对他们行为的描述。”。只有当我们查看实现该接口的类(接口实现 = 原型定义或描述)时,这才是正确的。
Yes, a little bit tricky to understand :)
是的,有点难以理解:)
Bibliography:
Definition vs Description
Context-dependent
Name visibility - C++ Tutorials
参考书目:
定义与描述
上下文相关的
名称可见性 - C++ 教程
ExtraWork:
加班:
Note: not tested, just thinking! :)
注意:未测试,只是思考!:)
C++:
C++:
// C++ namespace just with prototypes:
// could be used like interface similar with Java?
// hm, could we then define (describe) prototypes?
// could we then inherit namespace? :)
namespace anIntf{
void politeHello(char *msg);
void bigThankYou();
}
回答by libra
Prototypes provide the signatures of the functions you will use within your code. They are somewhat optional, if you can order your code such that you only use functions that are previously defined then you can get away without defining them
原型提供您将在代码中使用的函数的签名。它们在某种程度上是可选的,如果您可以对代码进行排序,以便仅使用先前定义的函数,那么您无需定义它们就可以逃脱
Below a prototype for a function that sums two integers is given.
下面给出了对两个整数求和的函数的原型。
int add(int a, int b);