在.NET中使用接口和抽象类 [英] Using Interface and Abstract Class in .NET

查看:97
本文介绍了在.NET中使用接口和抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码段:

Following is the code Snippet:

public interface ITest
{
    void print();
}
public abstract class Testing
{
    public abstract void print();
}
public class Usage: Testing, ITest
{
}




我们可以一起实现Interface和Abstract类吗?如果是,则调用其print().




Can we Implement an Interface and Abstract class together. If Yes, then whose print() gets called. Can you please give explanation also.

推荐答案

首先,它甚至不会编译.它必须看起来像这样:

First, that won''t even compile. It has to look like this:

public interface ITest
{      
    void print();
}

public abstract class Testing : ITest
{      
    public abstract void print();
}

public class Usage: Testing
{
    public override void print() {}
}



其次,只有派生类中的print方法会被选中. ITestTesting中的方法仅要求派生类具有该方法.本质上,您对print方法的要求是多余的.

=========
您说这个问题是正确的,因为它是在采访中被问到的."

这是不正确的.它根本不会编译.如果您花时间将其实际放在测试应用中并进行尝试,那么您会发现的.此外,我的回答是正确的.

抽象类中的方法没有主体,因此不能被命中".接口中的方法也不会被命中,因为它没有公共访问器. print方法将被击中的唯一位置是在派生类中.



Second, only the print method in the derived class will be hit. The methods in ITest and Testing simply require the derived class to have that method. Essentially you''re being redundant about the requirement for the print method.

=========
You stated that "the question is correct because it was asked in an interview".

It''s NOT correct. It simply won''t compile. If you had taken the time to actually put it in a test app and try it, you would have found that out. Further, my answer is correct.

A method in an abstract class cannot be "hit" because it has no body. A method in an interface can also not be hit because it has no public accessor. The only place the print method will be hit is in the derived class.


不.在给定的代码示例中,ITest.printTest.printUsage中可以具有相同的实现.如下定义Usage.print是完全合法的:

No. In the given code example, ITest.print and Test.print can have the same implementation in Usage. It is perfectly legal to define Usage.print as follows:

public class Usage: Testing, ITest {

    public override void print() {
     Console.WriteLine("Usage.Print");
    }

}



关键是将override修饰符添加到Usage.print的定义中.否则,Usage.print将(a)隐藏Test.print 的实现,并且(b)Usage无法实例化,因为它缺少Test.print的抽象声明的定义.

以下所有调用均默认为Usage.Print:



The key is to add the override modifier to the definition of Usage.print. Otherwise, Usage.print would (a) hide the implementation of Test.print and (b) Usage could not be instantiated because it would be lacking the definition of the abstract declaration from Test.print.

All of the following invocations default to Usage.Print:

static void CallPrint(Usage pUsageInstance) {
    pUsageInstance.print();
}

static void CallPrint(Test pTestInstance) {
    pTestInstance.print();
}

static void CallPrint(ITest pITestInstance) {
    pITestInstance.print();
}





Usage tUsage = new Usage();

CallPrint(tUsage);
CallPrint((Test)tUsage);
CallPrint((ITest)tUsage);


除了 paul_71 John Simmons 所写的内容外,还有另一种选择.

您可以执行以下操作:

There is another option apart from what paul_71 and John Simmons wrote.

You can do the following:

public class Usage: Testing, ITest {
  public override void print() {
    // Implementation of the Testing.print() method
  }

  void ITest.print() {
    // Implementation of the ITest.print() method
  }
}



现在,无论何时调用Usage.print()方法,都将调用从Testing继承的方法.但是,如果将对象作为ITest实例访问(将其强制转换为ITest),然后调用print()方法,则将调用第二个方法.



Now whenever you call the Usage.print() method, the method inherited from Testing will be called. However, if you access your object as an ITest instance (you cast it to ITest) and then call the print() method, the second method will be called.


这篇关于在.NET中使用接口和抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆