简体   繁体   English

C# 中的“扩展”和“实现”Java 等效项

[英]'Extends' and 'Implements' Java equivalents in C#

What is the C# equivalent syntax for the following Java statement:以下 Java 语句的 C# 等效语法是什么:

public class Lion extends Animal implements Diurnal()
{
}
  • Animal is Base class动物是基类
  • Diurnal is an Interface Diurnal 是一个接口

the inheritance could be declared like this.继承可以这样声明。

public class Lion : Animal, Diurnal
{

}

In C# , you can inherit one base class and can be multiple Interfaces.C# ,可以继承一个基类,也可以是多个接口。

One more tip, if you are making an Interface in C#, prefix it with I .还有一个提示,如果你在 C# 中创建一个Interface在它前面加上I前缀。 eg IDiurnal例如IDiurnal

public class Lion : Animal, // base class must go first
                    Diurnal // then interface(s) if any
{
}

Would look something like this:看起来像这样:

public class Lion :Animal, Diurnal {
}

Where Animal is a class and Diurnal is an interface .其中Animal是一个类,Diurnal是一个接口

Please note, that according to the C# naming convention, interface has to have "I" infront of its name, so finally it should look like this:请注意,根据 C# 命名约定,接口的名称前必须有“I”,因此最终如下所示:

public class Lion :Animal, IDiurnal {
}

In C#, there is uniform syntax for extending class and implementing interface.在 C# 中,扩展类和实现接口有统一的语法。

public class Lion : Animal, Diurnal {

}

你需要写下第一个基类像( Animal是基类),最近的接口像as( Diurnal是一个接口)

public class Lion : Animal, Diurnal {}

the first name after : is the extended class, after comes the implemented interfaces : 后面的名字是扩展类,后面是实现的接口

public class Lion : Animal, Diurnal
{
}

c# do not allow multiple class extension, but you can implement many interfaces c#不允许多个类扩展,但是可以实现很多接口

public class Lion : Animal, Diurnal
{
}

interface Diurnal
{
}

class Animal
{
}

Class Animal was inherited by Lion class.AnimalLion类继承。 Diurnal class is interface. Diurnal类是接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM