【问题标题】:Understanding why list is fixing abstract method type了解为什么 list 正在修复抽象方法类型
【发布时间】:2018-04-12 22:37:13
【问题描述】:

我用两个抽象方法创建了一个类。

在一种方法中,我根据抽象方法 List 中的返回类型的要求,在列表中返回我的泛型类型 U

在我的第二个方法中,我只是按照抽象方法中返回类型的要求返回 U,但我得到一个类型错误 错误 CS0029:无法隐式转换类型LB.HexSphereBuild.HexTile' toHexTile'

我不明白为什么这是一个隐式转换?特别是因为除了我使用 LB.HexSphereBuild 的公共抽象类 AStarFindPath 之外,所有类型都在 LB.HexSphereBuild 命名空间下;

请帮忙

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;
        }

        public void FindPath (Vector3 startPos, Vector3 targetPos)
        {
            // Get HexTile from start and end positions
            int startNodeIndex = _hexSphereObj.GetHexIndexFromPoint (startPos);
            HexTile startHex = _hexSphereObj.Hexes [startNodeIndex];
            int targetNodeIndex = _hexSphereObj.GetHexIndexFromPoint (targetPos);
            HexTile endHex = _hexSphereObj.Hexes [targetNodeIndex];

        }
    }

}

添加属性以防出现问题

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace LB.HexSphereBuild
{
    [System.Serializable]
    public class HexSphere
    {
        private List<HexTile> _hexes = new List<HexTile> ();
        // Data for the hexes. Read only
        public List<HexTile> Hexes {                             
            get{ return _hexes; }
        }
        // ....... other stuff
    }
}

添加数据类

namespace LB.HexSphereBuild
{
    public class HexTile : IAStarPathFinding<HexTile>
    {
        // Code .....
    }
}

【问题讨论】:

  • 尝试为通用类型定义约束。参考docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…。
  • 当我进行约束时,它只是告诉我我错过了一个强制转换错误 CS0266:无法隐式转换类型 LB.HexSphereBuild.HexTile' to HexTile'。存在显式转换(您是否缺少演员表?)
  • HexTile 是在哪里定义的?
  • 它在命名空间 LB.HexSphereBuild 下它自己的类中定义。我也会添加它

标签: c# unity3d


【解决方案1】:

您已经定义了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);
}

【讨论】:

  • 哈哈! .非常感谢 nvoigt
猜你喜欢
  • 2012-11-29
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
  • 2018-06-26
  • 1970-01-01
  • 2022-11-25
  • 2017-08-15
相关资源
最近更新 更多