【问题标题】:Confused about carrying variables and values from one method to another对将变量和值从一种方法传递到另一种方法感到困惑
【发布时间】:2017-06-19 12:54:55
【问题描述】:

我正在编写一个简单的练习程序,它允许用户输入各种形状的角的 x/y 坐标,然后程序会告诉用户关于我创建的所描述形状(面积、周长等)的基本事实一个名为 TRIANGLE 的类,我在其中有一个方法来确定每一边的长度(并将每一边的长度分配给它们自己的变量)和周长。我正在尝试编写另一种方法来比较这些长度变量以确定哪一侧是斜边、高度和底边。这是我到目前为止的代码:

public void GetLength()
{
    string choice;
    double side1length = Math.Sqrt(((corner1.x - corner2.x) + (corner1.y - corner2.y) * (corner1.x - corner2.x) + (corner1.y - corner2.y)));
    double side2length = Math.Sqrt(((corner2.x - corner3.x) + (corner2.y - corner3.y) * (corner2.x - corner3.x) + (corner2.y - corner3.y)));
    double side3length = Math.Sqrt(((corner3.x - corner1.x) + (corner3.y - corner1.y) * (corner3.x - corner1.x) + (corner3.y - corner1.y)));
    Console.WriteLine("Which side do you want the length of? (first, second, third, perimeter)");
    choice = Console.ReadLine();
    switch (choice)
    {
        case "first":
            Console.Write($"The first side is {length1} units long");
            break;
        case "second":
            Console.Write($"The second side is {length2} units long");
            break;
        case "third":
            Console.Write($"The third side is {length3} units long");
            break;
        case "perimeter":
            Console.Write($"The perimeter is {length1 + length2 + length3}");
            break;
    }
}

public void GetArea()
{
    double hypotenuse, height, Base;
    if (side1length > side2length)
        if (side1length > side3length)
            hypotenuse = length1;
}

我是否需要在 GetLength() 方法中使用 out 关键字?问题是我尝试访问GetArea() 方法中的变量的方式吗?两者都有吗?我完全打破了继承规则吗?有什么更好的方法来做到这一点?

【问题讨论】:

  • GetArea() 从不调用或作用域GetLength(),那么您希望它如何使用其变量值?

标签: c# variables inheritance methods out


【解决方案1】:

GetArea() 从不调用GetLength(),其变量范围为private,因此GetArea() 无法使用它们。要么使用out 参数,要么将变量限定为的私有,而不是方法:

    private double side1length;
    private double side2length;
    private double side3length;

    public void GetLength()
    {
        string choice;
        side1length = ...;
        side2length = ...;
        side3length = ...;
        ...
    }
    public void GetArea()
    {
        GetLength();

        // Now you can use the variables here
        ....
    }

【讨论】:

    【解决方案2】:

    这里您已经在 GetLength() 方法中编写了局部变量,即 side1length、side2length、...等等,因此您不能在 GetArea() 方法中使用它们。 您应该首先阅读此内容,然后再试一次解决方案。 https://docs.thunderstone.com/site/vortexman/variable_scope_global_vs_local.html

    【讨论】:

      【解决方案3】:

      首先,GetLength() 从未被调用,因此不会计算任何side*length

      其次,side*length 是局部变量,你最好先看变量作用域。

      有几种方法可以解决您的问题,Koby 回答了一种。如果你愿意,你也可以使用out,但记得在调用GetLength()之前声明变量

      public void GetLength(out double side1length, out double side2length, out double side3length) {
          // ...
      }
      
      public void GetArea() {
          double side1length, side2length, side3length;
          GetLength(out side1length, out side2length, out side3length);
          // ...
      }
      

      您还可以声明一个结构/类并作为变量返回。下面是一个使用struct的例子。

      public struct SideLengths {
          public double side1;
          public double side2;
          public double side3;
      }
      public SideLengths GetLength() {
          SideLengths sl;
          sl.side1 = Math.Sqrt(((corner1.x - corner2.x) + (corner1.y - corner2.y) * (corner1.x - corner2.x) + (corner1.y - corner2.y)));
          // ...
          return sl;
      }
      public void GetArea() {
          SideLengths sl = GetLength();
          // use sl.side1 instead side1length in your original code
      }
      

      另外,如果是我,我不会在函数中加入交互。最好在“主要”功能而不是单个实用程序功能中询问用户交互。例如:

      public double GetLength(string which) {
          double retVal;
          // calculating length
          return retVal;
      }
      static void Main() {
          // other code
          Console.WriteLine("Which side do you want the length of? (first, second, third, perimeter)");
          choice = Console.ReadLine();
          double length = GetLength(choice);
          // print out the length
      }
      

      这仍然不是最好的做法,但目前比你的要好。但是,我会在这个阶段离开你,因为显然你是一个初学者,如果你被推得太深,可能会搞砸你的想法。

      最后,请考虑一个更好的命名变量的做法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-14
        • 1970-01-01
        • 2013-01-29
        相关资源
        最近更新 更多