我用两个抽象方法创建了一个类。在一种方法中,根据抽象方法List中的返回类型的要求,在列表中返回通用类型U。在我的第二种方法中,我只是按抽象方法中返回类型的要求返回U,但出现类型错误错误CS0029:无法隐式转换类型LB.HexSphereBuild.HexTile' toHexTile'我不明白为什么这是隐式转换?特别是因为所有类型都在LB.HexSphereBuild命名空间下,除了我的使用LB.HexSphereBuild的公共抽象类AStarFindPath之外;请帮忙using System.Collections;using System.Collections.Generic;using UnityEngine;using LB.Managers;using LB.HexSphereBuild;namespace LB.AStarPathFinding{ public abstract class AStarFindPath<T,U> : MonoBehaviour{ public virtual void FindPath<T>(Vector3 startPos, Vector3 targetPos) { } // Method 1 public abstract U NodeFromWorldPoint<T,U> (Vector3 worldPos); // Method 2 public abstract List<U> GetNodeNeighbours<U> (U node); }}这是继承我的抽象类的类using System.Collections;using System.Collections.Generic;using UnityEngine;using LB.Managers;using LB.AStarPathFinding;namespace LB.HexSphereBuild{ [RequireComponent (typeof(PlanetBuildManager))] public class AStarHexSphere: AStarFindPath<Hexsphere,HexTile> { private HexSphere _hexSphereObj; void Awake () { // Reference to HexSphere _hexSphereObj = gameObject.GetComponent<PlanetBuildManager> ().hexSphereObj; } override public HexTile NodeFromWorldPoint<HexSphere,HexTile> (Vector3 worldPos ) { int startNodeIndex = _hexSphereObj.GetHexIndexFromPoint (worldPos); int startNodeIndex = _hexSphereObj.GetHexIndexFromPoint (worldPos); HexTile startHex = _hexSphereObj.Hexes[startNodeIndex];// error CS0029: Cannot implicitly convert type `LB.HexSphereBuild.HexTile' to `HexTile' return startHex; } override public List<HexTile> GetNodeNeighbours<HexTile> (HexTile node ) { List<HexTile> neighbours = new List<HexTile>(); List<int> hexNeighboursIndices = new List<int>(); int hexIndex =0; _hexSphereObj.GetNeighbors (hexIndex, ref hexNeighboursIndices); return neighbours; }
1 回答
胡说叔叔
TA贡献1804条经验 获得超8个赞
您已经定义了T和U,再次在方法中定义它们意味着T所有方法中的可能是不同的类型。那不是您想要的,删除方法上的泛型:
public abstract class AStarFindPath<T,U> : MonoBehaviour
{
public virtual void FindPath(Vector3 startPos, Vector3 targetPos)
{
}
// Method 1
public abstract U NodeFromWorldPoint(Vector3 worldPos);
// Method 2
public abstract List<U> GetNodeNeighbours(U node);
}
- 1 回答
- 0 关注
- 156 浏览
添加回答
举报
0/150
提交
取消