加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 继承自非泛型LineSegmentClass的Generic Vector类

发布时间:2020-12-16 02:02:47 所属栏目:百科 来源:网络整理
导读:我正在尝试编写一个可以表示Forces和Dimensions的Vector类.通常这不是问题,因为大多数人都会创建一个名为Magnitude的属性,它返回一个double. Double不关心他们代表什么,他们代表力量或尺寸就好了.但是,我使用 this open-source library called Unit Class Li
我正在尝试编写一个可以表示Forces和Dimensions的Vector类.通常这不是问题,因为大多数人都会创建一个名为Magnitude的属性,它返回一个double. Double不关心他们代表什么,他们代表力量或尺寸就好了.但是,我使用 this open-source library called Unit Class Library来表示尺寸和力量.

这是我想做的事情.有没有办法让属性根据T返回不同的类型(构造函数中会发生同样的事情).是否有可能,如果是这样,怎么样?

public class Vector<T> : LineSegment
{
    ForceUnit _magnitudeForce;

    /// <summary>
    /// Returns the magnitude of the vector (equals the length of the line segment parent if T is Dimension,if T is Force,return variable )
    /// </summary>
    public T Magnitude
    {
        get {

            Type typeT = typeof(T);

            if (typeT == typeof(Dimension))
            {
                return base.Length;
            }
            else if (typeT == typeof(ForceUnit))
            {
                return _magnitudeForce;
            }

        ...
    }

或者,我可以使用动态关键字来实现这一点吗?

或者我应该在Unit Library之上创建一组这样的接口:

interface IDistance
{
    double Centimeters { get; }
    double Feet { get; }
    double Inches { get; }
    double Kilometers { get; }
    double Meters { get; }
    double Miles { get; }
    double Millimeters { get; }
    double Sixteenths { get; }
    double ThirtySeconds { get; }
    double Yards { get; }
}

interface IForceUnit
{
    double Kips { get; }
    double Newtons { get; }
    double Pounds { get; }
}

然后创建一个Hybrid类,实现两者作为Class的Magnitude.

我运行Unit Class Library因此欢迎提出应该在那里进行的任何更改.但请记住,库的直观性和单位转换能力是我想要保留的项目的价值.

解决方法

为什么不从Vector派生并丢弃作为Dimension的父Magnitude值?

有这样的东西?

public class Load:Vector
{

    private ForceUnit _magnitude;

    public ForceUnit Magnitude
    {
        get
        {
                return this._magnitude;          
        }
    }



    public PointLoad(ForceUnit magnitudeOfLoad,Vector directionVector)
        : base(...)
    {
        _magnitude = magnitudeOfLoad;
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读