繁体   English   中英

C#访问子类属性

[英]C# Accessing a subclass property

我有几个类,我遇到从其他类方法访问子类中定义的属性时遇到问题。

我有一个名为Section的基类和一些子类,例如SectionPlane : Section 在每个子类中,定义了一组不同的字段和属性(在SectionPlane ,可以找到私有字段_t和公共属性t ,而在SectionExtruded : Section I中有私有字段_A和公共属性'A')。

课程部分

// General section object
public abstract class Section
{
    public Section()
    {}
}

类平面部分

// Section object representing a plane with thickness t
public class SectionPlane : Section
{
    private double _t;

    public SectionPlane(double t)
    {
        this.t = t;
    }

    public double t
    {
        get
        {
            return _t;
        }
        set
        {
            _t = value;
        }
    }
}

类挤压部分

// Section object of some geometry with cross section area A extruded along the element it is assigned to.
public class SectionExtruded : Section
{
    private double _A;

    public SectionExtruded(double A)
    {
        this.A = A;
    }

    public double A
    {
        get
        {
            return _A;
        }
        set
        {
            _A = value;
        }
    }
}

当我从类Element任何子类尝试访问属性时,会出现问题,因为没有在基类Section ,例如在元素Solid2D : Element

类元素

public abstract class Element
{
    private Section _section;

    public Element(Section section)
    {
        this.section = section;
    }

    public Section section
        {
            get 
            {
                return _section;
            }
            set
            {
                _section = value;
            }
        }
    }
}

类Solid 2D元素

// Solid2D elements can only have sections of type SectionPlane
public class Solid2D : Element
{
    public Solid2D(SectionPlane section)
        : base(section)
    {
    }

    public void Calc()
    {
        double t = section.t;    // This results in error 'Section' does not contain a definition for 't' and no extension method 't' accepting a first argument of type 'Section' could be found (are you missing a using directive or an assembly reference?)
    }
}

酒吧元素

// Bar elements can only have sections of type SectionExtruded
public class Solid2D : Element
{
    public Solid2D(SectionExtruded section)
        : base(section)
    {
    }

    public void Calc()
    {
        double A = section.A;    // This results in error 'Section' does not contain a definition for 'A' and no extension method 'A' accepting a first argument of type 'Section' could be found (are you missing a using directive or an assembly reference?)
    }
}

有没有办法访问我的属性t而不必将它包含在基类Section 这将非常有用,因为并非我将使用的所有部分都具有相同的属性。

既然你知道它只能是一个SectionPlane就可以投射它

double t = ((SectionPlane)section).t;

如果您不确定是否有正确类型的部分,则可以使用as关键字

double t = 0;
var sectionPane = section as SectionPlane;
if (sectionPane != null) {
    t = sectionPane.t;
}

as不抛出一个异常,如果该部分有另一种类型,但返回null来代替。

或者你可以简单测试一下

double t = 0;
if(section is SectionPlane) {
    t = ((SectionPlane)section).t;
}

但这不如使用as那么优雅,因为你必须测试类型然后再进行投射; 但是内部再次进行测试。

使用C#7.0中引入的新模式匹配,您可以编写:

double t = 0;
if(section is SectionPlane sp) {
    t = sp.t;
}

但是,如果你必须进行这样的测试,那么问题是,你的方法在面向对象的意义上是否正确。 如果将Calc方法移动到抽象类Section并让每个类执行自己的计算,则不需要进行类型测试或转换。

Section

public abstract void Calc();

SectionPlane

public override void Calc()
{
    var x = t;
}

...

section.Calc();  // No casting or type test required.

我会说,把你的属性“t”放在Section基类中,如果它属于那里。

以下是一些选项:

  1. Element.section更改为SectionPlane
  2. Calc()中将sectionSectionPlane
  3. 添加tSection
  4. 创建一个具有t属性的接口(例如IHasT ),使SectionPlane实现它,并将Element.section更改为IHasT
  5. (如果适用)将public abstract class Element更改为public abstract class Element<T> where T : Section ,将section更改为类型T ,并Solid2D : Element<SectionPlane>

如果您知道元素继承者将使用的节的类型,我将使元素基类成为通用的:

 public abstract class Element<SectionType>
     where SectionType:Section
 {
    //....
 }

您可以使用基类包含默认元素类型:

 public abstract class Element:Element<Section>
 {
    //....
 }

并且已知元素可以继承强类型:

 public class Solid2D : Element<SectionPlane>

暂无
暂无

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

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