简体   繁体   English

C#继承重写参数

[英]C# inheritance override a parameter

I have a "specific Manager" which inherits "Manager", I also have a "specific Object" which inherits "BaseClass" 我有一个继承“ Manager”的“特定管理器”,也有一个继承“ BaseClass”的“特定对象”

What I try to code is dealing "BaseClass" properties with "Manager" and dealing "specific Object" properties with "specific Manager". 我尝试编写的代码是将“ BaseClass”属性与“ Manager”一起处理,并将“特定对象”属性与“特定管理器”一起处理。

It might be messy now so I have a Class UML : 现在可能很乱,所以我有一个Class UML:

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

Here is the basic of the code: 这是基本的代码:

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)
    {

    }
}

Maybe the way I want this to work is not the right way to do, so I'm really fine getting some suggestions ! 也许我希望这种方法行之有效,所以我很高兴得到一些建议!

Thank you, dear helper ! 谢谢亲爱的助手!

The quick answer you to your specific question on how does inheritance work is that you could put public int level into your base class object (which @Sriram Sakthivel pointed out is not a good name to use for your class since it is the most base object in c#) and then both child classes would be able to use it. 对于有关继承如何工作的特定问题的快速解答是,您可以将public int级别放入基类对象中(@Sriram Sakthivel指出,该类不是用于您的类的好名字,因为它是最基础的对象在c#中),然后两个子类都可以使用它。 So your code would at least compile and run at that point. 因此,您的代码至少会在那时编译并运行。

This would look like the following (Note I've changed your class name from Object to BaseClass which while a better name than Object still isn't terribly descriptive for what the class does and so I wouldn't use either) 这看起来像下面的样子(注意,我已经将您的类名从Object更改为BaseClass,尽管比Object更好的名称仍然不能很好地描述该类的用途,所以我都不用)

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

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

Another suggestion would be that you not get into the habit of using public members in the first place and instead use properties. 另一个建议是您首先不要养成使用公共成员的习惯,而要使用属性。 This has several benefits but one of the main ones is that you can now override how the property is set. 这有几个好处,但主要好处之一就是您现在可以覆盖如何设置属性。 For example if SpecificObject1 needs to do something different with Level than SpecifictObject2 does. 例如,如果SpecificObject1需要对Level执行与SpecifictObject2不同的操作。

This change would look like the following... 此更改如下所示...

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();
            } 

    }
}

There are other things that you could do to improve your code but this should get you started in the right direction and explains your original question on inheritance. 您还可以采取其他措施来改进代码,但这应该可以帮助您朝正确的方向入手,并解释了有关继承的原始问题。

I might have find a solution. 我可能找到了解决方案。

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)
    {

    }
}

This compile and changes done under Manager are well seen under SpecificManagers. 在Manager下可以很好地看到此编译和所做的更改。 Terrible thing is that the code won't yell at build if I write stupids things under the Manage method... 糟糕的是,如果我在Manage方法下编写愚蠢的东西,代码将不会大喊大叫...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM