【问题标题】:C# Object InheritanceC# 对象继承
【发布时间】:2013-01-04 19:41:06
【问题描述】:

我正在尝试在 c# 中创建一个可以扩展到子类的基类。

例如:

public class ObjectsInTheSky 
{
    public string Size, Shape;
    public float Mass;
    public int DistanceFromEarth;
    public bool hasAtmosphere, hasLife;
    public enum ObjectTypes {Planets,Stars,Moons}

    public ObjectsInTheSky( int id ) 
    {
        this.Load( id );
    }
    public void Load( int id) 
    {
        DataTable table = Get.DataTable.From.DataBase(id);

        System.Reflection.PropertyInfo[] propInfo = this.GetType().GetProperties();
        Type tp = this.GetType();
        foreach (System.Reflection.PropertyInfo info in propInfo)
        {
            PropertyInfo p = tp.GetProperty(info.Name);
            try
            {
                if (info.PropertyType.Name == "String")
                {
                    p.SetValue(this, table.Rows[0][info.Name].ToString(), null);
                }
                else if (info.PropertyType.Name == "DateTime")
                {
                    p.SetValue(this, (DateTime)table.Rows[0][info.Name], null);
                }
                else
                {
                    p.SetValue(this, Convert.ToInt32(table.Rows[0][info.Name]), null);
                }
            }
            catch (Exception e) 
            {
                Console.Write(e.ToString());
            }
        }
    }
}

public class Planets : ObjectsInTheSky 
{
    public Moons[] moons;
}

public class Moons : ObjectsInTheSky 
{

}

public class Stars : ObjectsInTheSky 
{
    public StarTypes type;
    public enum StarTypes {Binary,Pulsar,RedGiant}
}

我的问题是当我尝试使用一个对象时:

Stars star = new Stars(142);

star.type 不存在和star 的属性,它作为star.star.type 存在但完全无法访问,或者我不知道如何访问它。

我不知道我是否正确扩展了 ObjectsInTheSky 属性。任何帮助或指示将不胜感激。

【问题讨论】:

  • 你的Stars类只有一个无参数的构造函数,编译器默认提供。
  • 只是未来的一点,@user1224302,Load 的代码不是必需的(即只需在方法的主体中添加类似 /* stuff loaded here */ 的内容)- 人们不需要总是费心向下滚动代码示例(即使我们确实应该这样做)。尽管如此,实际上拥有代码总比没有代码要好 - 所以你已经比 50% 的新用户做得好 100% ;)

标签: c# inheritance base-class


【解决方案1】:

您似乎正在尝试使用未在您的子类 Stars 或基类上定义的构造函数。

Stars star = new Stars(142);

如果您尝试使用.Load(int) 方法,那么您需要这样做:

Stars star = new Stars();
star.Load(142);

或者,如果您尝试使用基础构造函数,则需要在子类中定义它:

public class Stars : ObjectsInTheSky 
{
    public Stars(int id) : base(id) // base class's constructor passing in the id value
    {
    }

    public Stars()  // in order to not break the code above
    {
    }

    public StarTypes type;
    public enum StarTypes {Binary,Pulsar,RedGiant}
}

【讨论】:

  • 他可能只是想为所有子类型添加额外的构造函数,因为Load 可能不应该公开。
  • ...或在Stars 中添加一个int 构造函数,该构造函数在ObjectsInTheSky 中与相同的通道
  • 谢谢,我也添加了子类型的构造函数的创建。
  • cool +1 then - 尽管值得指出的是,如果遵循更新的代码,那么除非还添加了默认构造函数,否则您的第二个代码块将无法使用:)
  • @AndrasZoltan 再次感谢,添加了另一个构造函数和评论。
【解决方案2】:

C# 中的构造函数不是继承的。您需要为每个基类添加额外的构造函数重载:

public class Stars : ObjectsInTheSky 
{
    public Stars(int id) : base(id) { }

    public StarTypes type;
    public enum StarTypes {Binary,Pulsar,RedGiant}
}

这将创建一个构造函数,它只为您调用基类的构造函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    相关资源
    最近更新 更多