我正在尝试编写一个基本程序,有两个类。我可以用一个班级管理好:
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#很陌生,感谢您提供的任何帮助!
答案 0 :(得分:2)
好。如此。
我将如何编写此计算类:
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
是什么,请查看问题here的评论。我的解释很糟糕
答案 1 :(得分:0)
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();
}
}
}
答案 2 :(得分:0)
我建议你获得有关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;
}
}
答案 3 :(得分:0)
虽然您使用 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;
}
}
请注意,这是一个包含两个字段(半径,高度),方法(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
类可以在其他上下文中重用,并且可以调整内部逻辑而不会影响代码的其他部分。