gpt4 book ai didi

c# - 泛型不能显式转换类型

转载 作者:行者123 更新时间:2023-11-30 15:54:34 26 4
gpt4 key购买 nike

作为背景,我主要使用 Java 编程,并尝试在 C# 项目中使用/学习泛型,但遇到了困难。

这是我的问题。从主要方法中你可以看到我正在尝试设置士兵任务,但我收到了错误,

cannot convert from 'TaskHeal' to 'TaskBase<SoldierBase>'

看起来这个转换应该有效,因为 TaskHeal 是 TaskBase 的子项,但事实并非如此。这是我的完整代码:

public class Main {
static void main(string[] args) {
SoldierMedic myMedic = new SoldierMedic();
myMedic.setTask(new TaskHeal(myMedic)); // Problem!
}
}


public class SoldierBase {

private TaskBase<SoldierBase> currentTask;
public int status;

public void setTask(TaskBase<SoldierBase> newTask) {
this.currentTask = newTask;
}
}


public class SoldierMedic : SoldierBase {
public int healRate = 45;
}


public abstract class TaskBase<T> where T : SoldierBase {

protected T soldier;

public TaskBase(T unit) {
this.soldier = unit;
this.soldier.status = 1;
}

public abstract void preformTask();
}


public class TaskHeal : TaskBase<SoldierMedic> {

public TaskHeal(SoldierMedic unit) : base(unit) { }

public override void preformTask() {
this.soldier.healRate++;
}
}

最佳答案

如果您不介意为 TaskBaseSoldierBase 添加一个额外的非泛型基类,您可以这样做:

class Program
{
static void Main(string[] args)
{
var myMedic = new SoldierMedic();
myMedic.setTask(new TaskHeal(myMedic)); // Problem!
}
}

public class SoldierBase
{
public int status;
}

public class SoldierBase<T> : SoldierBase where T : SoldierBase
{
private TaskBase currentTask;

public void setTask(TaskBase newTask)
{
this.currentTask = newTask;
}
}

public class SoldierMedic : SoldierBase<SoldierMedic>
{
public int healRate = 45;
}

public abstract class TaskBase
{

}

public abstract class TaskBase<T> : TaskBase where T : SoldierBase<T>
{
protected T soldier;

public TaskBase(T unit)
{
this.soldier = unit;
this.soldier.status = 1;
}

public abstract void preformTask();
}


public class TaskHeal : TaskBase<SoldierMedic>
{

public TaskHeal(SoldierMedic unit) : base(unit) { }

public override void preformTask()
{
this.soldier.healRate++;
}
}

如果您希望它看起来更像 c#(使用属性、适当的访问修饰符和大小写),您可以这样做:

class Program
{
static void Main(string[] args)
{
var myMedic = new SoldierMedic();
myMedic.CurrentTask = new TaskHeal(myMedic); // Problem!
}
}

public class SoldierBase
{
public int Status { get; set; }
}

public class SoldierBase<T> : SoldierBase where T : SoldierBase
{
public TaskBase CurrentTask { get; set; }
}

public class SoldierMedic : SoldierBase<SoldierMedic>
{
public int HealRate { get; set; } = 45;
}

public abstract class TaskBase
{

}

public abstract class TaskBase<T> : TaskBase where T : SoldierBase<T>
{
protected T Soldier;

public TaskBase(T unit)
{
Soldier = unit;
Soldier.Status = 1;
}

public abstract void PerformTask();
}


public class TaskHeal : TaskBase<SoldierMedic>
{
public TaskHeal(SoldierMedic unit) : base(unit) { }

public override void PerformTask()
{
Soldier.HealRate++;
}
}

关于c# - 泛型不能显式转换类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50092507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com