what is the behaviour abstract class and interface in c#?
This question already has an answer here:
You need explicit implementation of interface. The abstract class method method()
implementation fulfill the need of implementation of abstract method of interface. So define the method of interface in the class childe
but explicit implementation need to call the method of interface but not on class.
public interface NomiInterface
{
void method();
}
public abstract class Nomi1
{
public void method()
{
Console.WriteLine("abstract class method");
}
}
public class childe : Nomi1, NomiInterface
{
void NomiInterface.method()
{
Console.WriteLine("interface method");
}
}
You can test how you can call the method of abstract class and interface implementation present in childe
childe c = new childe();
NomiInterface ni = new childe();
ni.method();
c.method();
The output is
interface method
abstract class method
On the other hand if you do not do explicit interface implementation then the implementation given in childe class wont be call on childe or interface object.
public interface NomiInterface
{
void method();
}
public abstract class Nomi1
{
public void method()
{
Console.WriteLine("abstract class method");
}
}
public class childe : Nomi1, NomiInterface
{
void method() { Console.WriteLine("interface method"); }
}
Create object of class and interface as we did previously.
childe c = new childe();
NomiInterface ni = new childe();
ni.method();
c.method();
The output you will get
abstract class method
abstract class method
As an additional note you will take care of naming conventions for class / method names. You can find more about naming conventions here.
链接地址: http://www.djcxy.com/p/54258.html上一篇: 抽象类和接口之间的技术差异
下一篇: 什么是在C#中的行为抽象类和接口?