C#访问一个子类属性 [英] C# Accessing a subclass property

查看:166
本文介绍了C#访问一个子类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个班,我在访问中与其他类中的方法子类中定义的属性问题。

I have a few classes and I am having problems accessing properties defined in subclasses from other class methods.

我有一个称为基类和几个子类,例如: SECTIONPLANE:科。在每个子类中,一组不同的字段和属性的定义(以 SECTIONPLANE ,私有字段 _t 和公共财产 T 可以发现,而在 SectionExtruded:科我的私人领域 _A 和公共财产'A')。

I have a base class called Section and a few subclasses, e.g. SectionPlane : Section. In each subclass, a different set of fields and properties are defined (in SectionPlane, private field _t and public property t can be found, whereas in SectionExtruded : Section I have private field _A and public property ´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;
        }
    }
}

时,会出现问题,当我从类元素的任何子类试图访问属性,因为theese没有在基类部分中设置,例如:在元素 Solid2D:元素

The problem occurs when I from any subclass of the class Element tries to access the properties, since theese are not set in the base class Section, e.g. in the element 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;
            }
        }
    }
}

类固体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 ,而无需将其包含在基类中?这将是非常有益的,因为不是所有的我将使用具有相同的属性的部分。

Is there any way to access my property t without having to include it in the base class Section? This would be very helpfull since not all of the sections that I will use have the same properties.

推荐答案

既然你知道,它只能是一个 SECTIONPLANE ,你可以将它转换

Since you know that it can only be a SectionPlane you can cast it

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

如果你不知道你是否有合适类型的部分,您可以使用关键字

If you are not sure whether you have a section of the right type, you can use the as keyword

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

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

as does not throw an exception if the section has another type, but returns null instead.

或者你可以简单地测试

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

不过这比使用,那么优雅,因为你必须测试的类型,然后将它转换;但铸造确实又在内部此项测试。

but this is less elegant than using as, since you have to test the type and then cast it; but casting does this test again internally.

这篇关于C#访问一个子类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆