C# 相当于 Java 'implements' 关键字?

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

C# equivalent of Java 'implements' keyword?

c#java

提问by timmyg

In Java if you were to have the statement:

在 Java 中,如果您有以下语句:

public class MyClass implements LargerClass {

Would you be extending the LargerClass with more methods?

你会用更多的方法扩展 LargerClass 吗?

What would be the equivalent of this class definition in C#?

C# 中这个类定义的等价物是什么?

I ask because I am not very familiar with Java and am currently converting some Java code to C# code and this one is giving me some trouble.

我问是因为我对 Java 不是很熟悉,目前正在将一些 Java 代码转换为 C# 代码,而这个代码给我带来了一些麻烦。

Thanks in advance.

提前致谢。

采纳答案by Rob

public class MyClass implements LargerClass

In Java, this declares that MyClassimplements the interfaceLargerClass; that is, sets out implementations for the behaviours defined in LargerClass.

在 Java 中, this 声明MyClass实现了接口LargerClass;也就是说,列出了 中定义的行为的实现LargerClass

To inheritfrom another class in Java, use extends, e.g.

要从Java 中的另一个类继承,请使用extends,例如

public class MyClass extends LargerClass

The C# equivalent, in both cases, is specified as

在这两种情况下,C# 等效项都指定为

public class MyClass : LargerClass

Since this syntax doesn't make it clear whether or not LargerClassis an interface or another class being inherited, you'll find C#/.NET developers adopt the convention that interface names are prefixed with uppercase "I", e.g. IEnumerable.

由于此语法没有明确LargerClass表示接口或其他类是否被继承,您会发现 C#/.NET 开发人员采用接口名称以大写“I”作为前缀的约定,例如IEnumerable

回答by David Morton

public class MyClass : LargerClass
{

}

回答by TheSmurf

public class MyClass : LargerClass {...}