微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java 方法中声明的任何内容都是本地的某些特殊情况除外,对吗?

如何解决Java 方法中声明的任何内容都是本地的某些特殊情况除外,对吗?

public class Triangle {
    double area;
    int height;
    int length;
    
    public static void main(String [] args) {
        int x = 0;
        Triangle [ ] ta = new Triangle[4];
        
        while ( x < 4 ) {
            ta[x] = new Triangle();
            ta[x].height = (x + 1) * 2;
            ta[x].length = x + 4;
            ta[x].setArea();
            System.out.print("triangle "+x+",area");
            System.out.println(" = " + ta[x].area);
            x = x + 1;
        }
        
        int y = x;
        x = 27;
        Triangle t5 = ta[2];
        ta[2].area = 343;
        System.out.print("y = " + y);
        System.out.println(",t5 area = "+ t5.area);
    }
    
    void setArea() {
        ta[x].area = (height * length) / 2;
    }
}

$javac Triangle.java
Triangle.java:25: error: cannot find symbol
ta[x].area = (height * length) / 2;
^
symbol:   variable ta
location: class Triangle
Triangle.java:25: error: cannot find symbol
ta[x].area = (height * length) / 2;
   ^
symbol:   variable x
location: class Triangle
2 errors

当然,我很快发现我需要接受“ta[x]”。 setArea 方法的事情,这很清楚,但现在我想知道为什么我不能在该 area 函数之前放置一个先前声明的 Triangle 数组。 是不是因为在该方法中,所有事物都是局部的,并且您不能使用未在该方法中声明的变量,即使您已经在代码的其他部分声明了它们?谢谢各位。

解决方法

x ta 是局部变量,它们的作用域被限制在它们各自的作用域之外,你不能引用它。

只需传递引用即可。

public class Triangle {
    double area;
    int height;
    int length;
    public static void main(String [] args) {
        int x = 0;
        Triangle [ ] ta = new Triangle[4];
        while ( x < 4 ) {
            ta[x] = new Triangle();
            ta[x].height = (x + 1) * 2;
            ta[x].length = x + 4;
            ta[x].setArea(ta[x]);
            System.out.print("triangle "+x+",area");
            System.out.println(" = " + ta[x].area);
            x = x + 1;
        }
        int y = x;
        x = 27;
        Triangle t5 = ta[2];
        ta[2].area = 343;
        System.out.print("y = " + y);
        System.out.println(",t5 area = "+ t5.area);
    }
    void setArea(Triangle t) {
        t.area = (height * length) / 2;
    }
}

ta,x 变量范围仅限于 Main() 函数

,

你问的问题与java中变量作用域的概念有关。 在 {} 中声明的任何变量,只能在那些 {} 中访问。{}在 {} 之外,无法访问变量。 在您的代码区域是 Triangle 类的实例变量,其长度和高度作为实例变量。 所以要计算面积,您可以直接使用长度和高度。像 面积=长*高 要计算特定对象的面积,请通过该对象调用它,或者您可以按照 user207421 发布的前一篇文章中提到的传递三角形对象

,

您在代码段中有几个问题。让我告诉你:

  • 最好尽可能使用不可变对象。想象一下,如果有人会使用您的 Triangle 而忘记调用 setArea();
  • OOP 的一项原则是封装,因此您应该避免直接访问类的变量(仅使用 getter);
  • 通常,您应该存储最少的信息。例如。 setArea() 应直接移至 Triangle 类。而且,计算起来非常简单,而且最好这样做
  • 最后看除法操作;在执行 / 之前,您必须将结果转换为 double。如果您执行 (int + int) / int - 结果将始终是一个整数。

请检查我的解决方案:

// class is final (this is not mandatory)
public final class Triangle {

    // variables are final (in general this is good approach)
    // variables are private (access only via getters)
    private final int width;
    private final int height;

    public Triangle(int width,int height) {
        this.width = width;
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    // 2.0 is very important,here we cast (width * height) to double before division
    public double getArea() {
        return (width * height) / 2.0;
    }

}
public class Main {

    public static void main(String... args) {
        Triangle[] triangles = createTriangles(4);
        print(triangles);
    }

    private static Triangle[] createTriangles(int total) {
        Triangle[] triangles = new Triangle[total];

        // for loops letters like i,j,k are more common (x is mostly to 2D)
        for (int i = 0; i < triangles.length; i++) {
            int width = i + 4;
            int height = (i + 1) * 2;
            triangles[i] = new Triangle(width,height);
        }

        return triangles;
    }

    private static void print(Triangle... triangles) {
        if (triangles != null)
            for (int i = 0; i < triangles.length; i++)
                System.out.format(Locale.ENGLISH,"triangle %d,area = %.2f",i,triangles[i].getArea());
    }

}
,

setArea() 方法是类三角形的一部分,因此计算三角形的面积没有问题。

public class Triangle {
double area;
int height;
int length;


public double setArea(){        
    return area=(length*height)/2;
}

public static void main(String[] args) {
    int x=0;
    Triangle [ ] ta = new Triangle[4];
    while ( x < 4 ) {
        ta[x] = new Triangle();
        ta[x].height = (x + 1) * 2;
        ta[x].length = x + 4;
        ta[x].setArea();
        System.out.print("triangle "+x+",area");
        System.out.println(" = " + ta[x].area);
        x = x + 1;
    }
    int y = x;
    x = 27;
    Triangle t5 = ta[2];
    ta[2].area = 343;
    System.out.print("y = " + y);
    System.out.println(",t5 area = "+ t5.area);

    
}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?