【发布时间】:2016-01-13 17:21:53
【问题描述】:
我正在尝试为 C# 中的小型 Windows 游戏创建一些类。我有一个带有构造函数的类 Weapon,它继承自我的基类 Item:
public class Weapon : Item
{
public int Attack { get; set; }
//constructor
public Weapon(int id, string name, int value, int lvl, int attack) :
base(id, name, value, lvl)
{
Attack = attack;
}
}
物品类:
public class Item
{
public int ID { get; set; }
public string Name { get; set; }
public int Value { get; set; }
public int Lvl { get; set; }
//constructor
public Item(int id, string name, int value, int lvl)
{
ID = id;
Name = name;
Value = value;
Lvl = lvl;
}
}
这一切都很好,我可以调用我的构造函数并创建 Weapon 对象的实例。但是,我还希望我的 Item 和 Weapon 类继承自 PictureBox 类,如下所示:
public class Item : PictureBox
{
public int ID { get; set; }
public string Name { get; set; }
public int Value { get; set; }
public int Lvl { get; set; }
//constructor
public Item(int id, string name, int value, int lvl)
{
ID = id;
Name = name;
Value = value;
Lvl = lvl;
}
}
但是应用上面的代码会导致错误“找不到类型'MyNamespace.Item'的构造函数”
我是否以正确的方式处理这个问题?如何让我的 Item 基类从另一个基类继承?
为什么它试图在表单设计器中打开我的类文件?我不明白!
【问题讨论】:
-
我强烈建议使用
Bitmap属性而不是覆盖PictureBox -
武器不能替代 PictureBox。我认为您可能应该重新考虑您的设计。
-
Picturebox 拥有我需要的所有属性。我只有一天的时间来创建它,所以只要它有效,整体设计并不重要。不过,我会看一下 Bitmap 属性。谢谢
-
我假设你只是在这里使用图片框,所以它实际上是一个 ItemPictureBox、WeaponPictureBox 等。
标签: c# .net inheritance