简体   繁体   English

如何在C#中的构造函数中使用重载函数

[英]How to use a overload function in a constructor in c#

I want to create a geometry, based on different dimensions and a geometry flag, to determine if it is a cube or circle. 我想基于不同的尺寸和几何标记创建几何,以确定它是立方体还是圆形。 To do so I have to use overload functions, but I do not know how i can utilize these in a class function to store my input. 为此,我必须使用重载函数,但是我不知道如何在类函数中利用这些函数来存储输入。 Here is what I did so far: 这是我到目前为止所做的:

public void Object( double x, double y, double z)
        {
            name = "Cube";
            a = x;
            b = y;
            c = z;
        }
        public void Object(double r, double y)
        {
            name = "Cylinder";
            r1 = r;
            b = y;

        }

    protected double a{ get; private set; }
    protected double b{ get; private set; }
    protected double c{ get; private set; }
    protected double r1{ get; private set; }

First problem I have is, i can't use the declared variables multiple times, i have to to declare a variable for each possible object, in this case I can not save two variables on b , which is kinda ineffective. 我遇到的第一个问题是,我不能多次使用声明的变量,我必须为每个可能的对象声明一个变量,在这种情况下,我不能在b上保存两个变量,这有点无效。

My second problem is if i want to call the object in my dataclass like this along with other values it does not work: 我的第二个问题是,如果我想像这样与其他值一起调用数据类中的对象,它将不起作用:

public MeasureObject(double hash, string name, new Object obj(int n, different variables), double coordinates, ...)

{
 this.Hash = hash;
 this.Object=obj;
}

Is there a better way to implement a generic geometry in an object which can take an integer and n-different dimensions,lengths whatever? 有没有更好的方法可以在对象中实现通用几何,该对象可以采用整数和n个不同的尺寸,长度不限?

Geometric objects have enough differences between them that it would be better to have separate classes. 几何对象之间有足够的差异,最好有单独的类。

For example, calculating the volume of each is done differently. 例如,计算每个体积的方法是不同的。

One solution could be to have a base class from which to inherit and have a factory class that determines which geometrical object instance you need based on the flag. 一种解决方案可能是拥有一个要从其继承的基类,并拥有一个基于该标志确定您需要哪个几何对象实例的工厂类。

I would make a Cube and Cylinder class and an abstract class Geometry. 我将创建一个Cube and Cylinder类和一个Geometry抽象类。 You can then put the methods that are equal for both classes in the abstract class and override the others. 然后,您可以将两个类都相等的方法放在抽象类中,并覆盖其他方法。

    public abstract class Shape
    {
        public int Height;
        public int Width;
        public int Depth;
        public double Area()
        {
            return Height * Width;
        }
        public abstract double Volume();
    }
    class Square : Shape
    {
        public Square(int height, int width)
        {
            Height = height;
            Width = width;            
        }
        public override double Volume()
        {
            throw new NotImplementedException();
        }            
    }
    class Cube : Shape
    {
        public Cube(int height, int width, int depth)
        {
            Height = height;
            Width = width;        
            Depth = depth;    
        }
        public override double Volume()
        {
            return Height * Width * Depth;
        }            
    }

I would design my classes this way: 我将以这种方式设计班级:

namespace Geometry
{
    public interface IMeasurableSolid
    {
        double CalcSurface();
        double CalcVolume();
    }

    public class Sphere : IMeasurableSolid
    {
        public double Radius { get; set; }

        public Sphere(double radius)
        {
            if (radius <= 0)
            {
                throw new ArgumentOutOfRangeException("radius", "value must be positive");
            }
            this.Radius = radius;
        }

        public double CalcSurface()
        {
            return (Math.PI * 4 * Math.Pow(this.Radius, 2));
        }

        public double CalcVolume()
        {
            return (Math.PI * 4 * Math.Pow(this.Radius, 3) / 3);
        }
    }

    public class Cylinder : IMeasurableSolid
    {
        public double Height { get; set; }
        public double Radius { get; set; }

        public Cylinder(double height, double radius)
        {
            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException("height", "value must be positive");
            }
            if (radius <= 0)
            {
                throw new ArgumentOutOfRangeException("radius", "value must be positive");
            }
            this.Height = height;
            this.Radius = radius;
        }

        public double CalcSurface()
        {
            return 2 * Math.PI * this.Radius * (this.Radius + this.Height);
        }

        public double CalcVolume()
        {
            return this.Height * Math.PI * Math.Pow(this.Radius, 2);
        }
    }
}

You can even derive Sphere and Cylinder from a base abstract class as Maarten Zeeman suggested; 您甚至可以像Maarten Zeeman所建议的那样,从基本抽象类派生SphereCylinder I prefer using Interfaces because I think it is a more versatile solution. 我更喜欢使用接口,因为我认为它是一种更通用的解决方案。 Anyway you decide to do, please let me suggest you to conform to some best practices (you will find several examples here: https://www.roberthalf.com/technology/blog/4-best-practices-for-microsoft-net-framework-and-applications ). 无论您决定做什么,请让我建议您遵循一些最佳做法(您将在此处找到几个示例: https : //www.roberthalf.com/technology/blog/4-best-practices-for-microsoft-net -framework-and-applications )。 That is: 那是:

  • avoid calling a class Object . 避免调用类Object It is a dangerous name. 这是一个危险的名字。 It can be confused with object by a simple mistyping. 可以通过简单地将其与object混淆。 Call it Geometry , or Solid . 将其称为“ Geometry ”或“ Solid
  • choose explicit property names and uppercase them (Height, Length and Width are better than a, b, c). 选择显式的属性名称并将其大写(Height,Length和Width优于a,b,c)。

Keep your model as close as possible to the reality you want to represent. 使模型尽可能接近要表示的现实。 It will be easier to properly design and maintain your classes. 正确设计和维护您的班级会更容易。

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

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