如何在程序中使用接口概念并满足其概念? [英] How do I utilize interface concept in a program and satisfy its concept?

查看:50
本文介绍了如何在程序中使用接口概念并满足其概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接口:



Interface :

public interface Interface2
   {
       void get2();
       bool insert2(User U);
   }









实施:







Implementation:

public class HomeController : Controller
   {
       Interfaces _iInterfaces;
       Interface2 _iInterface2;

       public HomeController(Interfaces iInterface,Interface2 iInterface2)
       {
           _iInterfaces = iInterface;
           _iInterface2 = iInterface2;
       }



       [HttpPost]
       public ActionResult Index(User objUser)
       {

           _iInterfaces.insert(objUser);
           _iInterface2.insert2(objUser);

           return View();
       }







我想知道的是我的Interface2声明了2种方法。我在类中实现接口,但只在接口中声明的2个方法中调用1方法(insert2)。



这是否满足Interface的使用,或者当实现该接口时,我必须强制给出接口内所有方法的定义。



我尝试过:



公共接口接口2

{

void get2();

bool insert2(用户U);

}





我已经实现了这个接口,只使用了其中一种方法。它是否足够?




What i want to know is my Interface2 has 2 methods declared. I am implementing the interface in a class but only calling 1 method(insert2) out of 2 methods declared in interface.

Does this satisfy the usage of Interface or i should compulsory give a definition to all the methods inside a interface when that interface is implemented.

What I have tried:

public interface Interface2
{
void get2();
bool insert2(User U);
}


I have implemented this interface and only used one of the method. Does it suffice ?

推荐答案

只要您在具体类中实现这些方法,那么您应该没问题。但是,如果您不使用在界面中定义的其他方法,那么您只需删除它。
For as long you've implemented those methods in a concrete class then you should be fine. However, if you don't use the other method you defined in your interface somewhere in your app then you could just simply remove it.


您不必调用所有成员界面,只需拨打你需要的。但是,如果你的界面实际上是相同的话,在线之间阅读;



You don't have to call all members of an interface, just call the ones you need. Reading between the lines, though, if your interfaces are effectively the same;

public interface Interfaces
   {
       void get();
       bool insert(User U);
   }

   public interface Interface2
   {
       void get2();
       bool insert2(User U);
   }





那么你根本不需要Interface2,只需让实现Interface2的类实现接口。



then you don't need Interface2 at all, simply have the class that implements Interface2 implement Interfaces instead.


实现接口的任何类(或在C#,Struct中)必须实现接口中指定的所有属性和方法(以及索引器和事件)(C#接口不允许使用Fields。



请记住,C#接口不是Class,或者是值或引用Type。虽然你可以调用一些'接口上的对象,比如'ToString(),但接口的本质在.NET中是相当独特的:请参阅Jon Skeet的这些评论:[ ^ ]。



接口可以继承自另一个界面,我建议你把它看成是扩展的一个例子。并且,您可以使用具有接口的泛型类型声明:
Any Class (or, in C#, Struct) that implements an Interface must implement all Properties and Methods (and Indexers, and Events) specified in the Interface (C# interfaces do not allow Fields).

Keep in mind that a C# Interface is not a Class, or, a value, or reference, Type. While you can call some methods of 'Object on an Interface, like 'ToString(), the "nature" of an Interface is rather unique in .NET: see these comments by Jon Skeet: [^].

An Interface can inherit from another Interface, and I suggest you look at this as being an example of "extension;" and, you can use generic Type declaration(s) with an Interface:
public interface IController
{
    // property
    string Name { set; get; }
}

public interface IControllerItems<T> : IController
{
    // property
    List<T> Items { set; get; }
    
    // method
    int ModelNumber { set; get; }
    
    // method
    void AddItem(T item);
}

因此,一个接口是一个合同,一个你将要实现的承诺,一个你必须实现的要求;我建议你认为这与Classes的继承不同。



一旦你在一个类中实现了一个接口,你在多大程度上利用了每个属性你实施的方法是......由你决定。



问题在这里。继续......

So, an Interface is a contract, a promise that you will implement, a demand that you must implement; I suggest you think of that as different from "inheritance" of Classes.

Once you have implemented an Interface in a Class, to what extent you make use of every Property and Method you've implemented is ... up to you.

Problem here. To be continued ...


这篇关于如何在程序中使用接口概念并满足其概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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