Java——当在类外调用时返回正确值,而不是从类内调用时返回正确值的方法?
我正在做Java的基础知识,所以在做一个更大的程序时,我陷入了这个问题,我想我会尝试在一个单独的程序中修复它。但这仍然让我困惑。 我把这个问题分解成一件我无法解释的事情 这是代码Java——当在类外调用时返回正确值,而不是从类内调用时返回正确值的方法?,java,arrays,class,oop,methods,Java,Arrays,Class,Oop,Methods,我正在做Java的基础知识,所以在做一个更大的程序时,我陷入了这个问题,我想我会尝试在一个单独的程序中修复它。但这仍然让我困惑。 我把这个问题分解成一件我无法解释的事情 这是代码 package Basics; import java.util.Scanner; import java.util.Arrays; public class Polygon { Scanner input = new Scanner(System.in); int num; Polyg
package Basics;
import java.util.Scanner;
import java.util.Arrays;
public class Polygon {
Scanner input = new Scanner(System.in);
int num;
Polygon(int n) {
this.num = n;
}
int[] getSides() {
int[] sides = new int[this.num];
for (int i = 0; i < this.num; i++) {
System.out.println("Enter length of side " + (i + 1));
sides[i] = input.nextInt();
}
//System.out.println(Arrays.toString(sides));
return sides;
}
int[] sides = this.getSides();
}
class runner {
public static void main(String[] args) {
Polygon rectangle = new Polygon(4);
int[] sides = rectangle.getSides();
System.out.println(Arrays.toString(sides));
System.out.println(Arrays.toString(rectangle.sides));
}
}
因此,当从类外部调用时,可以看到getSides方法返回正确的值,即用户输入的值。但从内部看不是吗?
我很确定这是一个愚蠢的问题,我遗漏了很多东西,但我真的需要帮助
这是什么原因造成的,我如何修复它?在函数getSides中定义了一个新的sides数组,您没有调用类实例变量,该变量定义为int[]sides=this.getSides
为了获得更好的代码,您可以编写多边形类,如下所示:
public class Polygon {
Scanner input = new Scanner(System.in);
int num;
int[] sides;
Polygon(int n) {
this.num = n;
this.sides = new int[this.num];
}
int[] getSides() {
for (int i = 0; i < this.num; i++) {
System.out.println("Enter length of side " + (i + 1));
sides[i] = input.nextInt();
}
System.out.println(Arrays.toString(sides));
return sides;
}
}
在函数getSides中,您定义了一个新的sides数组,但没有调用类实例变量,该变量定义为int[]sides=this.getSides
为了获得更好的代码,您可以编写多边形类,如下所示:
public class Polygon {
Scanner input = new Scanner(System.in);
int num;
int[] sides;
Polygon(int n) {
this.num = n;
this.sides = new int[this.num];
}
int[] getSides() {
for (int i = 0; i < this.num; i++) {
System.out.println("Enter length of side " + (i + 1));
sides[i] = input.nextInt();
}
System.out.println(Arrays.toString(sides));
return sides;
}
}
您的问题是,您需要调用getSides方法来分配变量sides,因为此分配将在构造函数之前运行,所以变量num是0默认值,因此不会进行迭代
在构造函数之前执行的示例方法
public class Polygon {
Scanner input = new Scanner(System.in);
int num;
int[] sides = this.getSides(); // Assign a variable with the return of a method, this is executed before the constructor
Polygon(int n) {
System.out.println("I am the constructor");
this.num = n;
}
int[] getSides() {
System.out.println("I will be executed before the constructor");
int[] sides = new int[this.num];
for (int i = 0; i < this.num; i++) {
System.out.println("Enter length of side " + (i + 1));
sides[i] = input.nextInt();
}
//System.out.println(Arrays.toString(sides));
return sides;
}
public static void main(String[] args) {
new Polygon(5);
}
您的问题是,您需要调用getSides方法来分配变量sides,因为此分配将在构造函数之前运行,所以变量num是0默认值,因此不会进行迭代
在构造函数之前执行的示例方法
public class Polygon {
Scanner input = new Scanner(System.in);
int num;
int[] sides = this.getSides(); // Assign a variable with the return of a method, this is executed before the constructor
Polygon(int n) {
System.out.println("I am the constructor");
this.num = n;
}
int[] getSides() {
System.out.println("I will be executed before the constructor");
int[] sides = new int[this.num];
for (int i = 0; i < this.num; i++) {
System.out.println("Enter length of side " + (i + 1));
sides[i] = input.nextInt();
}
//System.out.println(Arrays.toString(sides));
return sides;
}
public static void main(String[] args) {
new Polygon(5);
}
在java中,类名以大写字母开头,因此最好将runner写为runnerroy!我不知道在java中,类名以大写字母开头,所以最好将运行程序写为runner抱歉!我不知道我觉得这行不通。尝试将System.out.PrintLnArray.toStringrectangle.sides移动到System.out.PrintLnArray.toStringrectangle.sides;在getSides调用之前,您还将得到空数组;to sides=new int[this.num];它工作正常。是的,它适用于给定的测试用例。但在创建对象时,它不会初始化sides字段。@SatvikGupta我为您的问题添加了一个更好、更干净的代码,可以防止逻辑错误。@SatvikGupta,称为阴影。有关更多信息,请参阅-基本上,当两个变量在不同的作用域中具有相同的名称时,最直接的作用域是used,我认为这是行不通的。尝试将System.out.PrintLnArray.toStringrectangle.sides移动到System.out.PrintLnArray.toStringrectangle.sides;在getSides调用之前,您还将得到空数组;to sides=new int[this.num];它工作正常。是的,它适用于给定的测试用例。但在创建对象时,它不会初始化sides字段。@SatvikGupta我为您的问题添加了一个更好、更干净的代码,可以防止逻辑错误。@SatvikGupta,称为阴影。有关更多信息,请参见-基本上,当两个变量在不同的作用域中具有相同的名称时,将使用最直接的作用域。那么如何首先调用构造函数?什么?构造函数是一种特殊的方法,它是在任何其他方法之前执行的。多边形矩形=新多边形4;它将执行构造函数。我很困惑,你说对getSides的方法调用是在构造函数之前执行的,它是在构造函数之前执行的。构造函数是创建对象时执行的第一个方法。您的问题是,您正在使用方法的结果分配一个变量,而此方法使用另一个实例变量。因此,该方法是使用您声明和初始化的值执行的。如果您没有定义,将使用默认值。Bro,我将编辑我的answear并给出一个示例。看一看,那么我如何首先调用构造函数?什么?构造函数是一种特殊的方法,它是在任何其他方法之前执行的。多边形矩形=新多边形4;它将执行构造函数。我很困惑,你说对getSides的方法调用是在构造函数之前执行的,它是在构造函数之前执行的。构造函数是创建对象时执行的第一个方法。您的问题是,您正在使用方法的结果分配一个变量,而此方法使用另一个实例变量。因此,该方法是使用您声明和初始化的值执行的。如果您没有定义,将使用默认值。Bro,我将编辑我的answear并给出一个示例。看一看
public class Polygon {
Scanner input = new Scanner(System.in);
int num;
int[] sides;
Polygon(int n) {
this.num = n;
this.sides = new int[this.num];
}
int[] getSides() {
for (int i = 0; i < this.num; i++) {
System.out.println("Enter length of side " + (i + 1));
sides[i] = input.nextInt();
}
System.out.println(Arrays.toString(sides));
return sides;
}
}
public class Polygon {
Scanner input = new Scanner(System.in);
int num; // Java default value for integers is zero
Polygon(int n) {
this.num = n; // The value is changed when an object is created;
}
int[] getSides() {
int[] sides = new int[this.num];
for (int i = 0; i < this.num; i++) {
System.out.println("Enter length of side " + (i + 1));
sides[i] = input.nextInt();
}
return sides;
}
int[] sides = this.getSides(); // This method call to getSides will be executed before the Constructor, using all the instance variables(so, the variable num is 0)
}
public class Polygon {
Scanner input = new Scanner(System.in);
int num;
int[] sides = this.getSides(); // Assign a variable with the return of a method, this is executed before the constructor
Polygon(int n) {
System.out.println("I am the constructor");
this.num = n;
}
int[] getSides() {
System.out.println("I will be executed before the constructor");
int[] sides = new int[this.num];
for (int i = 0; i < this.num; i++) {
System.out.println("Enter length of side " + (i + 1));
sides[i] = input.nextInt();
}
//System.out.println(Arrays.toString(sides));
return sides;
}
public static void main(String[] args) {
new Polygon(5);
}