我用两个抽象方法创建了一个类。
在一种方法中,根据抽象方法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;
}
胡说叔叔
相关分类