Java 如何解决我遇到的多个“无法对非静态字段进行静态引用”错误?
我知道这个问题在这个网站上被问了很多次,但给出的答案似乎对我没有帮助。我对Java非常陌生Java 如何解决我遇到的多个“无法对非静态字段进行静态引用”错误?,java,Java,我知道这个问题在这个网站上被问了很多次,但给出的答案似乎对我没有帮助。我对Java非常陌生 import javax.swing.JOptionPane; public class Lab10 { String species, color, injuries, name, wiput, liput, locationFound, oiput; char gender; float weight, length; boolean hasShots, needs
import javax.swing.JOptionPane;
public class Lab10 {
String species, color, injuries, name, wiput, liput, locationFound, oiput;
char gender;
float weight, length;
boolean hasShots, needsShots;
int tagNumber, cageNumber, outcome;
public static void main(String[] args) {
strayAnimal(species, color, weight, length, injuries, locationFound, name);
}
public static void strayAnimal(String species, String color, float weight, float length, String injuries, String locationFound, String name) {
species = JOptionPane.showInputDialog("Please enter the species of the animal: ");
color = JOptionPane.showInputDialog("Please enter the color of the animal: ");
wiput = JOptionPane.showInputDialog("Please enter the weight of the animal");
weight = Float.parseFloat(wiput);
liput = JOptionPane.showInputDialog("Please enter the lenght of the animal: ");
length = Float.parseFloat(liput);
locationFound = JOptionPane.showInputDialog("Please enter the location the animal was found(City, County, State): ");
injuries = JOptionPane.showInputDialog("Please enter the injuries the animal has seperated with commas: ");
}
//public static char strayAnimal() {
//}
public static void setOutcome(int outcome) {
oiput = JOptionPane.showInputDialog("Please enter a '0' if the animal has or will be terminated or a '1' if the animal has been saved: ");
outcome = Integer.parseInt(oiput);
}
public void giveShots() {
boolean giveShots;
if (giveShots = true) {
}
}
public static void needShots() {
boolean needShots;
}
}
您有一个静态方法,例如SetOutput,它希望访问非静态变量,如Output。这是不允许的。
必须使变量结果为静态或方法为非静态。下面的变量必须为静态
String species, color, injuries, name, wiput, liput, locationFound, oiput;
char gender;
float weight, length;
boolean hasShots, needsShots;
int tagNumber, cageNumber, outcome;
因为不能使用非静态变量,所以在静态方法中
String species, color, injuries, name, wiput, liput, locationFound, oiput;
char gender;
float weight, length;
boolean hasShots, needsShots;
int tagNumber, cageNumber, outcome;
都是非静态类成员
像strayAnimals这样的方法是静态的。不能从静态方法引用任何非静态成员。您可以定义类的实例并使用该实例访问类成员
class Lab10 {
public Lab10 instance;
public static Lab10 getInstance() {
if (instance == null) instance = new Lab10();
return instance;
}
...
public static void strayAnimal(...) {
Lab10 instance = Lab10.getInstance();
// now you can use instance.wiput;
}
}
或者将成员设置为静态。但是,第二个选项的副作用是破坏非静态方法 在任何参数中使用strayAnimal pass是没有意义的。 在那之后,你应该很好 它还有助于在实现之外运行类
public class Runner {
public static void main(String[] args) {
Lab10 lab = new Lab10();
lab.strayAnimal();
}
}
这门课很好
public class Lab10 {
String species, color, injuries, name, wiput, liput, locationFound, oiput;
char gender;
float weight, length;
boolean hasShots, needsShots;
int tagNumber, cageNumber, outcome;
public void strayAnimal(String species, String color, float weight, float length, String injuries, String locationFound, String name) {
species = JOptionPane.showInputDialog("Please enter the species of the animal: ");
color = JOptionPane.showInputDialog("Please enter the color of the animal: ");
wiput = JOptionPane.showInputDialog("Please enter the weight of the animal");
weight = Float.parseFloat(wiput);
liput = JOptionPane.showInputDialog("Please enter the lenght of the animal: ");
length = Float.parseFloat(liput);
locationFound = JOptionPane.showInputDialog("Please enter the location the animal was found(City, County, State): ");
injuries = JOptionPane.showInputDialog("Please enter the injuries the animal has seperated with commas: ");
}
public void setOutcome(int outcome) {
oiput = JOptionPane.showInputDialog("Please enter a '0' if the animal has or will be terminated or a '1' if the animal has been saved: ");
outcome = Integer.parseInt(oiput);
}
public void giveShots() {
boolean giveShots;
if (giveShots = true) {
}
}
public void needShots() {
boolean needShots;
}
}