实施接口 [英] Implement an interface

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

问题描述



我必须实现此接口ICritterFactory,但是我很困惑.我知道我必须创建一个具有相同方法的新类,但我正在尝试找出方法.

以下是ICritterFactory的定义:

Hi,

I have to implement this interface ICritterFactory but I am so confused. I know that I have to make a new class that has the same method but I''m trying to figure out how to do so.

The following is a definition of ICritterFactory:

public interface ICritterFactory
{
ICritterBrain[] CreateCritterBrains();
}



CreateCritterBrains,返回实现接口ICritterBrain的对象数组.

-

我已经制作了此类来实现接口.



CreateCritterBrains, returns an array of objects that implement the interface ICritterBrain.

-

I have made this class to implement the interface.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CritterInterfaces;

namespace CritterWorld
{
    public class Createcritter : ICritterFactory
    {
        ICritterBrain[] array1 = new ICritterBrain[2];
        ICritterBrain[] CreateCritterBrains()
        {
            return array1;
        }
    }
}





is it right or am I missing something?

推荐答案

1.大致上对我很好.它可以编译吗?你测试过了吗?

2.但是,您可能要考虑您的接口/类方法/成员变量是否是公共的/受保护的/私有的-如有疑问,请使内容明确.

3.虽然array1可以作为数组使用,但其元素将为null,直到您将对象分配给它们为止.我认为ICritterBrain也是一个接口-因此您将无法直接实例化它,并且需要从中派生一个或多个类.
1. Broadly looks good to me. Does it compile? Have you tested it?

2. But you may want to consider whether your interface / class methods / member variables are public / protected / private - if in doubt make things explicit.

3. And while your array1 is good as an array its elements will be null until you assign objects to them. I assume ICritterBrain is also an interface - so you won''t be able to instantiate it directly and will need a class (or classes) that derive from it.


代码问题有问题的示例是接口方法未暴露给类用户.
有两种形式:

The problem of the code sample in question is that interface method is not exposed to the class user.
There are two forms:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CritterInterfaces;
 
namespace CritterWorld
{

    public class CritterFactoryGood : ICritterFactory
    {
        ICritterBrain[] someArray = new ICritterBrain[2];
        ICritterBrain[] ICritterFactory.CreateCritterBrains() //explicit interface name
        {
            return someArray;
        }
    } //CritterFactoryGood


    public class CritterFactoryQuestionable : ICritterFactory
    {
        ICritterBrain[] someArray = new ICritterBrain[2];
        public ICritterBrain[] CreateCritterBrains() //public
        {
            return someArray;
        }
    } //CritterFactoryQuestionable

} //namespace CritterWorld



CritterFactoryGood中的第一种形式更好,因为它更安全,不会创建冗余访问并且不会鼓励接口和类引用或结构的分离.考虑一下:



First form as in CritterFactoryGood is better because it is safer, does not create redundant access and encourage separation of interface and class references or structures. Consider this:

ICritterFactory ifactory = new CritterFactoryGood();
var result1 = ifactory.CreateCritterBrains();
CritterFactoryGood factoryInstance = new CritterFactoryGood();
//will not compile:
var result2 = factoryInstance.CreateCritterBrains();



使用CritterFactoryQuestionable,两个变体都可以编译.同样,对于第一种形式,一个类可以具有两个具有相同签名和名称的属性方法,这些方法实现两个不同的接口,重要的是,两个索引了"this"属性.

—SA



With CritterFactoryQuestionable, both variants would compile. Also, with first form, a class can have two properties methods with identical signature and names implementing two different interfaces, importantly, two indexed "this" properties.

—SA


您需要通过使用: ICritterFactory 并在其中添加方法来将接口添加为实现.它:
You need to add the interface as an implementation by using : ICritterFactory and adding the method in. Here''s how you''d add it:
public class CritterFactory : ICritterFactory // This is added to the 
//class definition
{
  public ICritterBrain[] CreateCritterBrains() // Implements method from the factory.
  {
  }
}


这篇关于实施接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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