C# 如何将值传递给构造函数?
对不起,我的问题有点理论性 我是OOP新手,正在学习以下代码C# 如何将值传递给构造函数?,c#,oop,C#,Oop,对不起,我的问题有点理论性 我是OOP新手,正在学习以下代码 public interface IShape { double getArea(); } public class Rectangle : IShape { int lenght; int width; public double getArea() { return lenght * width; } } public class
public interface IShape
{
double getArea();
}
public class Rectangle : IShape
{
int lenght;
int width;
public double getArea()
{
return lenght * width;
}
}
public class Circle : IShape
{
int radius;
public double getArea()
{
return (radius * radius) * (22 / 7);
}
}
public class SwimmingPool
{
IShape innerShape;
IShape outerShape;
SwimmingPool(IShape _innerShape, IShape _outerShape)
{
//assignment statements and validation that poolShape can fit in borderShape;
}
public double GetRequiredArea()
{
return outerShape.getArea() - innerShape.getArea();
}
}
此代码计算不同形状的面积。我可以看到SwimingPool类的构造函数,但我不知道如何将值传递给构造函数。我以前没有使用接口编程过。请告诉我3件事:
如何在设计时传递值。
当两个参数都可以是任何类型时,如何在运行时传递值。
这里如何以面向对象的方式进行验证?
谢谢你的时间和帮助。就这样做吧
SwimmingPool(IShape innerShape, IShape outerShape)
{
this.innerShape = innerShape;
this.outerShape = outerShape;
this.Validate();
}
private void Validate()
{
// or some other condition
if ( GetRequiredArea() < 0 ){
throw new Exception("The outer shape must be greater than the inner one");
}
}
if (shape1 is Circle) //...
就这样做吧
SwimmingPool(IShape innerShape, IShape outerShape)
{
this.innerShape = innerShape;
this.outerShape = outerShape;
this.Validate();
}
private void Validate()
{
// or some other condition
if ( GetRequiredArea() < 0 ){
throw new Exception("The outer shape must be greater than the inner one");
}
}
if (shape1 is Circle) //...
嗯,您正在使用接口,因此在SwimmingPool类中,构造函数将需要两个IShape参数。由于您需要一个实现来使用您的接口,例如矩形和圆形,您只需执行以下操作:
class Pool
{
private IShape _InnerShape;
private IShape _OuterShape;
public Pool(IShape inner, IShape outer)
{
_InnerShape = inner;
_OuterShape = outer;
}
public double GetRequiredArea()
{
return _InnerShape.GetArea() - _OuterShape.GetArea();
}
}
用法大概是这样的
IShape shape1 = new Rectangle() { Height = 1, Width = 3 };
IShape shape2 = new Circle() { Radius = 2 };
Pool swimmingPool = new Pool(shape1, shape2);
Console.WriteLine(swimmingPool.GetRequiredArea());
根据您的评论,您似乎想测试对象是否实现了接口
你可以这样做
SwimmingPool(IShape innerShape, IShape outerShape)
{
this.innerShape = innerShape;
this.outerShape = outerShape;
this.Validate();
}
private void Validate()
{
// or some other condition
if ( GetRequiredArea() < 0 ){
throw new Exception("The outer shape must be greater than the inner one");
}
}
if (shape1 is Circle) //...
嗯,您正在使用接口,因此在SwimmingPool类中,构造函数将需要两个IShape参数。由于您需要一个实现来使用您的接口,例如矩形和圆形,您只需执行以下操作:
class Pool
{
private IShape _InnerShape;
private IShape _OuterShape;
public Pool(IShape inner, IShape outer)
{
_InnerShape = inner;
_OuterShape = outer;
}
public double GetRequiredArea()
{
return _InnerShape.GetArea() - _OuterShape.GetArea();
}
}
用法大概是这样的
IShape shape1 = new Rectangle() { Height = 1, Width = 3 };
IShape shape2 = new Circle() { Radius = 2 };
Pool swimmingPool = new Pool(shape1, shape2);
Console.WriteLine(swimmingPool.GetRequiredArea());
根据您的评论,您似乎想测试对象是否实现了接口
你可以这样做
SwimmingPool(IShape innerShape, IShape outerShape)
{
this.innerShape = innerShape;
this.outerShape = outerShape;
this.Validate();
}
private void Validate()
{
// or some other condition
if ( GetRequiredArea() < 0 ){
throw new Exception("The outer shape must be greater than the inner one");
}
}
if (shape1 is Circle) //...
谢谢@scibuff,我需要你的建议,告诉我如何在创建SwimmingPool类的对象时将值传递给构造函数?混淆的是内部形状和外部形状可以是任何形状的圆圈或矩形如何知道在运行时传递哪一个?这种验证是不够的-您可以有一个不适合矩形的圆圈,即使矩形具有更大的尺寸area@haansi是的,这就是重点,只要它实现了GetArea方法,您就不关心它是什么类型的对象。例如,你可以这样做:CicleC1=newCircle 10;圆圈c2=新圆圈20;游泳池sp=新游泳池c1、c2@BrokenGlass,是的,当然,我是在演示如何做一般的事情,而不是如何实现正确的验证感谢scibuff,当我们不确定类型时,请指导如何在运行时将值传递给constructo?我没听懂,但可能是多态性?请指导Thank@scibuff,我需要您的建议,告诉我如何在创建SwimmingPool类的对象时将值传递给构造函数?混淆的是内部形状和外部形状可以是任何形状的圆圈或矩形如何知道在运行时传递哪一个?这种验证是不够的-您可以有一个不适合矩形的圆圈,即使矩形具有更大的尺寸area@haansi是的,这就是重点,只要它实现了GetArea方法,您就不关心它是什么类型的对象。例如,你可以这样做:CicleC1=newCircle 10;圆圈c2=新圆圈20;游泳池sp=新游泳池c1、c2@BrokenGlass,是的,当然,我是在演示如何做一般的事情,而不是如何实现正确的验证感谢scibuff,当我们不确定类型时,请指导如何在运行时将值传递给constructo?我没听懂,但可能是多态性?请引导感谢布莱恩·克罗斯比,它在设计时是可以的。当我们不确定类型时,请建议如何在运行时处理它?我不完全理解,但可能是多态性?Thanks@haansi:类型将是实现IShape的任何东西,因为我们定义了一个接受两个IShape对象的构造函数。您不必在运行时知道类型。谢谢@Bryan Crosby,如果我不知道类型,我将如何实例化?就像你做的那样:IShape shape1=新矩形{高度=1,宽度=3};IShape shape2=新圆{半径=2};这里已知一种是矩形,另一种是圆形。请给出建议。我的意思是shape1,shape2都可以是圆形或矩形的组合。@haansi:你不能实例化接口,你必须提供一个实现的类型,如我上面的例子。您只需传入实现IShape的任何对象。可以是圆柱体、三角形、圆形、正方形等。感谢Bryan Crosby,它在设计时还可以。当我们不确定类型时,请建议如何在运行时处理它?我不完全理解,但可能是多态性?Thanks@haansi:类型将是实现IShape的任何东西,因为我们定义了一个接受两个IShape对象的构造函数。您不必在运行时知道类型。谢谢@Bryan Crosby,如果我不知道类型,我将如何实例化?就像你做的那样:IShape shape1=新矩形{高度=1,宽度=3};IShape shape2=新圆{半径=2};这里已知一种是矩形,另一种是圆形。请给出建议。我的建议是什么
平均值是shape1,shape2都可以是圆形或矩形的组合。@haansi:您不能实例化接口,您必须提供一个实现的类型,如上面的示例所示。您只需传入实现IShape的任何对象。可以是圆柱体、三角形、圆形、正方形等。