Interfaces的概念来自哪里?

时间:2021-10-20 08:46:33

In c#, we have interfaces. Where did these come from? They didn't exist in c++.

在c#中,我们有接口。这些来自哪里?它们在c ++中不存在。

18 个解决方案

#1


10  

Interfaces are pretty old, and have been around for quite a while.

接口很老,已经存在了很长一段时间。

Early (mid to late late 1970's) non-object oriented languages such as Modula and Euclid used constructs called "modules" to specify the interfaces between components. Components would then communicate with each other via explicit importing and exporting modules. Interfaces in C# are object oriented evolutions of that same concept.

早期(1970年代中后期),非面向对象语言(如Modula和Euclid)使用称为“模块”的构造来指定组件之间的接口。然后,组件将通过显式导入和导出模块相互通信。 C#中的接口是同一概念的面向对象的演进。

Interfaces in C# directly extend from the concept of interfaces in C++ (and Java), where they were used as part of COM for describing object-oriented component interfaces.

C#中的接口直接从C ++(和Java)中的接口概念扩展而来,它们被用作COM的一部分,用于描述面向对象的组件接口。

EDIT: In doing a small amount of research, the earliest language I could find with an explicit "interface" keyword was Modula-3, a derivitive of Modula created around 1986.

编辑:在进行少量研究时,我能找到的最明确的“接口”关键词是Modula-3,它是1986年左右创建的Modula的衍生物。

#2


5  

Interfaces were also a central part of COM, which was a very successful technology for separating interfaces from implementation.

接口也是COM的核心部分,这是一种非常成功的技术,用于将接口与实现分离。

#3


3  

They came from java, and they were introduced because java (and C#) do not allow multiple inheritance.

它们来自java,因为java(和C#)不允许多重继承而引入它们。

EDIT: I'm receiving some downmods because people using COM interfaces in C++ disagree with the above statement. Regardless, the concept of an interface came from java, C++ COM interfaces were virtual classes, java was the first language to make it a language feature.

编辑:我收到一些downmods,因为在C ++中使用COM接口的人不同意上述声明。无论如何,接口的概念来自java,C ++ COM接口是虚拟类,java是第一种使它成为语言特性的语言。

END EDIT

For example, in C++, you could have a class named Dog that inherited from Animal and Mammal.

例如,在C ++中,您可以拥有一个名为Dog的类,该类继承自Animal和Mammal。

In C#, you would have a base class named Animal, and use an interface (IMammal). The I naming notation is historical from C++ (It was used to indicate an abstract virtual class), and was carried over to java but is more significant in C#, because there is no easy way to tell what is a base class and what is a interface from a C# class declaration:

在C#中,您将拥有一个名为Animal的基类,并使用一个接口(IMammal)。 I命名表示法是C ++中的历史记录(它用于表示一个抽象的虚拟类),并且被转移到java但在C#中更为重要,因为没有简单的方法来说明什么是基类,什么是来自C#类声明的接口:

public class Dog : Animal, IMammal

while in Java it was more obvious:

而在Java中则更为明显:

public class Dog extends Animal implements IMammal

Multiple inheritance is very tricky, so interfaces were derived to simplify it. A C# class can only inherit from one base class, but can implement N amount of interfaces.

多重继承非常棘手,因此可以派生接口来简化它。 C#类只能从一个基类继承,但可以实现N个接口。

In C++, interfaces can be simulated by using pure virtual classes. These require all methods to be overridden polymorphicaly by the inheriting class.

在C ++中,可以使用纯虚拟类来模拟接口。这些要求所有方法都由继承类重写多态。

#4


3  

I was under the impression that the first formalized concept of interfaces came from Objective-C (called "protocols"). I can tell you for sure that Java at least got the idea from Objective-C, so it wasn't Java that had interfaces first.

我的印象是第一个形式化的接口概念来自Objective-C(称为“协议”)。我可以肯定地告诉你,Java至少从Objective-C中得到了这个想法,所以Java首先不是接口。

Email from Patrick Naughton

来自Patrick Naughton的电子邮件

#5


2  

Interfaces existed in C++ if you did COM programming, which is where the IPrefix convention originates.

如果您进行了COM编程,那么接口就存在于C ++中,这是IPrefix约定的起源。

Although C++ itself didn't natively support interfaces, COM/C++ used type libraries generated from Interface Definition Language which has the only purpose of defining interfaces, and used the interface keyword long before Java or C# did.

尽管C ++本身并不支持接口,但COM / C ++使用了从接口定义语言生成的类型库,其唯一目的是定义接口,并且在Java或C#之前很久就使用了interface关键字。

Aside from allowing a form of multiple inheritence, .NET's motivation for interfaces have to do with its component-oriented origins and its main purpose is to define contracts between components that can interoperate without any knowledge of each other's implementations. Some COM interop is also done with .NET interfaces.

除了允许多重继承的形式之外,.NET的接口动机与其面向组件的起源有关,其主要目的是定义可以互操作的组件之间的契约,而无需了解彼此的实现。一些COM互操作也使用.NET接口完成。

#6


1  

C++ allows for multiple inheritance. When Java was developed, single inheritance was decided upon however classes were allowed to implement multiple interfaces. C# carried forward this concept.

C ++允许多重继承。在开发Java时,决定使用单继承,但允许类实现多个接口。 C#发扬了这个概念。

#7


1  

They existed in C++, but they were known as virtual base classes, which consisted only of pure virtual functions. This is where the "I-" prefix for interfaces came from -- to differentiate between virtual base classes from abstract base classes.

它们存在于C ++中,但它们被称为虚拟基类,它只包含纯虚函数。这是接口的“I-”前缀来自 - 用于区分虚拟基类和抽象基类。

#8


1  

i've seen the keyword interface first in java, but they are much older than that.

我在java中首先看到了关键字接口,但它们比它更老。

the same concept exists in c++ but it is not exactly the same. They are called "pure virtual classes"

c ++中存在相同的概念,但它并不完全相同。它们被称为“纯虚拟类”

http://en.wikipedia.org/wiki/Virtual_function

They exists with different syntax but are there to allow polymorphism in OOP.

它们存在不同的语法,但允许OOP中的多态性。

#9


1  

The earliest implementation of interfaces that I know of in computing came from CORBA.

我在计算中知道的最早的接口实现来自CORBA。

My understanding is that the concept came out of electrical and electronic engineering where a power outlet in a wall for instance can be used (and implemented) by anyone who knows the specification. Interfaces then provide the same flexibility programatically.

我的理解是,这个概念来自电气和电子工程,例如,任何知道规范的人都可以使用(并实施)墙上的电源插座。接口然后以编程方式提供相同的灵活性。

Incidentally while they were not created to reduce versioning problems they can certainly help with them.

顺便说一句,虽然它们不是为减少版本问题而创建的,但它们当然可以帮助它们。

#10


1  

I trink interfaces came from the fact that some programmers got tired of writing the implementation of a method over and over again. How many times can you write:

我认为接口来自于一些程序员厌倦了一遍又一遍地编写方法的实现。你能写多少次:

static string Method(int i)

without thinking there has to be an easier way?

没有想到必须有一个更简单的方法?

#11


0  

In C++ you could have an abstract class with no implementation, and you could inherit multiple classes. Java and C# got rid of multiple inheritance, so in order to have the ability to inherit multiple contracts (not behaviors), they created interfaces. You can only inherit one class in C#, but you can inherit as many interfaces as you want.

在C ++中,您可以拥有一个没有实现的抽象类,并且可以继承多个类。 Java和C#摆脱了多重继承,因此为了能够继承多个契约(而不是行为),他们创建了接口。您只能在C#中继承一个类,但您可以根据需要继承任意数量的接口。

An interface is jst a contract. It says which members that an instance must implement. It does this, however, without implementing any default behaviors.

接口是合约。它说明实例必须实现哪些成员。但是,它会执行此操作,而不会实现任何默认行为。

#12


0  

Well there wasn't any language integrated mechanism syntax for it, but you can achieve interfaces in C++ with pure virtual classes.

那么它没有任何语言集成机制语法,但您可以使用纯虚拟类在C ++中实现接口。

class IFoo
{
public:
  void Bar() =0;
  void Bar2() =0;
};

class Concrete : public IFoo
{
public:
  void Bar() { ... }
  void Bar2() { ... }
}

#13


0  

Interfaces came from computer science. Or, let's say, from common sense in programming. Interface is a logical group of methods of a class. C++ didn't need a separate language concept of "interface", because any class might be used as an interface -- just define a set of methods in it, make no implementation, call it like IExecutable and use:

接口来自计算机科学。或者,我们说,从编程的常识来看。接口是一个类的方法的逻辑组。 C ++不需要单独的“interface”语言概念,因为任何类都可以用作接口 - 只需在其中定义一组方法,不进行实现,像IExecutable一样调用它并使用:

class IExecutable
{
public:
    virtual void Execute() = 0;
};

class MyClass : public IExecutable
{
public:
    void Execute() { return; };
};

Some languages, called "dynamically typed", like Python, don't require to define interfaces at all, you just call a method you need, and run-time checks if it is possible ("If it walks like a duck and talks like a duck, it must be a duck").

一些被称为“动态类型”的语言,比如Python,根本不需要定义接口,你只需要调用一个你需要的方法,然后运行时检查是否可行(“如果它像鸭子那样行走并像一只鸭子,它必须是一只鸭子“)。

C# clearly separates a concept of interfaces from classes, because it uses static typing... and multiple inheritance is prohibited in that language, but it is ok for a class to have one base class and another interface, or to implement several interfaces at a time.

C#清楚地将接口的概念与类分开,因为它使用静态类型...并且在该语言中禁止多重继承,但是类可以有一个基类和另一个接口,或者在一个类中实现多个接口。时间。

public interface IPurring
{
    void Purr();
}

public class Cat : Animal, IPurring
{
    public Cat(bool _isAlive)
    {
        isAlive = _isAlive;
    }

    #region IPurring Members

    public void Purr()
    {
        //implement purring
    }

    #endregion
}

#14


0  

While not called 'interfaces', C data structure pointers with function pointers as elements of the structure implemented the concept of interfaces long before c++ did with virtual base classes IMO.

虽然没有被称为“接口”,但是具有函数指针作为结构元素的C数据结构指针在c ++使用虚拟基类IMO之前很久就实现了接口的概念。

#15


0  

Interfaces were also used in CORBA. Interface Definition Language (IDL) was used to describe interfaces independently of whatever language the object was implemented in. This separated not only interface and implementation, but also interface and language binding.

接口也用于CORBA。接口定义语言(IDL)用于独立于实现对象的任何语言来描述接口。这不仅分离了接口和实现,还分离了接口和语言绑定。

#17


-4  

It's just another layer of abstraction. Not really sure where it came from.. I still often hear them called contracts rather than interfaces.

它只是另一层抽象。不确定它来自哪里..我仍然经常听到他们叫合同而不是接口。

#18


-5  

I think the basic idea is "multiple inheritance". So the idea came from C++.

我认为基本的想法是“多重继承”。所以这个想法来自C ++。

#1


10  

Interfaces are pretty old, and have been around for quite a while.

接口很老,已经存在了很长一段时间。

Early (mid to late late 1970's) non-object oriented languages such as Modula and Euclid used constructs called "modules" to specify the interfaces between components. Components would then communicate with each other via explicit importing and exporting modules. Interfaces in C# are object oriented evolutions of that same concept.

早期(1970年代中后期),非面向对象语言(如Modula和Euclid)使用称为“模块”的构造来指定组件之间的接口。然后,组件将通过显式导入和导出模块相互通信。 C#中的接口是同一概念的面向对象的演进。

Interfaces in C# directly extend from the concept of interfaces in C++ (and Java), where they were used as part of COM for describing object-oriented component interfaces.

C#中的接口直接从C ++(和Java)中的接口概念扩展而来,它们被用作COM的一部分,用于描述面向对象的组件接口。

EDIT: In doing a small amount of research, the earliest language I could find with an explicit "interface" keyword was Modula-3, a derivitive of Modula created around 1986.

编辑:在进行少量研究时,我能找到的最明确的“接口”关键词是Modula-3,它是1986年左右创建的Modula的衍生物。

#2


5  

Interfaces were also a central part of COM, which was a very successful technology for separating interfaces from implementation.

接口也是COM的核心部分,这是一种非常成功的技术,用于将接口与实现分离。

#3


3  

They came from java, and they were introduced because java (and C#) do not allow multiple inheritance.

它们来自java,因为java(和C#)不允许多重继承而引入它们。

EDIT: I'm receiving some downmods because people using COM interfaces in C++ disagree with the above statement. Regardless, the concept of an interface came from java, C++ COM interfaces were virtual classes, java was the first language to make it a language feature.

编辑:我收到一些downmods,因为在C ++中使用COM接口的人不同意上述声明。无论如何,接口的概念来自java,C ++ COM接口是虚拟类,java是第一种使它成为语言特性的语言。

END EDIT

For example, in C++, you could have a class named Dog that inherited from Animal and Mammal.

例如,在C ++中,您可以拥有一个名为Dog的类,该类继承自Animal和Mammal。

In C#, you would have a base class named Animal, and use an interface (IMammal). The I naming notation is historical from C++ (It was used to indicate an abstract virtual class), and was carried over to java but is more significant in C#, because there is no easy way to tell what is a base class and what is a interface from a C# class declaration:

在C#中,您将拥有一个名为Animal的基类,并使用一个接口(IMammal)。 I命名表示法是C ++中的历史记录(它用于表示一个抽象的虚拟类),并且被转移到java但在C#中更为重要,因为没有简单的方法来说明什么是基类,什么是来自C#类声明的接口:

public class Dog : Animal, IMammal

while in Java it was more obvious:

而在Java中则更为明显:

public class Dog extends Animal implements IMammal

Multiple inheritance is very tricky, so interfaces were derived to simplify it. A C# class can only inherit from one base class, but can implement N amount of interfaces.

多重继承非常棘手,因此可以派生接口来简化它。 C#类只能从一个基类继承,但可以实现N个接口。

In C++, interfaces can be simulated by using pure virtual classes. These require all methods to be overridden polymorphicaly by the inheriting class.

在C ++中,可以使用纯虚拟类来模拟接口。这些要求所有方法都由继承类重写多态。

#4


3  

I was under the impression that the first formalized concept of interfaces came from Objective-C (called "protocols"). I can tell you for sure that Java at least got the idea from Objective-C, so it wasn't Java that had interfaces first.

我的印象是第一个形式化的接口概念来自Objective-C(称为“协议”)。我可以肯定地告诉你,Java至少从Objective-C中得到了这个想法,所以Java首先不是接口。

Email from Patrick Naughton

来自Patrick Naughton的电子邮件

#5


2  

Interfaces existed in C++ if you did COM programming, which is where the IPrefix convention originates.

如果您进行了COM编程,那么接口就存在于C ++中,这是IPrefix约定的起源。

Although C++ itself didn't natively support interfaces, COM/C++ used type libraries generated from Interface Definition Language which has the only purpose of defining interfaces, and used the interface keyword long before Java or C# did.

尽管C ++本身并不支持接口,但COM / C ++使用了从接口定义语言生成的类型库,其唯一目的是定义接口,并且在Java或C#之前很久就使用了interface关键字。

Aside from allowing a form of multiple inheritence, .NET's motivation for interfaces have to do with its component-oriented origins and its main purpose is to define contracts between components that can interoperate without any knowledge of each other's implementations. Some COM interop is also done with .NET interfaces.

除了允许多重继承的形式之外,.NET的接口动机与其面向组件的起源有关,其主要目的是定义可以互操作的组件之间的契约,而无需了解彼此的实现。一些COM互操作也使用.NET接口完成。

#6


1  

C++ allows for multiple inheritance. When Java was developed, single inheritance was decided upon however classes were allowed to implement multiple interfaces. C# carried forward this concept.

C ++允许多重继承。在开发Java时,决定使用单继承,但允许类实现多个接口。 C#发扬了这个概念。

#7


1  

They existed in C++, but they were known as virtual base classes, which consisted only of pure virtual functions. This is where the "I-" prefix for interfaces came from -- to differentiate between virtual base classes from abstract base classes.

它们存在于C ++中,但它们被称为虚拟基类,它只包含纯虚函数。这是接口的“I-”前缀来自 - 用于区分虚拟基类和抽象基类。

#8


1  

i've seen the keyword interface first in java, but they are much older than that.

我在java中首先看到了关键字接口,但它们比它更老。

the same concept exists in c++ but it is not exactly the same. They are called "pure virtual classes"

c ++中存在相同的概念,但它并不完全相同。它们被称为“纯虚拟类”

http://en.wikipedia.org/wiki/Virtual_function

They exists with different syntax but are there to allow polymorphism in OOP.

它们存在不同的语法,但允许OOP中的多态性。

#9


1  

The earliest implementation of interfaces that I know of in computing came from CORBA.

我在计算中知道的最早的接口实现来自CORBA。

My understanding is that the concept came out of electrical and electronic engineering where a power outlet in a wall for instance can be used (and implemented) by anyone who knows the specification. Interfaces then provide the same flexibility programatically.

我的理解是,这个概念来自电气和电子工程,例如,任何知道规范的人都可以使用(并实施)墙上的电源插座。接口然后以编程方式提供相同的灵活性。

Incidentally while they were not created to reduce versioning problems they can certainly help with them.

顺便说一句,虽然它们不是为减少版本问题而创建的,但它们当然可以帮助它们。

#10


1  

I trink interfaces came from the fact that some programmers got tired of writing the implementation of a method over and over again. How many times can you write:

我认为接口来自于一些程序员厌倦了一遍又一遍地编写方法的实现。你能写多少次:

static string Method(int i)

without thinking there has to be an easier way?

没有想到必须有一个更简单的方法?

#11


0  

In C++ you could have an abstract class with no implementation, and you could inherit multiple classes. Java and C# got rid of multiple inheritance, so in order to have the ability to inherit multiple contracts (not behaviors), they created interfaces. You can only inherit one class in C#, but you can inherit as many interfaces as you want.

在C ++中,您可以拥有一个没有实现的抽象类,并且可以继承多个类。 Java和C#摆脱了多重继承,因此为了能够继承多个契约(而不是行为),他们创建了接口。您只能在C#中继承一个类,但您可以根据需要继承任意数量的接口。

An interface is jst a contract. It says which members that an instance must implement. It does this, however, without implementing any default behaviors.

接口是合约。它说明实例必须实现哪些成员。但是,它会执行此操作,而不会实现任何默认行为。

#12


0  

Well there wasn't any language integrated mechanism syntax for it, but you can achieve interfaces in C++ with pure virtual classes.

那么它没有任何语言集成机制语法,但您可以使用纯虚拟类在C ++中实现接口。

class IFoo
{
public:
  void Bar() =0;
  void Bar2() =0;
};

class Concrete : public IFoo
{
public:
  void Bar() { ... }
  void Bar2() { ... }
}

#13


0  

Interfaces came from computer science. Or, let's say, from common sense in programming. Interface is a logical group of methods of a class. C++ didn't need a separate language concept of "interface", because any class might be used as an interface -- just define a set of methods in it, make no implementation, call it like IExecutable and use:

接口来自计算机科学。或者,我们说,从编程的常识来看。接口是一个类的方法的逻辑组。 C ++不需要单独的“interface”语言概念,因为任何类都可以用作接口 - 只需在其中定义一组方法,不进行实现,像IExecutable一样调用它并使用:

class IExecutable
{
public:
    virtual void Execute() = 0;
};

class MyClass : public IExecutable
{
public:
    void Execute() { return; };
};

Some languages, called "dynamically typed", like Python, don't require to define interfaces at all, you just call a method you need, and run-time checks if it is possible ("If it walks like a duck and talks like a duck, it must be a duck").

一些被称为“动态类型”的语言,比如Python,根本不需要定义接口,你只需要调用一个你需要的方法,然后运行时检查是否可行(“如果它像鸭子那样行走并像一只鸭子,它必须是一只鸭子“)。

C# clearly separates a concept of interfaces from classes, because it uses static typing... and multiple inheritance is prohibited in that language, but it is ok for a class to have one base class and another interface, or to implement several interfaces at a time.

C#清楚地将接口的概念与类分开,因为它使用静态类型...并且在该语言中禁止多重继承,但是类可以有一个基类和另一个接口,或者在一个类中实现多个接口。时间。

public interface IPurring
{
    void Purr();
}

public class Cat : Animal, IPurring
{
    public Cat(bool _isAlive)
    {
        isAlive = _isAlive;
    }

    #region IPurring Members

    public void Purr()
    {
        //implement purring
    }

    #endregion
}

#14


0  

While not called 'interfaces', C data structure pointers with function pointers as elements of the structure implemented the concept of interfaces long before c++ did with virtual base classes IMO.

虽然没有被称为“接口”,但是具有函数指针作为结构元素的C数据结构指针在c ++使用虚拟基类IMO之前很久就实现了接口的概念。

#15


0  

Interfaces were also used in CORBA. Interface Definition Language (IDL) was used to describe interfaces independently of whatever language the object was implemented in. This separated not only interface and implementation, but also interface and language binding.

接口也用于CORBA。接口定义语言(IDL)用于独立于实现对象的任何语言来描述接口。这不仅分离了接口和实现,还分离了接口和语言绑定。

#16


#17


-4  

It's just another layer of abstraction. Not really sure where it came from.. I still often hear them called contracts rather than interfaces.

它只是另一层抽象。不确定它来自哪里..我仍然经常听到他们叫合同而不是接口。

#18


-5  

I think the basic idea is "multiple inheritance". So the idea came from C++.

我认为基本的想法是“多重继承”。所以这个想法来自C ++。