【问题标题】:Can I accessing variables which is declare in say class A from say class B in Java [closed]我可以从 Java 中的 B 类访问在 A 类中声明的变量吗?
【发布时间】:2018-07-03 10:19:25
【问题描述】:

我是 Java 新手。我已经完成了一个查找矩形面积的示例程序。代码如下。

package rectangle;

public class Rectangle {
    int length, width;

    int rectArea() {
        int area = length * width;
        return (area);
    }
}

class RectArea {
    public static void main(String[] args) {
        int area1;

        Rectangle rect1 = new Rectangle();

        rect.length = 14;
        rect.width = 13;
        area1 = rect.rectArea();

        System.out.println("Area1=" + area1);
    }
}

在上面的代码中lengthwidth 是在类Rectangle 中声明的变量。现在area 也是一个保存数据length * width 的变量,并且这个areavariable 也在类Rectangle 中声明

我们可以使用点运算符从另一个名为RectArea 的类中访问lengthwidth 变量。但是为什么我们不能直接从RectArea 类(使用点运算符)访问Rectangle 类中声明的area 变量来评估Rectangle 的值?

也就是说,为什么我们不能使用下面的代码来评估来自RectArea 类的新创建对象rect1 的值。

area1 = rect1.area;
System.out.println("Area1="+ area1);

或者为什么我们不能使用上面的代码从RectArea 类中访问Rectangle 类中声明的area 变量?

【问题讨论】:

  • 因为它是方法内部的局部变量,所以它不存在于类的范围内。你也不应该像那样暴露你的其他变量。您应该将它们声明为私有,并通过 getter 和 setter 限制访问
  • rect1 没有名为 area 的字段。因此您无法访问它
  • rect.area 应该是什么? rect 是一个 Rectangle,它没有 area

标签: java class variables dot-operator


【解决方案1】:

有局部变量和实例(类)变量。类变量就像长度和宽度,它们可以在整个类中使用。

但是对于局部变量,它们只能在您声明它们的方法/代码块中使用。在这种情况下,区域在方法中声明并且仅在方法中可用。

一旦代码跳出方法(return)区域就不再存在了。

我已经修复了下面的代码,使其可以正常工作:

    int length, width, area;

        void getData(int x, int y)
        {
            length=x;
            width=y;
        }

        int rectArea()
        {
            area=length*width;
            return area;

        }

        }
        class RectArea{

            public static void main(String[] args) {

                int area1, area2;

                 Rectangle  rect1 =new Rectangle();

                 Rectangle rect2 =new Rectangle();

                rect1.length=14;
                rect1.width=13;
                area1=rect1.length*rect1.width;
                rect2.getData(13,14);
                area2=rect2.rectArea();

          System.out.println("Area1="+ area1);
          System.out.println("Area1="+ area2);
System.out.println("Area1="+ rect2.area);
            }

        }

【讨论】:

  • Java没有全局变量,你的意思是局部变量(方法/块作用域)和实例或静态变量(类作用域)的区别
  • 谢谢,我编辑我的答案!
【解决方案2】:

arearectArea 方法的局部变量。类外,甚至类内都无法访问,除非是同一个方法。

如果您希望 area 可以在外面访问,您为什么不尝试这样的事情:

public class Rectangle {

    int length, width, area;

    void getData(int x, int y) {
        length = x;
        width = y;
        area = length * width;
    }
}

【讨论】:

    【解决方案3】:

    area 不是类变量,它在方法内部,您无法访问方法变量,因为它们是本地的并且仅对方法可见。

    【讨论】:

      【解决方案4】:

      area 不是类级别的变量。它是 rectArea 方法中的一个局部变量,因此,它在方法外是不可见的,也不能像类变量一样通过点运算符访问

      【讨论】:

      • 如果我将区域变量声明为静态会怎样
      • 您不能在方法中声明静态变量。静态变量仅在类级别声明
      • 但是如果我将方法声明为静态,我可以将方法声明为静态会发生什么。
      • 仍然无法访问方法的本地变量,也无法将局部变量声明为静态变量。
      • @coding_ninza 这是因为方法是在类级别声明的。您不会在方法中找到声明的方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多