用c#写一个类,当没有输入参数时,显示“no”,一个参数时显示“参数不全”,两个参数时计算圆柱体积,大于两个参数时现实“参数过多”
时间: 2024-04-16 11:24:24 浏览: 21
下面是使用 C# 编写的满足您要求的类:
```csharp
using System;
public class MyClass
{
public void MyMethod()
{
Console.WriteLine("No");
}
public void MyMethod(int param)
{
Console.WriteLine("参数不全");
}
public void MyMethod(int param1, int param2)
{
double volume = Math.PI * Math.Pow(param1, 2) * param2;
Console.WriteLine("圆柱体积为:" + volume);
}
public void MyMethod(params int[] paramsArray)
{
Console.WriteLine("参数过多");
}
}
public class Program
{
public static void Main(string[] args)
{
MyClass myObj = new MyClass();
if (args.Length == 0)
{
myObj.MyMethod();
}
else if (args.Length == 1)
{
int param = int.Parse(args[0]);
myObj.MyMethod(param);
}
else if (args.Length == 2)
{
int param1 = int.Parse(args[0]);
int param2 = int.Parse(args[1]);
myObj.MyMethod(param1, param2);
}
else
{
int[] paramsArray = Array.ConvertAll(args, int.Parse);
myObj.MyMethod(paramsArray);
}
}
}
```
在 `MyClass` 类中,我们定义了四个重载方法 `MyMethod`。根据输入参数的数量,选择不同的重载方法进行调用。最后一个重载方法使用 `params` 关键字,可以接受任意数量的参数。在 `Main` 方法中,我们通过判断 `args` 数组的长度来确定输入的参数数量,并调用相应的方法。如果参数数量超过了预期,会调用最后一个重载方法并输出 `"参数过多"`。