いくつかのクラスがあり、他のクラス メソッドからサブクラスで定義されたプロパティにアクセスする際に問題があります。
と呼ばれる基本クラスとSection
いくつかのサブクラスがありSectionPlane : Section
ます。各サブクラスでは、異なるフィールドとプロパティのセットが定義されています (SectionPlane
ではプライベート フィールド_t
とパブリック プロパティt
が見つかりますが、SectionExtruded : Section
私はプライベート フィールド_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;
}
}
}
}
クラス ソリッド 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
か? 使用するセクションのすべてが同じプロパティを持っているわけではないため、これは非常に役立ちます。