调用this()构造函数和不同的base()构造函数

时间:2018-08-26 22:57:27

标签: c# class oop constructor base-class

我有一些类都扩展了父类。像这样:

public abstract class EasyUIELementFoundation
{
    protected GameObject UIElement;
    private RectTransform RectOptions;
    private Vector2 _position;
    private Vector2 _dimensions;

    protected Vector2 Position
    {
        get
        {
            return _position;
        }
        set
        {
            RectOptions.pivot = value;
            _position = value;
        }
    }

    protected Vector2 Dimensions
    {
        get
        {
            return _dimensions;
        }
        set
        {
            RectOptions.sizeDelta = value;
            _dimensions = value;
        }
    }

    protected EasyUIELementFoundation()
    {
        UIElement = new GameObject();
        RectOptions = UIElement.AddComponent(typeof(RectTransform)) as RectTransform;
    }

    protected EasyUIELementFoundation(Vector2 position) : this()
    {
        _position = position;
    }

    protected EasyUIELementFoundation(Vector2 position, Vector2 dimensions) : this()
    {
        _dimensions = dimensions;
        _position = position;
    }
}

public class EasyRawImage : EasyUIELementFoundation
{
    private RawImage UIImageComponent;
    private Texture2D _image;

    public Texture2D Image
    {
        get
        {
            return _image;
        }
        set
        {
            UIImageComponent.texture = value;
            _image = value;
        }
    }

    public EasyRawImage() : base()
    {
        UIImageComponent = UIElement.AddComponent(typeof(RawImage)) as RawImage;
    }

    public EasyRawImage(UnityEngine.Object image, Vector2 position) : this() : base(position)  
    // here is the error it is not possible to call the base.
    {
         //some code...
    }
}

所以我的问题是我有3个不同的基类构造函数,并且我想从派生类中调用不同的基类构造函数,但我不能atm。即使没有,我什至有可能这样做吗?

如果不清楚,请告诉我,以便我澄清。
我是OOP的新手,如果这是一个奇怪的问题,请对不起。

2 个答案:

答案 0 :(得分:1)

调用this()将隐式或显式调用基本构造函数。因此,base的另一次调用将是您第二次构造同一对象,这是不允许的。

您可以通过创建私有帮助器方法来共享构造函数的代码,如下所示:

public class EasyRawImage : EasyUIELementFoundation
{
    private RawImage UIImageComponent;
    private Texture2D _image;

    public Texture2D Image
    {
        get
        {
            return _image;
        }
        set
        {
            UIImageComponent.texture = value;
            _image = value;
        }
    }

    public EasyRawImage() : base()
    {
        InitUiImageComponent();
    }

    public EasyRawImage(UnityEngine.Object image, Vector2 position) : base(position)  
    {
        InitUiImageComponent();
         //some code...
    }
    private void InitUiImageComponent() {
        UIImageComponent = UIElement.AddComponent(typeof(RawImage)) as RawImage;
    }

}

在您的特定情况下,可以将初始化代码直接放入初始化程序中,因此无需使用helper方法:

public class EasyRawImage : EasyUIELementFoundation
{
    private RawImage UIImageComponent = UIElement.AddComponent(typeof(RawImage));
    private Texture2D _image;

    public Texture2D Image
    {
        get
        {
            return _image;
        }
        set
        {
            UIImageComponent.texture = value;
            _image = value;
        }
    }

    public EasyRawImage() : base()
    {
    }

    public EasyRawImage(UnityEngine.Object image, Vector2 position) : base(position)  
    {
         //some code...
    }

}

答案 1 :(得分:0)

您不能同时调用this和base构造函数。通常,最佳方法如注释中已经提到的那样,首先实现最复杂(具有最多参数)的构造函数并调用基类。然后,让更简单的构造函数使用默认值调用复杂的构造函数。 通过这种方法,您的班级将最终看起来像这样:

public class EasyRawImage : EasyUIELementFoundation
{
    private RawImage UIImageComponent;
    private Texture2D _image;

    public Texture2D Image
    {
        get
        {
            return _image;
        }
        set
        {
            UIImageComponent.texture = value;
            _image = value;
        }
    }

    public EasyRawImage() : this(default(UnityEngine.Object), default(Vector2))
    {
    }

    public EasyRawImage(UnityEngine.Object image, Vector2 position) : base(position)
    {
         UIImageComponent = UIElement.AddComponent(typeof(RawImage)) as RawImage;
         //some code...
    }
}