.net c#使用一个变量类型引用我的班级中的任何一个?

reference any one of a range of my Classes with one variable type?
2021-07-22
  •  译文(汉语)
  •  原文(英语)

对于C#来说,这是一个非常新的问题,对于大多数人来说可能是一个简单的问题,我对此几乎没有经验.

可以说我有一个为宇宙飞船创建和处理模型,控件,声音等的类,因此:

Ship myShip = new Ship();

创建了飞船,我可以看到它并且在那里一切都很好,我可以使用"ship"访问它的变量,并且我可以制造另一个并命名为其他东西..但是如果我有另一个用于..的战舰..为战斗机说另一个..战斗中的舰船的AI有一个"目标".所以我可以使用:

Ship target;

"目标"将引用当前目标,该目标可能会更改为舰船的另一个实例,但是我的问题是,是否存在一个可以处理所有这些类的变量类型,例如目标从舰船的实例切换为战舰.我会收到一个错误,它无法将Battleship类型转换为Ship.

如果没有这样的变量类型,是否有更简单的方法来代替可能成为目标的每种Class类型使用不同的变量呢?

只是万一即时消息不是很清楚..我基本上希望它能正常工作而不会出现错误:

WhatsThisHere target = new Ship();
target = new DifferentTypeOfShip();

谢谢!!

解决过程1

您可以做的一件事就是创建一个接口,所有船只都可以通过该接口实现.这样,您可以创建通用的函数/方法(请注意,接口不能在函数内实现任何代码,因为接口是抽象的),并在您的类中实现它们.例如.

public interface IShipInterface
{
    //any common properties here

    //any common methods/functions here
    void Target();
    void AnythingElseYouNeed();
}

public class Ship : IShipInterface
{
    //fields

    public Ship()
    {
        //constructor
    }

    public void Target()
    {
        //implement your ship Target method here
        throw new NotImplementedException();
    }

    public void AnythingElseYouNeed()
    {
        //implement other function code here
        throw new NotImplementedException();
    }
}

public class BattleShip : IShipInterface
{
    //fields 

    public BattleShip()
    {
        //constructor
    }

    public void Target()
    {
        //here is your battleship targetting, which could be completely different from your ship targetting.
        throw new NotImplementedException();
    }

    public void AnythingElseYouNeed()
    {
        throw new NotImplementedException();
    }
}

在这里,您可以像这样创建它们;

IShipInterface ship = new Ship();
IShipInterface battleShip = new BattleShip();

这具有优势,因为您可以通过ShipInterface类型引用所有船舶,例如在foreach循环中

foreach (IShipInterface ship in ShipList.OfType<BattleShip>())
        {
            //all your battleships
        }
速聊1:
非常感谢您的帮助(以及其他所有人:D),循环也将非常有用.
速聊2:
没问题:)如果您需要更多帮助,请随时提问!
解决过程2

使用接口并Ship从中继承.然后,您的目标不必仅是运送.

interface ITargetable
{
    //
}

class Ship : ITargetable
{
    //
}

在另一个类中,您将只使用ITargetable.

解决过程3

您可以建立

public class Ship {
   //Common behaviour and parameters for every ship in the world
}

public class BattleShip : Ship {
   // get basic Ship behavior and adds it's 
   // own battle specific properties and functions
}

所以在代码中,您喜欢:

Ship me = new BattleShip(); 
Ship enimy = new BattleShip(); 
me.Target = enimy;
解决过程4

让所有看起来像轮船的类型都源自Ship:

public class BattleShip : Ship
{

}

pretty new with C#, probably an easy question for most of you I just have little experience with this.

lets say i have a class which creates and handles the model, control, sound etc. for a space ship, so:

Ship myShip = new Ship();

Creates the Ship, and i can see it and all is good there, i can access its variables with 'ship', and i could make another and call it something else.. but if i had another Class which was for a.. Battleship.. say another for a fighter.. the AI for the ships in a battle have a 'target'. So i could use:

Ship target;

and 'target' will reference the current target which may change to another instance of Ship, but my question, is there a variable type which could handle any of these classes, like say the target switches from an instance of Ship, to a Battleship. I would get an error that it cant convert from type Battleship to Ship.

if there is no such variable type is there an easier way to do this instead of a different variable for each type of Class that may become a target?

and just incase im not real clear.. I basically want this to work without errors:

WhatsThisHere target = new Ship();
target = new DifferentTypeOfShip();

Thankss!!

Solutions1

One thing you could do is create an interface from which all of your ships implement. This way you can create common functions / methods (Note that the interface cannot implement any code within the functions, because an interface is abstract) and implement these in your classes. For example.

public interface IShipInterface
{
    //any common properties here

    //any common methods/functions here
    void Target();
    void AnythingElseYouNeed();
}

public class Ship : IShipInterface
{
    //fields

    public Ship()
    {
        //constructor
    }

    public void Target()
    {
        //implement your ship Target method here
        throw new NotImplementedException();
    }

    public void AnythingElseYouNeed()
    {
        //implement other function code here
        throw new NotImplementedException();
    }
}

public class BattleShip : IShipInterface
{
    //fields 

    public BattleShip()
    {
        //constructor
    }

    public void Target()
    {
        //here is your battleship targetting, which could be completely different from your ship targetting.
        throw new NotImplementedException();
    }

    public void AnythingElseYouNeed()
    {
        throw new NotImplementedException();
    }
}

From here you can create them like so;

IShipInterface ship = new Ship();
IShipInterface battleShip = new BattleShip();

This has advantages because all of your ships can then be referred to by type ShipInterface, for example in a foreach loop

foreach (IShipInterface ship in ShipList.OfType<BattleShip>())
        {
            //all your battleships
        }
Talk1:
Thank you very much for your help (and everyone else :D) Loop will also be very useful..
Talk2:
No problem : ) if you need more help, feel free to ask!
Solutions2

Use an interface and inherit Ship from it. Then your targets don't need to be ships only.

interface ITargetable
{
    //
}

class Ship : ITargetable
{
    //
}

In another class you would then only use ITargetable.

Solutions3

You can create

public class Ship {
   //Common behaviour and parameters for every ship in the world
}

public class BattleShip : Ship {
   // get basic Ship behavior and adds it's 
   // own battle specific properties and functions
}

so in the code you do like:

Ship me = new BattleShip(); 
Ship enimy = new BattleShip(); 
me.Target = enimy;
Solutions4

Let all types that look like a ship derive from Ship:

public class BattleShip : Ship
{

}
转载于:https://stackoverflow.com/questions/17361727/reference-any-one-of-a-range-of-my-classes-with-one-variable-type

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入