在C#中使用ref
本文关键字:ref | 更新日期: 2024-08-01 20:21:58
我正在尝试用两个类编写一个基本程序。我可以管理好一个类:
public static void Main (string[] args)
{
double radius;
double height;
double volume;
{
System.Console.WriteLine ("Enter radius");
radius = double.Parse(System.Console.ReadLine());
System.Console.WriteLine (radius);
System.Console.WriteLine ("Enter height");
height = double.Parse(System.Console.ReadLine());
System.Console.WriteLine (height);
volume = Math.PI * radius * radius * height;
System.Console.WriteLine (volume);
但我无法使用ref从一个类到另一个类,例如,我尝试删除最终的体积计算,并制作这个单独的类:
class Calculation
{
double radius2 = 0.00;
double height2 = 0.00;
double volume2
radius2(ref radius);
height2(ref height);
volume2 = Math.PI * radius2 * radius2 * height2;
}
但它根本不起作用。我是C#的新手,谢谢你能提供的任何帮助!
好的。所以。
我将如何编写这个计算类:
public class Calculation
{
public readonly double Result;
public Calculation(double radius, double height)
{
Result = Math.PI * radius * radius * height;
}
}
然后你会这样使用它:
Calculation myCalc = new Calculation(myRadius, myHeight);
double volume = myCalc.Result;
至于ref
是什么,请查看此处对该问题的评论。我对的解释不满意
using System;
namespace ConsoleApplication1
{
class Calculation
{
public double Radius { get; set; }
public double Height { get; set; }
public double GetVolume()
{
return Math.PI * Radius * Radius * Height;
}
}
class Program
{
static void Main(string[] args)
{
double radius;//not used but left for your understanding, you didn't need radius2 in your Calculation class, you could have used the name radius, this is why you need to study variable scope
double height;//not used but left for your understanding, same reason as radius
Calculation calc = new Calculation();
Console.WriteLine("Enter radius");
calc.Radius = double.Parse(Console.ReadLine());
Console.WriteLine(calc.Radius);
Console.WriteLine("Enter height");
calc.Height = double.Parse(Console.ReadLine());
Console.WriteLine(calc.Height);
Console.WriteLine(calc.GetVolume());
Console.ReadLine();
}
}
}
我建议您了解C#的基本知识。然而,你可以用这种方式做到这一点?
class Program
{
public static void Main(string[] args)
{
double radius = 0.00;
double height = 0.00;
double volume = 0.00;
Calculator calObject = new Calculator();
System.Console.WriteLine("Enter radius");
radius = double.Parse(System.Console.ReadLine());
System.Console.WriteLine(radius);
System.Console.WriteLine("Enter height");
height = double.Parse(System.Console.ReadLine());
System.Console.WriteLine(height);
volume = calObject.FindVolumne(radius, height);
System.Console.WriteLine(volume);
Console.ReadLine();
}
}
public class Calculator
{
public double FindVolumne(double radius, double height)
{
double volume = 0.00;
volume = Math.PI * radius * radius * height;
return volume;
}
}
尽管您使用Calculation作为一个类,但您最终还是试图在圆柱体上定义一个操作。圆柱体可以用高度和半径(属性)来表示,并且对于暴露方法(如"获取圆柱体的体积")是一个有用的构造
让我们从这里开始定义Cylinder
类:
class Cylinder
{
public double Radius;
public double Height;
public double GetVolume()
{
return Math.PI * Radius * Radius * Height;
}
// Constructor
public Cylinder(double radius, double height)
{
this.Radius = radius;
this.Height = height;
}
}
请注意,这是一个简单的类,包含两个字段(radius、height)、一个方法(GetArea)和一个构造函数。它封装在圆柱体上构造和执行操作所需的数据。所有这些成员都是公共的,这意味着使用该类的程序可以访问它们。(相比之下,私人会员只能在班级内部使用。)
现在我们可以构建一个利用类的程序:
class Program
{
static Cylinder GetCylinderFromUser()
{
double radius, height;
Console.Write("Enter radius: ");
radius = double.Parse(Console.ReadLine());
Console.Write("Enter height: ");
height = double.Parse(Console.ReadLine());
return new Cylinder(radius, height);
}
static void Main()
{
Cylinder c = GetCylinderFromUser();
Console.WriteLine("Created cylinder with height={0} and radius={1}",
c.Height, // replaces {0} with height of c
c.Radius // replaces {1} with radius of c
);
double volume = c.GetVolume();
Console.WriteLine("The volume of the cylinder is {0}.", volume);
Console.ReadLine();
}
}
这是一个非常基本的例子,但请注意,程序被分解成小块,然后耦合在一起。这些类将用户界面(获得用户提供的输入)与计算逻辑分离开来。Cylinder
类可以在其他上下文中重用,并且可以在不影响代码其他部分的情况下调整内部逻辑。