C#继承覆盖参数

时间:2015-03-29 11:37:53

标签: c# inheritance

我有一个特定的经理"继承"经理", 我还有一个特定的对象"它继承了" BaseClass"

我尝试编码的是" BaseClass"物业与"经理"和特定对象"特定对象"使用"特定经理"。

的属性

现在可能很乱,所以我有一个UML类:

http://i.stack.imgur.com/pdbZk.jpg

以下是代码的基础:

class Program
{
    static void Main(string[] args)
    {
        SpecificManager ManagerFoo = new SpecificManager();
        SpecificManager2 ManagerBar = new SpecificManager2();
    }
}

public class Manager
{

    public void Manage()
    {
        // deal with BaseClass properties of the SpecificObject
        // myObject should be know as type of SpecificObject or SpecificObject2 depending of SpecificManager child
        myObject.isAlive = true;
    }
}

public class SpecificManager : Manager
{
    public SpecificObject myObjectSpec = new SpecificObject("Alfred");
    public void SpecificManage() 
    {
        // Manage Attributes of SpecificObject class
        myObjectSpec.level = 2;
        base.Manage();
    }
}
public class SpecificManager2 : Manager
{
    public SpecificObject2 myObjectSpec = new SpecificObject2("Alfred2");
    public void SpecificManage2()
    {
        // Manage Attributes of SpecificObject2 class
        myObjectSpec.description = "Foo";
        base.Manage();
    }
}

public class BaseClass
{
    public string gameObject;
    public bool isAlive;

    public BaseClass(string gameObjectStructure)
    {
        this.gameObject = gameObjectStructure;
    }
}

public class SpecificObject : BaseClass
{
    public int level = 1;
    public SpecificObject(string objectname) : base(objectname)
    {

    }
}

public class SpecificObject2 : BaseClass
{
    public string description = String.Empty;
    public SpecificObject2(string objectname) : base(objectname)
    {

    }
}

也许我想让它工作的方式不是正确的方法, 所以我收到一些建议真的很好!

谢谢,亲爱的帮手!

2 个答案:

答案 0 :(得分:1)

快速回答您关于继承如何工作的具体问题,您可以将public int level放入您的基类对象(@Sriram Sakthivel指出这不是一个好用的名称,因为它是c#中的大多数基础对象,然后两个子类都可以使用它。所以你的代码至少会在那时编译并运行。

这看起来如下所示(注意我已经将您的类名从Object更改为BaseClass,虽然比Object更好的名称仍然不能描述该类的功能,所以我不会&#39 ; t使用其中一个)

public class BaseClass
{
    public string gameObject;
    public bool isAlive;
    public int level;

    public BaseClass(string gameObjectStructure)
    {
        this.gameObject = gameObjectStructure;
        level = 1;
    }
}

另一个建议是,你不会养成使用公共成员的习惯,而是使用属性。这有几个好处,但其中一个主要的好处是你现在可以覆盖属性的设置方式。例如,如果SpecificObject1需要使用Level而不是SpecifictObject2执行不同的操作。

此更改将如下所示......

public class BaseClass
{
    private string gameObject;
    private bool isAlive;
    private int level;

    public BaseClass(string gameObjectStructure)
    {
        this.gameObject = gameObjectStructure;
        level = 1;
    }

    //don't include a set property to prevent anyone from changing the value
    public string GameObject { get { return gameObject; } }

    //"normal" property which includes a way to set and get the value
    public bool IsAlive { get { return isAlive; } set { isAlive = value; } }

    //Include virtual which allows the child classes to override the behavior
    public virtual Level { get { return level; } set { level = value; } }
}

public SpecificObject : BaseClass
{
    private string levelMessage;

    public SpecificObject(string objectname) : base(objectname)
    {
        //No need to call base.gameObject = objectName; because that is what
        //base(objectname) is doing for you.
        levelMessage = String.Empty;
    }

    //Contrived example showing overrides on a virtual method.
    public override Level 
    {
        get { return base.Level } 
        set { 
                base.Level = value;
                levelMessage = value.ToString();
            } 

    }
}

您可以采取其他措施来改进您的代码,但这应该让您从正确的方向开始,并解释您有关继承的原始问题。

答案 1 :(得分:0)

我可能找到了解决方案。

class Program
{
    static void Main(string[] args)
    {
        SpecificManager specificManager = new SpecificManager();
        SpecificManager2 specificManager2 = new SpecificManager2();
        //...
        specificManager.Manage();
        specificManager2.Manage();
    }
}

public class Manager
{
    dynamic myObject = null;
    public void ManagerInit (dynamic myObject)
    {
        this.myObject = myObject;
    }
    public void Manage()
    {
        if (myObject == null)
            return;
        // deal with BaseClass properties of the SpecificObject
        myObject.isAlive = true;
    }
}

public class SpecificManager : Manager
{
    public SpecificObject myObjectSpec = new SpecificObject("Alfred");
    public SpecificManager ()
    {
        base.ManagerInit(myObjectSpec);
    }
    public void SpecificManage()
    {
        // Manage Attributes of SpecificObject class
        myObjectSpec.level = 2;
        base.Manage();
    }
}
public class SpecificManager2 : Manager
{
    public SpecificObject2 myObjectSpec2 = new SpecificObject2("Alfred2");
    public SpecificManager2 ()
    {
        base.ManagerInit(myObjectSpec2);
    }
    public void SpecificManage2()
    {
        // Manage Attributes of SpecificObject2 class
        myObjectSpec2.description = "Foo";
        base.Manage();
    }
}

public class BaseClass
{
    public string gameObject;
    public bool isAlive;

    public BaseClass(string gameObjectStructure)
    {
        this.gameObject = gameObjectStructure;
    }
}

public class SpecificObject : BaseClass
{
    public int level = 0;
    public SpecificObject(string objectname) : base(objectname)
    {

    }
}

public class SpecificObject2 : BaseClass
{
    public string description = String.Empty;
    public SpecificObject2(string objectname) : base(objectname)
    {

    }
}

在Manager下完成的编译和更改在SpecificManagers下可见。 可怕的是,如果我在Manage方法下编写愚蠢的东西,那么代码在构建时不会大喊......