接口契约,类对象?
契约与接口的关系就像对象与类的关系一样吗?
从代码到执行代码,有什么必要区分像这样的相同事物? 我有点明白将类命名为类并将实例化的执行类命名为对象的想法,但总的来说,这是这些半冗余术语的唯一原因吗?
Is contract to interface as object is to class?
What is the need to differentiate identical things like this, from the code to the executing code? I sort of get the idea behind naming a class a class and the instantiated executing class an object, but overall, is that the only reason for these semi-redundant terms?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
并不真地。 这里有四个术语,所以我将逐一介绍:
接口
接口是一个抽象类(在像 Java 这样的语言中,没有多重继承,有时还有其他限制,例如单独的数据类型),即旨在用作访问许多行为相似的对象的公共基础。 从概念上讲,抽象性没有要求,但通常,一个接口至少有一个抽象方法。 接口是程序与许多类似的类进行通信的方法,每个类具有不同的语义,但通用目的相同。
契约 契约
是类或接口的用户和实现者之间达成的隐含协议。 例如,前置条件和后置条件(不变量通常是类实现中的契约 - 一般来说,内部成员之间的关系之类的事情不需要公开)。 返回值或参数的规范也可以是合约的一部分。 它基本上代表了如何使用函数/类/接口,并且通常不能完全用任何语言来表示(某些语言,如 Eiffel,允许您放入显式契约,但即使这些也不能总是完全充实需求)。 当您实现接口或从类派生时,您始终必须满足接口要求,或者在重写非抽象类时,表现得足够相似,以至于外部查看者不会注意到差异(这就是里氏法则)替换原则;派生对象应该能够替换基对象,而从外部角度看,其行为没有差异)。
A类
不需要太多复习,因为你以前显然已经使用过它们。 类是数据类型,在某些语言中是接口的超集(没有正式定义,如 C++),而在其他语言中是独立的(如 Java)。
对象
对象是类类型(或通常是任何非类类型)的实例。 对象的确切定义对于某种语言来说是非常特定的,但一般定义是由指向同一事物的多个引用/指针引用的实际事物 - 例如,在 Java 等某些语言中, == 比较两个变量是否是相同的对象,不一定它们在语义上是否相同。 对象独立于类或接口——它们代表单个实例。 另一种思考方式是,类或接口是模具,而对象是从模具中出来的物理对象(一个相当糟糕的类比,但这是我现在能想到的最好的类比)。
Not really. There are four terms here, so I'll go over each of them:
Interface
An interface is an abstract class (in languages like Java where there is no multiple inheritance, sometimes there are other restrictions, such as a separate data type) that is intended to be used as a common base to access a number of similarly-behaving objects. Conceptually, there is no requirement for abstractness, but usually, an interface will have at least one abstract method. An interface is a method for your program to communicate with a number of similar classes, each with different semantics but the same general purpose.
Contract
A contract is the implicit agreement you make between users and implementers of a class or interface. For instance, preconditions and postconditions (invariants are usually a contract within the class' implementation - generally, things like the relation between internal members don't need to be exposed). The specification for a return value or an argument can also be part of the contract. It basically represents how to use the function/class/interface, and isn't generally fully representable in any language (some languages, like Eiffel, allow you to put explicit contracts in, but even these can't always fully flesh out the requirements). When you implement an interface or derive from a class, you are always having to meet the interface requirements, or, when overriding a non-abstract class, to behave similar enough that an external viewer doesn't notice the difference (this is the Liskov Substitution Principle; a derived object should be capable of replacing the base with no difference in behavior from the outside perspective).
Class
A class doesn't need a lot of going over, since you clearly have used them before. A class is the data type, and in some languages is a superset of interfaces (which have no formal definition, as in C++), and in others is independent (such as in Java).
Object
An object is an instance of a class type (or of any non-class type, usually). The exact definition of an object is very specific to a language, but the general definition is the actual thing referred to by multiple references/pointers to the same thing - for instance, in some languages like Java, == compares whether two variables are the same object, not necessarily whether they are semantically the same. Objects are independent from classes or interfaces - they represent a single instance. Another way of thinking of it is that class or interface is the mold, and the object is the physical object that comes out of the mold (a rather bad analogy, but it's the best I can come up with right now).
不,不是真的。 类是您定义的模板。 实例化该类的每个对象都遵循模板。 它们并不是真正多余的术语,因为两者并不相同。 您可以将类视为用户定义的数据类型。 类和对象之间的区别就如同原始数据类型 int 与文字值的区别一样。 3.
接口定义了一组所有实现类都必须支持的方法。 接口本身是您为实现类定义的契约。 它只是说任何实现该接口的类都必须具有该接口的一组公共方法。
No, not really. A class is a template that you define. Each object that instantiates that class follows the template. They're not really redundant terms, because the two things are not identical. You can think of a class as a user-defined data type. Classes and objects are different from each other in the exact same way that the primitive data type
int
is different from the literal value 3.An interface defines a set of methods that all implementing classes must support. The interface itself is the contract that you define for the implementing classes. It just says that any class that implements the interface, must have that interface's set of public methods.
好吧,我猜......如果一个接口指定了一个契约,那么一个类指定了特定对象的一个(或多个)实例。
不过,术语不如应用重要。
Well I guess... if an interface specifies a contract than a class specifies an (or multiple) instance(s) of a particular object.
Terminology is less important than application though.
实际上,当一个对象是一个类的实例时,接口就是一个契约——它们是不同的东西,没有太多共同点。
接口只是为对象提供了一个外观,或者为调用者提供了保证,即使不知道它的实现,该对象也可以执行某些操作。
例如,您可以让两个类实现相同的接口/契约,但执行完全不同的操作(即使执行它们的含义可能相同)。
以IDisposable接口为例:每个对象都可以释放它使用的资源,但是它可以通过多种不同的方式来释放,它可以选择不释放任何东西。 这是对象的选择。
至少这是 .NET 中的 POV
Actually, an interface is a contract, when an object is an instance of a class - they are different things that don't have too much in common.
The interface just provides a facade for objects, or a warranty for the caller that the object can do some operation, even without knowing it's implementation.
For example, you can have two classes implementing the same interface/contract, but do totaly different things (even though the meaning of doing them may be the same).
Take the IDisposable interface for example: Each object can release the resources that it uses, but it can do it in many different ways, it can choose not to release anything. It's the object's choice.
At least this would be the POV in .NET
为了完成前面的答案,关于接口的一句话:
如果类不仅仅是一个对象的模板(因为它的全局特性独立于任何实例),那么接口也可以被描述为一个点视图
实现多个接口的类:
“观点”意味着您可以通过仅关注该接口定义的契约来使用对象。
正是在这个方面,接口是一个“抽象类”,就像“抽象”一样(它继承了类的某些特征,但忽略了其他一些特征)。 在java世界中,接口实际上遗漏了很多东西,因为它只能用于定义契约,而不能用于静态方法或函数。
To complete the previous answers, a word about interfaces:
If the class is more than a template for an object (because of its global characteristics independent of any instances), the interface can be also be described as a point of view
A class implementing several interface:
"Point of view" means you can using an object by focusing solely on the contract define by that interface.
It is in that aspect an interface is an "abstract class", as in an "abstraction" (something which takes after some of the characteristics of a class, but leave some others out). In java world, an interface leaves actually a lot out, since it can only be applied to define contract for instance, not for static methods or functions.
“类”和“对象”代表两个不同的事物; 它们是相关的,但它们所代表的东西是不同的,而且是非常强烈的。
描述这一点的最好方法是查看静态。 类可以具有静态成员,它们与该类的任何实例完全分开。 该类的对象可能会也可能不会使用这些静态成员; 但该类的对象实例与该类的任何静态使用完全分开(或者至少应该如此)。
或者想想单例模式。 将类对象的实例存储在静态类访问器中是一种常见的做法,并且显示了差异。 您引用类静态访问器来获取单例类的对象实例; 如果类静态成员没有要引用的对象实例,则类创建对象的实例 对象。
换一种方式; 对象是类的实例; 但类可以不仅仅是一个用于实例化对象的模板。 类的静态成员在内存中具有完全独立于这些类的对象实例的表示。
"Class" and "Object" represent two different things; they are related, but what they represent IS different, quite strongly.
The best way to describe this is to look at Static. A class can have static members, which are completely separate from any INSTANCE of that class. Objects of that class may or may not use those static members; but the instance of the object of that class is completely separate from any static uses of that class (or should be, at the very least).
Or think of the singleton pattern. Storing an instance of the class object in a static class accessor is a common practice, and shows the difference. You refer to the class static accessor to get the object instance of a singleton class; if the class static member does not have an object instance to refer to, the class creates the instance of the object.
Put another way; an object is an instance of a class; but a class can be more than just a template from which objects are instantiated. Static members of classes have a representation in memory that is completely independent of object instances of those classes.