使用参数创建方法实例

时间:2019-04-09 09:57:59

标签: c# .net

如何调用另一个类的方法实例中的参数,因为我想使用该方法进行计算并显示它。

class Box
{
    int width = 10;
    int height = 15;

    public int Area(int Area)
    {
        Area = width * height;

        return Area;
    }

    public int Perimeter(int Para)
    {
        Para = 2 * (height + width);

        return Para;
    }
}

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

static void Main(string[] args)
{
     Box b = new Box();
     b.Area(Area);
     b.Perimeter(Para);

     Console.ReadLine();
 }

这使我对b.Area(Area)产生了错误;和b.Perimeter(Para);

1 个答案:

答案 0 :(得分:3)

也许您想这样做:

class Program
{
    static void Main(string[] args)
    {
        Box box = new Box(10, 15);
        Console.WriteLine("Area is: " + box.CalculateArea());
        Console.WriteLine("Perimeter is: " + box.CalculatePerimeter());
        Console.ReadLine();
    }
}

public class Box
{
    public int Width { get; set; }
    public int Height { get; set; }

    public Box(int width, int height)
    {
        Width = width;
        Height = height;
    }

    public int CalculateArea()
    {
        return Width * Height;
    }

    public int CalculatePerimeter()
    {
        return 2 * (Width + Height);
    }
}