简体   繁体   English

类型'foo.Phyl.DSpeed'已经定义了一个名为'DSpeed'的成员,它具有相同的参数类型

[英]Type 'foo.Phyl.DSpeed' already defines a member called 'DSpeed' with the same parameter types

While making a simple C# physics simulator, I ran across some complications and fixed all of them. 在制作一个简单的C#物理模拟器时,我遇到了一些并发症并修复了所有这些模拟器。 At the end another problem popped out; 最后又出现了另一个问题;

"Type 'Ball_and_gravity.Phyl.DSpeed' already defines a member called 'DSpeed' with the same parameter types" “类型'Ball_and_gravity.Phyl.DSpeed'已经定义了一个名为'DSpeed'且具有相同参数类型的成员”

It's located at the constructors. 它位于建设者。 I have no idea what to do. 我不知道该怎么做。

    public struct DSpeed : ISpeed, IAngleSpeed
    {
        public double Angle { get; set; }
        public double Speed { get; set; }
        public double XSpeed { get; set; }
        public double YSpeed { get; set; }

        public DSpeed(double ang, double sp)
        {
            Angle = ang;
            Speed = sp;

            XSpeed = Math.Sin(Angle) * Speed;
            YSpeed = Math.Cos(Angle) * Speed;
        }

        public DSpeed(double xs, double ys)
        {
            XSpeed = xs;
            YSpeed = ys;

            DPoint p1 = new DPoint(0, 0);
            DPoint p2 = new DPoint(XSpeed, YSpeed);
            Speed = p1.GetDistance(p2);
            Angle = Math.Atan2(XSpeed, YSpeed);
        }
    }

What's the problem here? 这有什么问题?

In this case, both methods use 2 doubles. 在这种情况下,两种方法都使用2个双打。 for the compiler is the same thing. 对于编译器来说是一回事。

for more info 了解更多信息

Member Overloading on .Net 会员在.Net上进行重载

Your proposed solution of using float parameters is not recommendable, as it makes the API of your struct very opaque to the client code. 您建议的使用float参数的解决方案不值得推荐,因为它使您的struct的API对客户端代码非常不透明。 Even if you're the only user of your library, you can almost be sure to be pulling your hair out a few months from now over why your code is failing, when you accidentally call the wrong constructor. 即使你是你图书馆的唯一用户,你几乎肯定会在几个月后拔掉你的代码失败的原因,当你不小心打电话给错误的构造函数时。 Not to mention the obvious reason for both of the types float and double existing, namely the difference in precision. 更不用说现有float和double两种类型的明显原因,即精度的差异。

You should consider introducing two named static factory methods to make it explicit how you're creating the instances. 您应该考虑引入两个命名的静态工厂方法,以明确您如何创建实例。 (You can probably come up with better names than the ones I've chosen below) (你可能会提出比我下面选择的更好的名字)

public struct DSpeed 
{
    public double Angle { get; private set; }
    public double Speed { get; private set; }
    public double XSpeed { get; private set; }
    public double YSpeed { get; private set; }

    private DSpeed(double angle, double speed, double xspeed, double yspeed)
    {
        Angle = angle;
        Speed = speed;
        XSpeed = xspeed;
        YSpeed = yspeed;
    }

    public static DSpeed FromAngle(double angle, double speed) 
    {
        var xspeed = Math.Sin(angle) * speed;
        var yspeed = Math.Cos(angle) * speed;
        return new DSpeed(angle, speed, xspeed, yspeed);
    }

    public static DSpeed FromXY(double xspeed, double yspeed)
    {
        DPoint p1 = new DPoint(0, 0);
        DPoint p2 = new DPoint(xspeed, yspeed);
        var speed = p1.GetDistance(p2);
        var angle = Math.Atan2(xspeed, yspeed);

        return new DSpeed(angle, speed, xspeed, yspeed);
    }
}

Also, I've made your property setters private, because mutable structs are evil 此外,我已将您的属性设置者设为私有,因为可变结构是邪恶的

暂无
暂无

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

相关问题 类型'Startup'已经定义了一个名为'Configuration'的成员,它具有相同的参数类型 - Type 'Startup' already defines a member called 'Configuration' with the same parameter types Web Api已经使用相同的参数类型定义了一个名为“ Get”的成员 - Web Api already defines a member called 'Get' with the same parameter types 错误已使用相同的参数类型定义了一个名为“索引”的成员 - Error already defines a member called 'Index' with the same parameter types 已经定义了一个使用相同参数类型调用的成员 c# - already defines a member called with the same parameter types c# 我该如何修复错误:Type Form1'已经定义了一个名为'Dispose'的成员,它具有相同的参数类型? - How can i fix the error: Type Form1' already defines a member called 'Dispose' with the same parameter types? C#:专用模板方法-错误:类型“…”已经定义了具有相同参数类型的成员“…” - C#: specialized template method - Error: Type '…' already defines a member called '…' with the same parameter types 错误 CS0111:“程序”类型已经定义了一个名为“Main”的成员,具有相同的参数类型 c# - error CS0111: Type 'Program' already defines a member called 'Main with the same parameter types c# 类型“Type”已经定义了一个名为“Equals”的成员,在 Equals(object) 上具有相同的参数类型 - Type 'Type' already defines a member called 'Equals' with the same parameter types on Equals(object) 已经使用相同的参数类型定义了一个名为“InitializeComponent”的成员 - Already defines a member called 'InitializeComponent' with the same parameter types 错误 - 已经使用相同的参数类型定义了一个名为“InitializeComponent”的成员 - Error - already defines a member called 'InitializeComponent' with the same parameter types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM