繁体   English   中英

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

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

我想基于不同的尺寸和几何标记创建几何,以确定它是立方体还是圆形。 为此,我必须使用重载函数,但是我不知道如何在类函数中利用这些函数来存储输入。 这是我到目前为止所做的:

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; }

我遇到的第一个问题是,我不能多次使用声明的变量,我必须为每个可能的对象声明一个变量,在这种情况下,我不能在b上保存两个变量,这有点无效。

我的第二个问题是,如果我想像这样与其他值一起调用数据类中的对象,它将不起作用:

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

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

有没有更好的方法可以在对象中实现通用几何,该对象可以采用整数和n个不同的尺寸,长度不限?

几何对象之间有足够的差异,最好有单独的类。

例如,计算每个体积的方法是不同的。

一种解决方案可能是拥有一个要从其继承的基类,并拥有一个基于该标志确定您需要哪个几何对象实例的工厂类。

我将创建一个Cube and Cylinder类和一个Geometry抽象类。 然后,您可以将两个类都相等的方法放在抽象类中,并覆盖其他方法。

    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;
        }            
    }

我将以这种方式设计班级:

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);
        }
    }
}

您甚至可以像Maarten Zeeman所建议的那样,从基本抽象类派生SphereCylinder 我更喜欢使用接口,因为我认为它是一种更通用的解决方案。 无论您决定做什么,请让我建议您遵循一些最佳做法(您将在此处找到几个示例: https : //www.roberthalf.com/technology/blog/4-best-practices-for-microsoft-net -framework-and-applications )。 那是:

  • 避免调用类Object 这是一个危险的名字。 可以通过简单地将其与object混淆。 将其称为“ Geometry ”或“ Solid
  • 选择显式的属性名称并将其大写(Height,Length和Width优于a,b,c)。

使模型尽可能接近要表示的现实。 正确设计和维护您的班级会更容易。

暂无
暂无

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

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