English 中文(简体)
当一个执行该分类的类别有其自身属性时, 如何使用接口?
原标题:how to use interface when a class that implements it have its own attributes?

我正在使用一个界面引用变量来访问接口上的属性

但除此之外,执行接口的类别也有其自身的属性。

我无法通过此界面引用访问类属性 。

以下是我的问题:

1) 为什么会这样?

2) 这个问题的解决方案是什么?我是否可以通过机器进入空调级的冷却电源变量?TYPE CAST是否有效?

 interface IMachines
{
    #region properties
    int machineID { get; set; 时 时
    static int totalID { get; set; 时 时
    string name { get; set; 时 时
    string make { get; set; 时 时
    int weight { get; set; 时 时
    int cost { get; set; 时 时
    int warranty { get; set; 时 时
    DateTime creationDate { get; set; 时 时
    #endregion

    int generateWarrantyExpiry();
    int searchMachine();
时 时

public class AC:IMachines
{
    #region ACMembers

    protected int _machineID;
    protected string _name;
    protected int _weight;
    protected string _make;
    protected DateTime _creationDate;
    protected int _warranty;
    protected int _cost;
    public int _coolentPower;

    public int CoolentPower
    {
        get { return _coolentPower; 时 时
        set { _coolentPower = value; 时 时
    时 时

    #endregion




    #region IMachines Members

    public int machineID
    {
        get { return _machineID; 时 时
        set { _machineID = value; 时 时
    时 时

    public string name
    {
        get { return _name; 时 时
        set { _name = value; 时 时
    时 时

    public string make
    {
        get { return _make; 时 时
        set { _make = value; 时 时
    时 时

    public int weight
    {
        get { return _weight; 时 时
        set { _weight = value; 时 时
    时 时

    public int cost
    {
        get { return _cost; 时 时
        set { _cost = value; 时 时
    时 时

    public int warranty
    {
        get { return _warranty; 时 时
        set { _warranty = value; 时 时
    时 时

    public DateTime creationDate
    {
        get { return _creationDate; 时 时
        set { _creationDate = value; 时 时
    时 时

    public int searchMachine()
    {
        //Search machine logic to be implemented
return 2
    时 时

    public void GenerateWarranty()
{
//generate warranty logic to be implemented

时 时

    #endregion
时 时

时 时

最佳回答

请注意,使用石膏,正如许多答复者所建议的那样,将打破IMachines 接口提供的抽象内容。

如果您真的只使用一个执行界面的类型, AC , 那么这将会有效, 但如果您想要支持其他类型的 IMachines , 事情可能会崩溃 。

问题回答

是的, 您可以使用“ as” 关键字来尝试投影到 AC 类 :

IMachines machines = new AC();
(machines as AC).CoolentPower = 3;

以这种方式使用接口的有趣之处之一是能够检查一个通用变量是否执行接口。例如,您有一个在较低级别运行的通用函数。如果给您给定对象执行 IMachines, 请对它做一些特殊或额外的事情。

public void DOSTUFF (object myobject)
{
    IMachines machine = myobject as IMachines;
    if (machine != null)
    {
        //do something special for IMachine objects
    }

    //the rest of the function that gets run against everything
}

和是,要访问界面中未定义的类别部分,您必须直接投放到类型上,而不是使用界面。

为什么?

接口是 Contract 的声明。 您的意思是执行类型符合该合同。

当使用接口访问属性时, 您只能访问界面所声明的属性 - 界面如何“ 知道”这些属性? 它需要“ 了解” 执行该属性的所有类型, 您指的是哪个类型 。

解决方案是当您需要界面抽象时使用界面,当您需要使用该类型及其所有定义属性时使用具体类型。

有一个解决办法,叫做CAST

投到该类的类型上, 使该类能够进入 内脏本身没有的属性/ 方法 。

var machine = (AC)interfaceVar;
machine.CoolentPower

或者,简单地使用""http://msdn.microsoft.com/en-us/library/dd264741.aspx" rel="no follow" > 动力

dynamic machine = interfaceVar; //no cast needed ! non need to "know" actual type
machine.CoolentPower

如果您选择了课程,您可以访问课程的属性 。

var cool = machine as AC;

回答 1- 因为接口定义了某类人将执行的合同, 因此只要合同( 即该界面) 保持不变, 你就可以更改执行 。 您想要做的是获得不属于合同一部分的属性 - 如果您可以访问您的AC 类属性, 你会期望会发生什么? 但是有人在使用 IMACHIN 但不具备 AAC 类属性的 MMAICX 中通过了一个例子 。 编译者无法猜到在这种情况下要做什么, 因此您必须明确告诉它, 如果您的机器是 AC, 那么作为 AC 来做一些事情( 并且其他的解答是用来投影的 ) 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签