- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我遇到了上述非常简单的错误。我以为我会很快修复它,但即使经过相当多的搜索,我也无法终生找出问题所在。我有以下界面:
public interface ITemperatureEmitter
{
float CurrentTemperatureAddon { get; }
}
我在另外两个(暂时为空)接口(interface)中实现了这个:
public interface ITemperatureEmitterEnvironment : ITemperatureEmitter
public interface ITemperatureEmitterSphere : ITemperatureEmitter
随后我在下面的类中使用了这三个接口(interface):
using System.Collections.Generic;
using UnityEngine;
public class TemperatureReceiver : MonoBehaviour, ITemperatureReceiver
{
public float PerceivedTemperature;
// Serialized for debug purposes
[SerializeField] private List<ITemperatureEmitterSphere> temperatureEmitterSpheres;
[SerializeField] private List<ITemperatureEmitterEnvironment> temperatureEmitterEnvironments;
[SerializeField] private float environmentTemperature;
[SerializeField] private float temperatureToModifyBy;
[SerializeField] private float currentTemperatureAddon;
[SerializeField] private float appliedTemperatureAddon;
[SerializeField] private float totalTemperatureAddon;
private void Update()
{
UpdatePerceivedTemperature();
}
private void OnTriggerEnter(Collider other)
{
if (other.GetComponent<ITemperatureEmitterSphere>() != null)
{
temperatureEmitterSpheres.Add(other.GetComponent<ITemperatureEmitterSphere>());
}
else if (other.GetComponent<ITemperatureEmitterEnvironment>() != null)
{
temperatureEmitterEnvironments.Add(other.GetComponent<ITemperatureEmitterEnvironment>());
}
}
private void OnTriggerExit(Collider other)
{
if (other.GetComponent<ITemperatureEmitterSphere>() != null)
{
temperatureEmitterSpheres.Remove(other.GetComponent<ITemperatureEmitterSphere>());
}
else if (other.GetComponent<ITemperatureEmitterEnvironment>() != null)
{
temperatureEmitterEnvironments.Remove(other.GetComponent<ITemperatureEmitterEnvironment>());
}
}
private void UpdatePerceivedTemperature()
{
ModifyPerceivedTemperature(temperatureEmitterSpheres);
ModifyPerceivedTemperature(temperatureEmitterEnvironments);
}
private void ModifyPerceivedTemperature<ITemperatureEmitter>(List<ITemperatureEmitter> list)
{
if (list.Count > 0)
{
foreach (var item in list)
{
currentTemperatureAddon += item.CurrentTemperatureAddon;
}
currentTemperatureAddon = currentTemperatureAddon / list.Count;
appliedTemperatureAddon = PerceivedTemperature;
temperatureToModifyBy = currentTemperatureAddon = appliedTemperatureAddon;
PerceivedTemperature += temperatureToModifyBy;
}
}
}
现在 ModifyPercievedTemperature
方法中的 item.CurrentTemperatureAddon
发出“error CS1061: Type ITemperatureEmitter
does not contain a definition for CurrentTemperatureAddon
并且找不到类型为 ITemperatureEmitter
的扩展方法 CurrentTemperatureAddon
。是否缺少程序集引用?
ITemperatureEmitter
确实包含了 CurrentTemperatureAddonm
的定义...有人知道这里发生了什么吗?
最佳答案
您误报了 ModifyPerceivedTemperature
private void ModifyPerceivedTemperature<ITemperatureEmitter>(List<ITemperatureEmitter> list)
在这里你使用ITemperatureEmitter
作为通用参数的名称。所以在这个范围内,它不再意味着interface ITemperatureEmitter
, 但通用参数。
只需删除泛型参数,它无论如何都不会被使用:
private void ModifyPerceivedTemperature(List<ITemperatureEmitter> list)
现在你只需要一个 List<ITemperatureEmitter> list
作为参数和你所有的var item in list
确实是 ITemperatureEmitter
类型,而不是通用类型。
关于c# - Unity3d error CS1061 : Type x does not contain a definition for y. 但是确实如此吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51159389/
我是一名优秀的程序员,十分优秀!