0

I have the following code sections and I am trying to implement a method that returns a list. My structure is as follows : abstract class "Coordinates" -> public class "YCoordinate" with my interface pointing to "YCoordinate". I don't want my interface directly chatting to my abstract class as I feel the implementations of my abstract class is responsible for work.

Interface :

interface IYCoordinate
{
    System.Collections.Generic.List<Coordinates> GetYCoordinateList();
}

Abstract class :

public abstract class Coordinates
{
    private int coordinatePriority;
    private int coordinate;

    public Coordinates(int CoordinatePriority, int Coordinate)
    {
        this.CoordinatePriority = CoordinatePriority;
        this.Coordinate = Coordinate;
    }

    public Coordinates() { }
    public int CoordinatePriority { get { return coordinatePriority; } set { coordinatePriority = value; } }
    public int Coordinate { get { return coordinate; } set { coordinate = value; } }

    public virtual List<Coordinates> GetYCoordinateList()
    {
        throw new System.NotImplementedException();
    }
}

Implementation of Interface and abstract class (this is the part that breaks):

public class YCoordinate : Coordinates, IYCoordinate
{
    public override List<Coordinates> GetYCoordinateList() 
    {
        List<Coordinates> _list = new List<Coordinates>();
        _list.Add(new IYCoordinate(1, 5000));
        _list.Add(new IYCoordinate(2, 100000));
        return _list;
    }
}

However, I am trying to return a list of Coordinates and getting stuck on the public override function in the "YCoordinate" class as I cannot instantiate the Coordinates class directly (because its abstract). How can I return a list of Coordinates? The same error happens if I put in IYCoordinate as shown above.

Maybe my implementation is completely wrong? Any recommendation for doing it better would be welcome.

Later on there will be XCoordinate class and so on. If this approach seems like a bit much its because I am trying to get the hang of the theory behind this.

4

5 回答 5

1

我会这样做:

1)你有一个界面 - ,但你说你也IYCoordinate想拥有。只需使用单个界面 -和一个功能 - :IXCoordinateIZCoordinateICoordinateGetCoordinateList

interface ICoordinate
{
    IList<Coordinates> GetCoordinateList();
}

实际上,如果您使用我的方法,可以消除此接口

2)在您的抽象类中,您将有一个GetCoordinateList不做任何事情的存根

public abstract class Coordinates: ICoordinate
{
    public virtual List<Coordinates> GetCoordinateList()
    {
        throw new System.NotImplementedException();
    }
}

实际上,您可以ICoordinate完全消除接口并仅使用抽象类。没关系。

3)您将从该类派生一个(或多个)类并根据需要Coordinates实现GetCoordinate

public class YCoordinate : Coordinates
{
    public override List<Coordinates> GetCoordinateList() 
    {
        List<Coordinates> _list = new List<Coordinates>();
        _list.Add(new YCoordinate(1, 5000));
        _list.Add(new YCoordinate(2, 100000));
        return _list;
    }
}

因此,您有一个单一的接口和一个单一的函数名来从任何派生的类中获取坐标Coordinates。如果XCoordinate将来要添加类,则需要使用相同的名称实现相同的功能。当你将实例化XCoordinateYCoordinateWhateverCoordinate类时,你总是知道有一个GetCoordinateList你需要的函数

var coordClass = new XCoordinate();
coordClass.GetCoordinateList();

此外,当传递必须有GetCoordinateList成员的东西时,你可以在任何你喜欢的地方使用你的接口ICoordinate或抽象类坐标。

于 2013-04-29T19:40:08.447 回答
0

在这些线上:

_list.Add(new IYCoordinate(1, 5000));
_list.Add(new IYCoordinate(2, 100000));

您需要实例化从该类派生的Coordinates类的对象。在这里,您试图创建一个接口的新实例,但您不能这样做——接口成员没有定义!

创建一个子类Coordinates并实例化它。(或者abstract从这个类的定义中删除关键字——不清楚为什么它是抽象的,因为没有抽象成员。)

于 2013-04-29T19:24:35.113 回答
0

你至少需要一个具体的类。

目前你只有抽象类和接口。

于 2013-04-29T19:25:09.517 回答
0
  • 从 Blablaster 回答开始 - 在我看来,你不需要这里的界面。

    公共抽象类坐标 { public int CoordinatePriority { get; 放; } 公共 int 坐标 { 获取;放; }

    protected Coordinates(int CoordinatePriority, int Coordinate)
    {
        this.CoordinatePriority = CoordinatePriority;
        this.Coordinate = Coordinate;
    }
    
    public abstract List<Coordinates> GetCoordinateList();
    

    }

正如您所注意到的,我创建了一个抽象方法,而不是接口中的方法。每个派生类都必须实现此方法。

class YCoordinate:Coordinates
{
    public YCoordinate(int CoordinatePriority, int Coordinate) : 
        base(CoordinatePriority, Coordinate) //Call constructor of baseclass
    {
    }

    public override List<Coordinates> GetCoordinateList()
    {
        List<Coordinates> list = new List<Coordinates>();
        list.Add(new YCoordinate(1, 5000));
        list.Add(new YCoordinate(2, 100000));
        return list;
    }
}
于 2013-04-29T20:27:52.373 回答
0

简单的答案....更改这两行:

    _list.Add(new IYCoordinate(1, 5000));
    _list.Add(new IYCoordinate(2, 100000));

对此:

    _list.Add(new YCoordinate(1, 5000));
    _list.Add(new YCoordinate(2, 100000));

请注意,我在那里创建类,而不是界面。

长答案... Blabblablaster 做对了:-)

于 2013-04-29T20:03:01.653 回答