C# 类与接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2271104/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:55:35  来源:igfitidea点击:

Class vs. Interface

c#javaoop

提问by hartanto

I have a quite basic question:

我有一个非常基本的问题:

When should we decide to use Interface or Class for a specific class?

我们什么时候应该决定对特定类使用接口还是类?

For example: says we have 2 classes, Customer and Doctor.

例如:说我们有 2 个类,Customer 和 Doctor。

In Inheritance (class): we could set these 2 classes to inherit from parent class Person.

在继承(类)中:我们可以将这 2 个类设置为从父类 Person 继承。

Couldn't we do the same with Interface? Says we have InterfacePerson and have both Customer and Doctor implement the interface?

我们不能对接口做同样的事情吗?说我们有 InterfacePerson 并且 Customer 和 Doctor 都实现了这个接口?

Thus, this lead to: when do we decide to use one over the other and vice versa?

因此,这导致:我们什么时候决定使用一种而不是另一种,反之亦然?

回答by Adriaan Stander

Interfaces are used to enforce certain Methods/Properties. In a nutshell- an interface is a set of rules.

接口用于强制执行某些方法/属性。简而言之 - 界面是一组规则。

A class can be used to inherit/override base functionality.

类可用于继承/覆盖基本功能。

Have a look at

看一下

回答by Fredrik

Simply put, you use classes when there is code/implementation involved and interfaces when it is just interface descriptions. When referring to objects in your code, prefer to refer to the interfaces as it makes it easier to replace the actual implementation (or add more and different implementations).

简而言之,当涉及代码/实现时使用类,而当它只是接口描述时使用接口。在代码中引用对象时,更喜欢引用接口,因为这样可以更轻松地替换实际实现(或添加更多不同的实现)。

And yes, both Docor and Customer/Patient are likely to implement or extend Person.

是的,Docor 和 Customer/Patient 都可能实现或扩展 Person。

回答by Bozho

  • Read the wikipedia article

  • Read a book, then read the chapters about OOP again

  • In your example, Personshould be a class, because it contains implementation detailsthat are common to both a Doctorand a Customer.

  • interfaces don't have (and don't need) implementation details - they only denote whatobjects which implement them are doing. Not how. Why is this useful? Because when you are using the object you don't care howit's going to do its job.

  • 阅读维基百科文章

  • 读一本书,然后再读一遍关于OOP的章节

  • 在您的示例中,Person应该是一个类,因为它包含a和 a共有的实现细节DoctorCustomer

  • 接口没有(也不需要)的实施细则-他们只分别表示什么对象,它们实现他们在做什么。不怎么。为什么这很有用?因为当您使用对象时,您并不关心它将如何工作。

Let's take a look at a simple example - there is an interfaces Comparable(in Java at least). It denotesthat its implementors can be compared with each other. So you can have two classes:

让我们看一个简单的例子 - 有一个接口Comparable(至少在 Java 中)。它表示它的实现者可以相互比较。所以你可以有两个类:

class Doctor implements Comparable {..}

class Customer implements Comparable {..}

Now you can have a commonmethod which takes any set of objectswhich implement Comparableand call comparable1.compareTo(comparable2), because you knowthey can perform comparison - it's denoted by their interface.

现在您可以拥有一个通用方法,该方法接受任何实现Comparable和调用的对象集comparable1.compareTo(comparable2),因为您知道它们可以执行比较 - 它由它们的 interface 表示

回答by Amit

In C#, multiple inheritance can be achived through Interface only. Based on u r business requirement if there is a need that your class needs multiple inheritance going fwd, then use Interface else use class.

在 C# 中,只能通过接口实现多重继承。根据您的业务需求,如果您的类需要多重继承,则使用接口,否则使用类。

Also all members of Interfaces should be given defination in class i.e. interface members are must-implemented members.

此外,接口的所有成员都应在类中进行定义,即接口成员是必须实现的成员。

回答by Hyman

class suggests that object that inherits base class is some kind of this class

class 表明继承基类的对象是此类的某种

if you use interface it only shows that your class have some common behaviour that interface describes

如果您使用接口,它只会表明您的类具有接口描述的一些常见行为

回答by GuruKulki

Parent Class is the one which will have bare minimum properties common to all of its sub classes.

父类是具有所有子类共有的最少属性的类。

But Interface is a contract which tells its implantations to provide if it is not an abstract class.

但是接口是一个契约,它告诉它的植入物如果它不是一个抽象类。

And the One important difference between a class and interface is that

类和接口之间的一个重要区别是

class inheritance will give relation between two common subclasses.

类继承将给出两个公共子类之间的关系。

Where as Interface implementation gives a relation between two uncommon classes.

其中接口实现给出了两个不常见类之间的关系。

回答by CheesePls

First thing to remember, Classes can be instantiated, Interfaces cannot.

首先要记住,类可以实例化,接口不能。

Secondly, a Class can only extend ONE Class. Interfaces are not limited by this and so we can have multiple inheritance like so

其次,一个类只能扩展一个类。接口不受此限制,因此我们可以像这样具有多重继承

public class foo extends Person implements Man, Mammal

foo is a Person. It is also a Man and a Mammal;

foo 是一个人。它也是一个人和一个哺乳动物;

The only issue is Interfaces cannot have variables or method implementations where as a class(or abstract class for that matter) can.

唯一的问题是接口不能有变量或方法实现,而类(或抽象类)可以。

Generally I would say stick with interfaces and avoid abstract classes if you can.

一般来说,我会说坚持使用接口并尽可能避免使用抽象类。

回答by deleted

Think of the interface as a contract. The class may commit to the contract (implement the interface)

将接口视为合同。类可以承诺契约(实现接口)

Suppose you have class Person with subclasses Doctor and Patient. Then you'd have interface Treatable with the method getSymptoms() implemented by Patient and interface Treating with method cure(Treatable) implemented by Doctor. Most likely cure(Treatable) would call getSymptoms() at some point ...

假设您有类 Person 和子类 Doctor 和 Patient。然后,您将拥有带有由 Patient 实现的 getSymptoms() 方法的 Treatable 接口和由 Doctor 实现的带有方法 curure(Treatable) 的 Treating 接口。最有可能的治愈(可治疗)会在某个时候调用 getSymptoms() ......

回答by marklai

  • Interface - describe the behavior
  • Class - do the behavior
  • 接口 - 描述行为
  • 类 - 做行为

A class extending another class inherits the behavior. On the other hand, implementing an interface just says it need to behave that way but the class still has to know the how-to.

扩展另一个类的类继承了该行为。另一方面,实现一个接口只是说它需要以这种方式行事,但类仍然必须知道如何操作。

Besides single inheritance limitations, code using interfaces are easier to refactor and test, e.g. provide a mock implementation for a database access object in unit tests.

除了单一继承限制之外,使用接口的代码更容易重构和测试,例如在单元测试中为数据库访问对象提供模拟实现。

So, the real answer is, it depends on your design.

所以,真正的答案是,这取决于你的设计。

It may be that you use interfaces to describe behavior, and abstract parent classes to implement the behavior that can be inherited by subclasses. Or maybe the subclasses are so different that each implements the interface in their own way.

可能是你用接口来描述行为,抽象父类来实现子类可以继承的行为。或者也许子类是如此不同,以至于每个子类都以自己的方式实现了接口。

回答by aryeh

In object modeling, one shouldn't use inheritance for people and their roles. One should instead model them with two associated objects. That is, using composition instead of inheritance.

在对象建模中,不应该对人和他们的角色使用继承。人们应该用两个相关联的对象对它们进行建模。也就是说,使用组合而不是继承。

So in a way, of three alternatives, your discussing the two wrong ones: inheritance and interfaces for modeling parties and roles.

因此,在某种程度上,在三种替代方案中,您讨论了两种错误的方案:继承和用于建模各方和角色的接口。

A class is a template for a set of objects. Those objects have behavior, (and typically) state and characteristics. In regards to behavior, each object has an (implicit) interfacealready: the set of methods that can be externally called on it.

类是一组对象的模板。这些对象具有行为、(通常)状态和特征。在问候行为,每个对象具有一个(隐含的)接口:所述一组,可以被外部调用它的方法。

The question then becomes, why should you create a namedinterface, a subset of the interface already provided by an object?

那么问题就变成了,为什么要创建一个命名接口,一个对象已经提供的接口的子集?

  1. One wants to represent a subsetof behavior so different classesof objects can be treated polymorphically.
  2. One wants to constrain the possible behavior of an object exposed to another, to a subset of its (implicit) interface because the object is acting in a different context.
  1. 人们想要表示行为的一个子集,以便可以多态地处理不同类别的对象。
  2. 人们希望将暴露给另一个对象的可能行为限制在其(隐式)接口的子集,因为该对象在不同的​​上下文中运行。