英文:
Entity class and encapsulation confusion - how do you call a non-static method from main()?
问题
我正在创建一个名为"Crate"的类,该类将读取一个包含以英寸表示的物体尺寸的整数的输入文件,并输出一个报告,其中包含每个物体所需的包装箱尺寸。一切都看起来很好,直到我尝试测试我的方法时,因为我无法从main()中调用它们。尝试时,我收到了一个错误消息,上面写着:"无法从静态上下文引用非静态方法createReport()。"
我是一个初学者,所以如果有什么基本错误,我将乐意接受批评。我只是想知道是否有一个简单的修复方法,以及关于静态/非静态方法的知识差距是什么。
package crate;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Crate {
private Crate[] crateList;
private int crateAmount;
private int height, width, depth, panels;
private int smallestCratePosition, smallestCrateSize;
private int largestCratePosition, largestCrateSize;
private double averageCrateSize;
public static void main(String[] args) throws IOException {
Crate crate = new Crate(); // Create an instance of the Crate class
crate.createReport(); // Call the createReport method on the instance
}
public Crate() {
height = 1;
width = 1;
depth = 1;
panels = 6;
}
public void createReport() throws IOException {
Scanner infile = new Scanner(new File("ShippingSizes.txt"));
PrintWriter outfile = new PrintWriter(new File("CrateReport.txt"));
createCrateList(infile);
outfile.println("There are " + crateAmount + " crates.");
for (int i = 0; i < crateList.length; i++) {
outfile.println(i + ": height=" + crateList[i].getHeight() + ", width=" +
crateList[i].getWidth() + ", depth=" + crateList[i].getDepth() +
", panels=" + crateList[i].getPanels());
}
outfile.println("\n" + "Smallest crate is at " + smallestCratePosition +
": height=" + crateList[smallestCratePosition].getHeight() + ", width=" +
crateList[smallestCratePosition].getWidth() + ", depth=" +
crateList[smallestCratePosition].getDepth() + ", panels=" +
crateList[smallestCratePosition].getPanels());
outfile.println("Largest crate is at " + largestCratePosition + ": height," +
crateList[largestCratePosition].getHeight() + ", width=" +
crateList[largestCratePosition].getWidth() + ", depth=" +
crateList[largestCratePosition].getDepth() + ", panels=" +
crateList[largestCratePosition].getPanels());
outfile.println("Average crate size is: " + averageCrateSize);
outfile.close();
infile.close();
}
public void setCrateDimensions(Scanner scnr) {
int tempValue = scnr.nextInt();
height = tempValue / 12;
if (tempValue % 12 != 0)
height++;
tempValue = scnr.nextInt();
width = tempValue / 12;
if (tempValue % 12 != 0)
width++;
tempValue = scnr.nextInt();
depth = tempValue / 12;
if (tempValue % 12 != 0)
depth++;
panels = 2 * ((height * width) + (height * depth) + (width * depth));
}
public void createCrateList(Scanner scnr) {
crateAmount = scnr.nextInt();
crateList = new Crate[crateAmount]; // Initialize the array
for (int i = 0; i < crateAmount; i++) {
crateList[i] = new Crate(); // Create instances of Crate for each element
crateList[i].setCrateDimensions(scnr);
}
}
public void getSmallestCrate() {
smallestCrateSize = crateList[0].getPanels();
for (int i = 0; i < crateList.length; i++) {
if (crateList[i].getPanels() < smallestCrateSize) {
smallestCratePosition = i;
smallestCrateSize = crateList[i].getPanels();
}
}
}
public void getLargestCrate() {
largestCrateSize = crateList[0].getPanels();
for (int i = 0; i < crateList.length; i++) {
if (crateList[i].getPanels() > largestCrateSize) {
largestCratePosition = i;
largestCrateSize = crateList[i].getPanels();
}
}
}
public void getAverageCrateSize() {
double totalPanels = 0;
for (int i = 0; i < crateList.length; i++)
totalPanels += crateList[i].getPanels();
averageCrateSize = totalPanels / (double) (crateList.length);
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public int getDepth() {
return depth;
}
public int getPanels() {
return panels;
}
}
我已经在main()方法中创建了Crate类的一个实例,并且通过该实例调用了createReport()方法,这应该解决了你遇到的问题。希望这能帮助你。
英文:
I am creating a class called "Crate" that will read an input file of integers representing objects dimensions in inches and output a report containing the crate sizes required for each respective item. Everything seemed fine until I tried to test my methods, as I am unable to call them from main(). When trying, I get an error message saying, "non-static method createReport() cannot be referenced from a static context."
I am a beginner, so if anything is fundamentally wrong I will happily accept criticism. I am just curious if there is a simple fix to this and what my gap in knowledge is regarding static/non-static methods.
package crate;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Crate {
private Crate[] crateList;
private int crateAmount;
private int height, width, depth, panels;
private int smallestCratePosition, smallestCrateSize;
private int largestCratePosition, largestCrateSize;
private double averageCrateSize;
public static void main(String[] args) throws IOException {
createReport();
}
public Crate() {
height = 1;
width = 1;
depth = 1;
panels = 6;
}
public void createReport() throws IOException {
Scanner infile = new Scanner(new File("ShippingSizes.txt"));
PrintWriter outfile = new PrintWriter(new File("CrateReport.txt"));
createCrateList(infile);
outfile.println("There are " + crateAmount + " crates.");
for (int i = 0; i < crateList.length; i++) {
outfile.println(i + ": height=" + crateList[i].getHeight() + ", width=" +
crateList[i].getWidth() + ", depth=" + crateList[i].getDepth() +
", panels=" + crateList[i].getPanels());
}
outfile.println("\n" + "Smallest crate is at " + smallestCratePosition +
": height=" + crateList[smallestCratePosition].getHeight() + ", width=" +
crateList[smallestCratePosition].getWidth() + ", depth=" +
crateList[smallestCratePosition].getDepth() + ", panels=" +
crateList[smallestCratePosition].getPanels());
outfile.println("Largest crate is at " + largestCratePosition + ": height," +
crateList[largestCratePosition].getHeight() + ", width=" +
crateList[largestCratePosition].getWidth() + ", depth=" +
crateList[largestCratePosition].getDepth() + ", panels=" +
crateList[largestCratePosition].getPanels());
outfile.println("Average crate size is: " + averageCrateSize);
outfile.close();
infile.close();
}
public void setCrateDimensions(Scanner scnr) {
int tempValue = scnr.nextInt();
height = tempValue/12;
if (tempValue%12 != 0)
height++;
tempValue = scnr.nextInt();
width = tempValue/12;
if (tempValue%12 != 0)
width++;
tempValue = scnr.nextInt();
depth = tempValue/12;
if (tempValue%12 != 0)
depth++;
panels = 2*((height*width)+(height*depth)+(width*depth));
}
public void createCrateList(Scanner scnr) {
crateAmount = scnr.nextInt();
for (int i = 0; i < crateAmount; i++) {
crateList[i].setCrateDimensions(scnr);
}
}
public void getSmallestCrate() {
smallestCrateSize = crateList[0].getPanels();
for (int i = 0; i < crateList.length; i++) {
if (crateList[i].getPanels() < smallestCrateSize) {
smallestCratePosition = i;
smallestCrateSize = crateList[i].getPanels();
}
}
}
public void getLargestCrate() {
largestCrateSize = crateList[0].getPanels();
for (int i = 0; i < crateList.length; i++) {
if (crateList[i].getPanels() > largestCrateSize) {
largestCratePosition = i;
largestCrateSize = crateList[i].getPanels();
}
}
}
public void getAverageCrateSize() {
double totalPanels = 0;
for (int i = 0; i < crateList.length; i++)
totalPanels += crateList[i].getPanels();
averageCrateSize = totalPanels / (double)(crateList.length);
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public int getDepth() {
return depth;
}
public int getPanels() {
return panels;
}
}
I tried declaring the Scanner, PrintWriter, Crate[] object, and crateAmount integer inside of main(), however this only led to the same issue later on as I was unable to use those variables in my methods. Any help would be greatly appreciated!
答案1
得分: 1
因为createReport
方法与它所属的Class Crate
的实例相关联。
你必须使用new
关键字创建一个实例,然后才能调用它的方法createReport
。
另一种方式是,如果你不想创建类的实例,可以使用关键字static
将方法声明为静态方法。这样,在启动JVM时会加载该方法。
建议:你应该通过阅读一本书或另一篇文章来深入理解面向对象编程(OOP)。
英文:
because the createReport
method is related to it's instance of Class Crate.
you must create an instance by new
keywords, then you can call it's method createReport
.
another way, if you don't want create an instance of class, you can declared method to static method by keyword static
. then the method will be load when starting the jvm.
suggestion: you should deeply understand the OOP programming by reading an book or another article
答案2
得分: 0
非静态方法属于类的实例,而不属于类本身。这就是为什么您无法从main()
函数中访问createReport()
方法,因为main()
函数位于静态作用域,而createReport()
方法不是。尝试实例化类的一个实例,并在该对象上调用createReport()
方法。
例如:
Crate crate = new Crate();
crate.createReport();
英文:
Non-static methods belong to an instance of a class, not the class itself. This is why you can't access the createReport()
method from main()
, because main is in the static scope, while createReport is not. Try instantiating an instance of the class and calling createReport()
on the object.
For example:
Crate crate = new Crate();
crate.createReport();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论