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

C# 如何在c中的构造函数中使用重载函数#,c#,object,overloading,derived-class,C#,Object,Overloading,Derived Class,我想根据不同的尺寸和几何标志创建一个几何体,以确定它是立方体还是圆。为此,我必须使用重载函数,但我不知道如何在类函数中使用这些函数来存储输入。以下是我迄今为止所做的: public void Object( double x, double y, double z) { name = "Cube"; a = x; b = y; c = z; } publ

我想根据不同的尺寸和几何标志创建一个几何体,以确定它是立方体还是圆。为此,我必须使用重载函数,但我不知道如何在类函数中使用这些函数来存储输入。以下是我迄今为止所做的:

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个不同的维度、长度等?

几何对象之间有足够的差异,因此最好使用单独的类

例如,计算每个容器的体积是不同的


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

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

例如,计算每个容器的体积是不同的


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

我将创建一个立方体和圆柱体类以及一个抽象几何类。然后,可以将两个类的相等方法放在抽象类中,并重写其他方法

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

我将创建一个立方体和圆柱体类以及一个抽象的几何类。然后,可以将两个类的相等方法放在抽象类中,并重写其他方法

    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);
        }
    }
}
名称空间几何体
{
公共接口IMeasurableSolid
{
双CalcSurface();
双CalcVolume();
}
公共类球体:IMeasurableSolid
{
公共双半径{get;set;}
公共球体(双半径)
{

如果(radius我会这样设计我的类:

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);
        }
    }
}
名称空间几何体
{
公共接口IMeasurableSolid
{
双CalcSurface();
双CalcVolume();
}
公共类球体:IMeasurableSolid
{
公共双半径{get;set;}
公共球体(双半径)
{

如果(半径)如果对象不同,则应在单独的类中处理它们。泛型应用于以相同方式处理不同类型的代码。如果对象不同,则应在单独的类中处理它们。泛型应用于以相同方式处理不同类型的代码。我承认这是正确的。但c我可以使用这样一个类并保持输入的通用性吗?例如,我只使用一个函数,该函数为相应的几何体提供一个整数标志,以及x值?@DerBenutzer到底会有什么问题?在一种情况下,您首先使用Object\u Object=Cube(1,2,3),在另一种情况下,您使用Object second\u Object=Cylinder(4,5).project third\u Object(1,2,3)//这不可能,因为几何体将从数据库中读取。类似的是,将有10000个输入,以及它们应该如何知道是否应该调用cube()或columer()。他们需要一个标志来调用某个方法。@DerBenutzer:然后你创建一个工厂类;工厂类的目的是从输入中找出它需要创建什么样的类。我承认这是真的。但是我如何使用这样一个类并使输入保持通用性呢?例如,我只使用一个函数,该函数为e对应的几何体,以及x值?@DerBenutzer问题到底是什么?在一种情况下,你使用Object first\u Object=Cube(1,2,3),在另一种情况下使用Object second\u Object=Cylinder(4,5)。比Object third\u Object(1,2,3)可读性强得多//这是不可能的,因为几何体将从数据库中读取。这就像有10000个输入,他们如何知道应该调用cube()还是圆柱体()。他们需要一个标志来调用某个方法。@DerBenutzer:然后创建一个工厂类;工厂类的目的是从输入中找出它需要创建什么样的类。