C# 无法从类型转换为其他类型

C# 无法从类型转换为其他类型,c#,.net,generics,C#,.net,Generics,我在OOP中练习,我创建了一个简单的控制台应用程序,其中我有3个派系,每个派系都必须有自己的军队,每个不同的军队可以包含特定的单位 我使用了接口和继承,但在泛型转换中存在一些问题。我想不出如何正确地解决这个问题,请帮帮我 这是代码 这是我的陆军和部队实施 public abstract class Unit<T> : IUnit<T> where T : Faction { public string UnitName { get; set; } publ

我在OOP中练习,我创建了一个简单的控制台应用程序,其中我有3个派系,每个派系都必须有自己的军队,每个不同的军队可以包含特定的单位

我使用了接口和继承,但在泛型转换中存在一些问题。我想不出如何正确地解决这个问题,请帮帮我

这是代码

这是我的
陆军
部队
实施

public abstract class Unit<T> : IUnit<T> where T : Faction {
    public string UnitName { get; set; }
    public IFaction Nation { get; set; }

    public Unit(string name) {
        UnitName = name;
    }

    public virtual void PrintUnitInfo() {
        Console.WriteLine($"UnitName: {UnitName}\nNation: {Nation}\n");
    }
}

public interface IUnit<T> where T : IFaction {
    string UnitName { get; set; }
    IFaction Nation { get; set; }

    void PrintUnitInfo();
}

public class Army<T> where T : IFaction {
    private List<IUnit<T>> units = new List<IUnit<T>>();

    public void Recruit(IUnit<T> unit) {
        units.Add(unit);
    }

    public void PrintFactionName() {
        Console.WriteLine(nameof(IFaction));
    }

}
public abstract class Faction : IFaction {
    public string Name { get; }
    public int Minerals { get; set; }
    public Army<Faction> Army { get; set; }

    public Faction(string name, Army<Faction> army) {
        Name = name;
        Minerals = 100;
        Army = army;
    }

    public virtual void Greetings() {
        Console.WriteLine("Printing FucntionInfo from BaseFaction");
    }

    public virtual void PrintUnitsInfo() {
        Console.WriteLine("Printing UnitsInfo from BaseFaction");
    }

}

public interface IFaction {

    string Name { get; }
    Army<Faction> Army { get; set; }
    int Minerals { get; set; }

    void Greetings();
    void PrintUnitsInfo();

}
最后,这里是编译器显示错误的用法:

无法从“陆军”转换为“陆军”。

publicstaticvoidmain(字符串[]args){
派系虫族=新虫族(“虫族”,新军队());
zerg.Army.printproductName();
Console.ReadKey();
}
}
公共级虫族:派系{
公共虫族(字符串名称,陆军):基地(名称,陆军){}
}

很抱歉,如果问题看起来非常大且令人困惑,但我无法用其他方式说明我的问题。

您从未在
IUnit
Unit
中使用
t
,也没有在您提供的其余代码中使用。因此,使用泛型是无用的

所以你可以简单地写:

static void Test()
{
  Faction zerg = new Zerg("Zerg", new Army());
  zerg.Army.Recruit(new Zealot("Zealot"));
  Console.ReadKey();
}

public interface IUnit
{
  string UnitName { get; set; }
  IFaction Nation { get; set; }

  void PrintUnitInfo();
}

abstract public class Unit : IUnit
{
  public string UnitName { get; set; }
  public IFaction Nation { get; set; }

  public Unit(string name)
  {
    UnitName = name;
  }

  public virtual void PrintUnitInfo()
  {
    Console.WriteLine($"UnitName: {UnitName}\nNation: {Nation}\n");
  }
}

public interface IFaction
{
  Army Army { get; set; }
  string Name { get; }
  int Minerals { get; set; }

  void Greetings();
  void PrintUnitsInfo();
}

public abstract class Faction : IFaction
{
  public string Name { get; }
  public int Minerals { get; set; }
  public Army Army { get; set; }

  public Faction(string name, Army army)
  {
    Name = name;
    Minerals = 100;
    Army = army;
  }

  public virtual void Greetings()
  {
    Console.WriteLine("Printing FucntionInfo from BaseFaction");
  }

  public virtual void PrintUnitsInfo()
  {
    Console.WriteLine("Printing UnitsInfo from BaseFaction");
  }
}

public class Army
{
  private List<IUnit> units = new List<IUnit>();

  public void Recruit(IUnit unit)
  {
    units.Add(unit);
  }

  public void PrintFactionName()
  {
    Console.WriteLine(nameof(IFaction));
  }
}

public class Zerg : Faction
{
  public Zerg(string name, Army army) : base(name, army) { }
}

public class Zealot : Unit
{
  public Zealot(string name) : base(name) { }
}
static void Test()
{
派系虫族=新虫族(“虫族”,新军队());
虫族军队新兵(新西兰狂热者);
Console.ReadKey();
}
公共接口单元
{
字符串UnitName{get;set;}
iAction Nation{get;set;}
作废printuninfo();
}
抽象公共类单位:IUnit
{
公共字符串UnitName{get;set;}
公共iAction国家{get;set;}
公共单位(字符串名称)
{
UnitName=名称;
}
公共虚拟空间printuninfo()
{
Console.WriteLine($“UnitName:{UnitName}\n选项:{Nation}\n”);
}
}
公共接口操作
{
陆军{get;set;}
字符串名称{get;}
int{get;set;}
无效问候语();
无效PrintUnitsInfo();
}
公共抽象类派系:IFaction
{
公共字符串名称{get;}
公共整数{get;set;}
公军{get;set;}
公共派系(字符串名称,陆军)
{
名称=名称;
矿物=100;
陆军=陆军;
}
公共虚拟空问候语()
{
Console.WriteLine(“从Baseption打印功能信息”);
}
公共虚拟void PrintUnitsInfo()
{
控制台。WriteLine(“来自Baseption的打印单元信息”);
}
}
公营军
{
私有列表单位=新列表();
公开招聘(IUnit单位)
{
单位。添加(单位);
}
public void printName()
{
Console.WriteLine(名称(iAction));
}
}
公共级虫族:派系
{
公共虫族(字符串名称,陆军):基地(名称,陆军){}
}
公共类狂热者:单位
{
公共狂热者(字符串名称):基(名称){}
}
现在,如果你想用泛型来做一些我不懂的事情,你可以用一张纸、一支铅笔和带线的矩形来画一个简单的草图,或者你可以用UML来完善你的设计

还有一个问题要问你:为什么要使用接口

要回答这个问题,可以问:我写的定义接口的代码,我用它做什么

泛型也是如此




您从未在
IUnit
Unit
中使用
T
,也没有在您提供的其余代码中使用。因此,使用泛型是无用的

所以你可以简单地写:

static void Test()
{
  Faction zerg = new Zerg("Zerg", new Army());
  zerg.Army.Recruit(new Zealot("Zealot"));
  Console.ReadKey();
}

public interface IUnit
{
  string UnitName { get; set; }
  IFaction Nation { get; set; }

  void PrintUnitInfo();
}

abstract public class Unit : IUnit
{
  public string UnitName { get; set; }
  public IFaction Nation { get; set; }

  public Unit(string name)
  {
    UnitName = name;
  }

  public virtual void PrintUnitInfo()
  {
    Console.WriteLine($"UnitName: {UnitName}\nNation: {Nation}\n");
  }
}

public interface IFaction
{
  Army Army { get; set; }
  string Name { get; }
  int Minerals { get; set; }

  void Greetings();
  void PrintUnitsInfo();
}

public abstract class Faction : IFaction
{
  public string Name { get; }
  public int Minerals { get; set; }
  public Army Army { get; set; }

  public Faction(string name, Army army)
  {
    Name = name;
    Minerals = 100;
    Army = army;
  }

  public virtual void Greetings()
  {
    Console.WriteLine("Printing FucntionInfo from BaseFaction");
  }

  public virtual void PrintUnitsInfo()
  {
    Console.WriteLine("Printing UnitsInfo from BaseFaction");
  }
}

public class Army
{
  private List<IUnit> units = new List<IUnit>();

  public void Recruit(IUnit unit)
  {
    units.Add(unit);
  }

  public void PrintFactionName()
  {
    Console.WriteLine(nameof(IFaction));
  }
}

public class Zerg : Faction
{
  public Zerg(string name, Army army) : base(name, army) { }
}

public class Zealot : Unit
{
  public Zealot(string name) : base(name) { }
}
static void Test()
{
派系虫族=新虫族(“虫族”,新军队());
虫族军队新兵(新西兰狂热者);
Console.ReadKey();
}
公共接口单元
{
字符串UnitName{get;set;}
iAction Nation{get;set;}
作废printuninfo();
}
抽象公共类单位:IUnit
{
公共字符串UnitName{get;set;}
公共iAction国家{get;set;}
公共单位(字符串名称)
{
UnitName=名称;
}
公共虚拟空间printuninfo()
{
Console.WriteLine($“UnitName:{UnitName}\n选项:{Nation}\n”);
}
}
公共接口操作
{
陆军{get;set;}
字符串名称{get;}
int{get;set;}
无效问候语();
无效PrintUnitsInfo();
}
公共抽象类派系:IFaction
{
公共字符串名称{get;}
公共整数{get;set;}
公军{get;set;}
公共派系(字符串名称,陆军)
{
名称=名称;
矿物=100;
陆军=陆军;
}
公共虚拟空问候语()
{
Console.WriteLine(“从Baseption打印功能信息”);
}
公共虚拟void PrintUnitsInfo()
{
控制台。WriteLine(“来自Baseption的打印单元信息”);
}
}
公营军
{
私有列表单位=新列表();
公开招聘(IUnit单位)
{
单位。添加(单位);
}
public void printName()
{
Console.WriteLine(名称(iAction));
}
}
公共级虫族:派系
{
公共虫族(字符串名称,陆军):基地(名称,陆军){}
}
公共类狂热者:单位
{
公共狂热者(字符串名称):基(名称){}
}
现在,如果你想用泛型来做一些我不懂的事情,你可以用一张纸、一支铅笔和带线的矩形来画一个简单的草图,或者你可以用UML来完善你的设计

还有一个问题要问你:为什么要使用接口

要回答这个问题,可以问:我写的定义接口的代码,我用它做什么

泛型也是如此




请,发布主代码,而不是图片!你从来不会在
IUnit
Unit
中使用
T
,也不会在你提供的其他代码中使用。因此使用泛型是没有用的……但是我如何指定没有泛型的某支军队的派系呢?好的,我会编辑它来绘制你的层次结构图,比如使用UML或仅仅是用纸和铅笔画长方形和线条。你会看到你设计的缺陷。我试过了,但每次都没能达到我想要的效果。请发布主代码,而不是图像!你从不在
IUnit
Unit
n中使用
T