<BaseClass> 列表和访问子变量
List of <BaseClass> and accessing childs variables
我正在创建一个对话系统,我想使用多态性来简化工作,所以我有一个 class 那就是包含列表 dialogue_tree 的对话树......然后我有TextBoxNPC、TextBoxText、TextBoxItem 等,因此如果我愿意,我可以让每个页面显示不同类型的对话框。每个 TextBox 都包含它是什么类型的文本框的枚举,当我想更改不在基础 class.
中的变量时,问题就出现了
public class Dialogue_Tree
{
List<TextBox> dialogues = new List<TextBox>();
public Dialogue_Tree()
{
dialogues.Add(new TextBoxNPC());
dialogues[0].
}
}
如果在 textboxnpc 中我有 Sprite npc_portrait 我无法更改它,因为它被视为文本框。
所以即使我施放它,我也会得到一个 null 异常,无论如何都可以这样做吗?
我唯一的其他选择是只有一个 class 文本框,它具有每种不同类型对话所需的每个变量,但每个文本框页面有 35 个未使用的变量,这似乎是一种资源浪费.
您对多态性的理解颠倒了。如果你有一个使用这个多态的绘图程序:
abstract class Shape {
public int X = 0;
public int Y = 0;
}
class Rectangle:Shape {
public int Width = 10;
public int Height = 20;
}
class Circle:Shape {
public int Diameter = 50;
}
您不会在 parent class 中查看 child 的类型并做一些不同的事情:
foreach(Shape s in _listOfShapes){
if(s is Circle c)
myCanvas.DrawCircle(c.X,c.Y,c.Diameter);
else if(s is Rectangle r)
myCanvas.DrawRectangle(c.X,c.Y,r.Width,r.Height);
}
你创造了一种方法来对所有形状一视同仁:
abstract class Shape {
public int X = 0;
public int Y = 0;
abstract void DrawYourself(Canvas c)
}
class Rectangle:Shape {
public int Width = 10;
public int Height = 20;
override DrawYourself(Canvas c){
c.DrawRectangle(base.X,base.Y,this.Width,this.Height);
}
}
class Circle:Shape {
public int Diameter = 50;
override DrawYourself(Canvas c){
c.DrawCircle(base.X,base.Y,this.Diameter);
}
}
现在所有形状都知道如何在 canvas(parent 拥有)上绘制自己,您将它们都视为普通旧形状:
foreach(Shape s in _listOfShapes){
s.DrawYourself(myCanvas);
}
我正在创建一个对话系统,我想使用多态性来简化工作,所以我有一个 class 那就是包含列表 dialogue_tree 的对话树......然后我有TextBoxNPC、TextBoxText、TextBoxItem 等,因此如果我愿意,我可以让每个页面显示不同类型的对话框。每个 TextBox 都包含它是什么类型的文本框的枚举,当我想更改不在基础 class.
中的变量时,问题就出现了public class Dialogue_Tree
{
List<TextBox> dialogues = new List<TextBox>();
public Dialogue_Tree()
{
dialogues.Add(new TextBoxNPC());
dialogues[0].
}
}
如果在 textboxnpc 中我有 Sprite npc_portrait 我无法更改它,因为它被视为文本框。 所以即使我施放它,我也会得到一个 null 异常,无论如何都可以这样做吗?
我唯一的其他选择是只有一个 class 文本框,它具有每种不同类型对话所需的每个变量,但每个文本框页面有 35 个未使用的变量,这似乎是一种资源浪费.
您对多态性的理解颠倒了。如果你有一个使用这个多态的绘图程序:
abstract class Shape {
public int X = 0;
public int Y = 0;
}
class Rectangle:Shape {
public int Width = 10;
public int Height = 20;
}
class Circle:Shape {
public int Diameter = 50;
}
您不会在 parent class 中查看 child 的类型并做一些不同的事情:
foreach(Shape s in _listOfShapes){
if(s is Circle c)
myCanvas.DrawCircle(c.X,c.Y,c.Diameter);
else if(s is Rectangle r)
myCanvas.DrawRectangle(c.X,c.Y,r.Width,r.Height);
}
你创造了一种方法来对所有形状一视同仁:
abstract class Shape {
public int X = 0;
public int Y = 0;
abstract void DrawYourself(Canvas c)
}
class Rectangle:Shape {
public int Width = 10;
public int Height = 20;
override DrawYourself(Canvas c){
c.DrawRectangle(base.X,base.Y,this.Width,this.Height);
}
}
class Circle:Shape {
public int Diameter = 50;
override DrawYourself(Canvas c){
c.DrawCircle(base.X,base.Y,this.Diameter);
}
}
现在所有形状都知道如何在 canvas(parent 拥有)上绘制自己,您将它们都视为普通旧形状:
foreach(Shape s in _listOfShapes){
s.DrawYourself(myCanvas);
}