【发布时间】:2015-12-23 19:43:02
【问题描述】:
我有以下课程:
public abstract class Tween : MonoBehaviour {
protected List<TweenItem> items = new List<TweenItem>();
}
public class TweenItem {
// Properties
}
public class ValueTweenItem : TweenItem {
// Properties
}
在这个类中,我试图覆盖items:
public class ValueTween : Tween{
protected List<ValueTweenItem> items = new List<ValueTweenItem>();
}
当我这样做时,我收到以下警告:
Assets/simple/Scripts/Util/Tween/ColorTween.cs(9,40):警告 CS0108:
Simple.Tween.ColorTween.items' hides inherited memberSimple.Tween.Tween.items'。如果要隐藏,请使用 new 关键字
我已尝试添加 override 和 virtual 关键字,但这不起作用。
我怎样才能让这个警告消失?
编辑
这是基地:
using UnityEngine;
using System.Collections.Generic;
namespace Simple.Tween{
public abstract class AbstractTween<T> : MonoBehaviour {
[SerializeField]
protected List<T> items = new List<T>();
}
public abstract class Tween : AbstractTween<TweenItem>{
// Common functions here
}
public class TweenItem {
public Component component;
public float duration = 2f;
public bool playOnStart = false;
[HideInInspector]
public float time = 0f;
[HideInInspector]
public bool complete = false;
[HideInInspector]
public bool runTween = false;
}
public class ValueTweenItem : AbstractTween<ValueTweenItem> {
public string propertyName;
public float startValue = 0f;
public float endValue = 1f;
}
public class ColorTweenItem : AbstractTween<ColorTweenItem> {
public Gradient gradient;
}
}
然后这是我正在使用的类:
using UnityEngine;
using System.Collections.Generic;
namespace Simple.Tween{
[AddComponentMenu("Simple/Tween/Value Tween")]
public class ValueTween : Tween{
[SerializeField]
protected List<ValueTweenItem> items = new List<ValueTweenItem>();
void Update(){
// Set the color of the object
foreach(ValueTweenItem item in items){
if(item.complete || item.runTween == false){
continue;
}
}
}
void TweenValue(Component item, string field, float value){
item.GetType().GetProperty(field).SetValue(item, value, null);
}
}
}
【问题讨论】:
-
好吧,你想要发生什么?默认情况下,
ValueTween上面会有一个items,因为它继承自Tween,所以不需要声明它。如果您真的想隐藏它,请使用new关键字。 -
item 现在是字段,您必须将其更改为 property public / protected virtual
-
我看到了您的编辑...您的问题是什么?
-
public class ValueTweenItem : AbstractTween<ValueTweenItem>如何编译? -
您的 TweenItem 类不是为了扩展抽象 Tween...这些项目看起来只是数据类。