我想使用界面 [英] I Want to use the Interface

查看:58
本文介绍了我想使用界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有2个项目,分别是Accounts and Info

Accounts Project中的类之一(公共后的帐户是表中的列)

使用System.ComponentModel.Composition;
.......

Hi,
I have 2 projects Namely Accounts and Info

One of the class in Accounts Project(Account after public is a column in a table)

using System.ComponentModel.Composition;
.......

namespace Accounts
{
     [export(typeof(IAccount))]
      public class Account : IAccount
      {
             public Account GetAccountByEmail(string Ema)
             {
              ..........  
             }
       }
}



这是我的界面(这是另一个项目)



This is my interface(this is another project)

namespace Interfaces
{
    public interface IAccount
    {
    }
}



这是我在Info项目中的代码



this is my code in Info project

using System.Componentmodel.Composition;
using Interfaces;

public class AccountSer
{
   [Import]
   public IAccount _account;
   {
     public bool bhjyhf(string user)
     { 
          Account account = _account.GetAccountByEmail(user);
          if(account != null)
            return true;
          return false;
     }
    }
}



IAccount没有扩展方法(GetAccountByEmail)出现错误
我应该在界面中键入什么是什么问题?还是应该做其他事情??

请帮帮我!
当我使用



There is an error that IAccount has no extension method(GetAccountByEmail)
What is wrong should i type something in my interface.. or should i do something else??

please help me!!
When i Use

private Account _account ;

并继续...以相同的方式处理其余代码时,我得到所需的输出

请让我知道该怎么办..

谢谢.

and proceed... with the rest of the code in the same way i get the required output

Please let me know what to do..

Thank you.

推荐答案

您的特定错误是必须在IAccount 界面中定义一个方法才能使用它:

Your specific error is that you have to define a method in your IAccount interface so that you can use it:

namespace Interfaces
{
    public interface IAccount
    {
        Account GetAccountByEmail(string Ema);
    }
}



这意味着在将object 定义为IAccount时尝试使用该方法时,可以访问该方法.

您也可以将IAccount 强制转换为Account 来执行相同的操作.



That means that when you attempt to use the method when you have defined an object as IAccount, you can access the method.

You can also cast IAccount as Account to do the same.

acct = ((Account)iAcct).GetAccountByEmail(ema);


您的问题与接口无关.暂时忘掉接口,只关注继承和运行时与编译时类型.

考虑一下:
Your question has nothing specific to interfaces. Forget about interfaces for a minute and focus on just inheritance and run-time vs. compile-time types.

Consider this:
class Base { / *...* / }

class Derived {
    internal DerivedSpecific() { / *...* / }
}

//...

// compile-time type: Base, actual run-time type: Derived
Base instance = new Derived();

// even though the instance is Derived and Derived.DerivedSpecific can be called,
// instance "does not know" about it during compile time.
// this will fail to compile:
instance.DerivedSpecific(); // compilation failure


现在,Base可以是一个类或在接口中,您的错误的性质是相同的.您需要将方法移至基本类型(视情况而定)至接口.特定于派生类的方法应在此类中或通过其实例(而不是接口(或基类)类型的实例)进行调用.还有一件事:您可以小写实例并仍然调用方法. 甚至不用考虑-可能,但是这意味着不良的设计违反了继承和封装的目的.您的代码示例表明,您还没有感觉到这些目的.您需要从一开始就重新审视对OOP的理解,而不仅仅是接口.

—SA


Now, Base could be a class or in interface, the nature of your bug is the same. You need to move the method to the base type, in your case, to the interface. The methods specific to the derived class should be called in this class or via its instance, not the instance of the interface (or base class) type. One more thing: you can down-case the instance and still call the method. Don''t even think about it — it would be possible but means a bad design and defeat of the purpose of inheritance and encapsulation. You code sample shows that you don''t feel these purposes, just yet. You need to review your understanding of OOP from the very beginning, not just the interfaces.

—SA


从此开始: ^ ]
界面-编程指南 [
Start with this: Interfaces in C# (For Beginners)[^]
Interfaces - Programming guide[^]

If you want more, use Google with phrase: "C# interface"


这篇关于我想使用界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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