英文:
Java cannot find symbol when calling a method from another class
问题
我试图从我的演示类中调用 Klub 类中的 show 方法,并从我的 Klub 类中调用演示类中的 visAlleAtleter 方法,但是我得到一个错误,显示找不到符号,然后是方法的名称。我尝试过指定我希望从哪里调用,但我不确定如何做。这是我的 Klub 类:
import java.util.ArrayList;
public class Klub {
private String navn;
private ArrayList<Atlet> atleter;
public Klub(String navn) {
this.navn = navn;
atleter = new ArrayList<>();
}
public void addAtlet(Atlet atlet) {
atleter.add(atlet);
}
public void visAlleAtleter() {
System.out.println(navn);
for(Atlet atlet: atleter) {
atlet.display();
}
}
public void visAlleAtleter2(boolean samletValue) {
if(samletValue = true) {
Atlet.show();
} else {
}
}
}
这是我的 Demo 类:
public class Demo {
public static void main(String[] args) {
Klub minKlub = new Klub("SWU United");
Atlet atlet1 = new Atlet("Pan", "Tennis", 43.5, 21);
Atlet atlet2 = new Atlet("Peter", "Golf", 41.5, 29);
Atlet atlet3 = new Atlet("Robby", "Tennis", 43.5, 20);
Atlet atlet4 = new Atlet("Niklas Klein", "Maisball", 21.2, 23);
Atlet atlet5 = new Atlet("Linni Maister", "Maisball", 44.1, 32);
Atlet atlet6 = new Atlet("Dennis Michael", "Maisball", 32.5, 11);
minKlub.addAtlet(atlet1);
minKlub.addAtlet(atlet2);
minKlub.addAtlet(atlet3);
minKlub.addAtlet(atlet4);
minKlub.addAtlet(atlet5);
minKlub.addAtlet(atlet6);
Klub.visAlleAtleter();
}
}
这是我的 Atlet 类:
public class Atlet {
private String navn;
private String sportsgren;
private double pris;
private int alder;
public Atlet(String navn, String sportsgren, double pris, int alder) {
this.navn = navn;
this.sportsgren = sportsgren;
this.pris = pris;
this.alder = alder;
}
public void oppdaterPris(double nyPris) {
pris = nyPris;
}
public double getPris() {
return pris;
}
public double predSalgspris() {
return pris-0.95*Math.abs(25-alder);
}
public void display() {
System.out.println(navn + " (" + alder + ") - " +
sportsgren + ": " + pris + "kr (" + predSalgspris() + "kr)");
}
}
英文:
Im trying to call the method show in the klub class, from my demo class, and to call the method visAlleAtleter in the demo class (from my klub class), but I get an error saying it can't find the symbol and then the name of the method. I have tried to specify where I want it from, but I'm not certain how to do it. This is my Klub class
import java.util.ArrayList;
public class Klub {
private String navn;
private ArrayList<Atlet> atleter;
public Klub(String navn) {
this.navn = navn;
atleter = new ArrayList<>();
}
public void addAtlet(Atlet atlet) {
atleter.add(atlet);
}
public void visAlleAtleter() {
System.out.println(navn);
for(Atlet atlet: atleter) {
atlet.display();
}
}
public void visAlleAtleter2(boolean samletValue) {
if(samletValue = true) {
Atlet.show();
} else {
}
}
}
this is my Demo class
public class Demo {
public static void main(String[] args) {
Klub minKlub = new Klub("SWU United");
Atlet atlet1 = new Atlet("Pan", "Tennis", 43.5, 21);
Atlet atlet2 = new Atlet("Peter", "Golf", 41.5, 29);
Atlet atlet3 = new Atlet("Robby", "Tennis", 43.5, 20);
Atlet atlet4 = new Atlet("Niklas Klein", "Maisball", 21.2, 23);
Atlet atlet5 = new Atlet("Linni Maister", "Maisball", 44.1, 32);
Atlet atlet6 = new Atlet("Dennis Michael", "Maisball", 32.5, 11);
minKlub.addAtlet(atlet1);
minKlub.addAtlet(atlet2);
minKlub.addAtlet(atlet3);
minKlub.addAtlet(atlet4);
minKlub.addAtlet(atlet5);
minKlub.addAtlet(atlet6);
Klub.visAlleAtleter();
}
}
and my atlet class
public class Atlet {
private String navn;
private String sportsgren;
private double pris;
private int alder;
public Atlet(String navn, String sportsgren, double pris, int alder) {
this.navn = navn;
this.sportsgren = sportsgren;
this.pris = pris;
this.alder = alder;
}
public void oppdaterPris(double nyPris) {
pris = nyPris;
}
public double getPris() {
return pris;
}
public double predSalgspris() {
return pris-0.95*Math.abs(25-alder);
}
public void display() {
System.out.println(navn + " (" + alder + ") - " +
sportsgren + ": " + pris + "kr (" + predSalgspris() + "kr)");
}
}
答案1
得分: 2
方法 visAlleAtleter
不是静态的,因此您不能像调用 Klub.visAlleAtleter()
那样调用它。
只需在对象 minKlub.visAlleAtleter()
上调用方法。
英文:
Method visAlleAtleter
is not static so your can't call it as you do Klub.visAlleAtleter()
.
Just call method on object minKlub.visAlleAtleter()
.
答案2
得分: 1
问题出在Demo
类的main
方法中的Klub.visAlleAtleter();
这一行代码上。要纠正这个问题,你需要将它替换为minKlub.visAlleAtleter();
。
造成这个问题的原因是根据你的代码,你有一个名为minKlub
的Klub
类实例。方法visAlleAtleter()
绑定到Klub
类的任何实例,而不是Klub
类本身。
编辑:
为了说明你的尝试可能存在的问题,考虑以下示例:
public class Demo {
public static void main(String[] args) {
Klub minKlub = new Klub("SWU United");
Klub dinKlub = new Klub("SEU United");
Atlet atlet1 = new Atlet("Pan", "Tennis", 43.5, 21);
Atlet atlet2 = new Atlet("Peter", "Golf", 41.5, 29);
minKlub.addAtlet(atlet1);
minKlub.addAtlet(atlet2);
Atlet atlet3 = new Atlet("Mads", "Fotball", 123, 123);
Atlet atlet4 = new Atlet("Mikkelsen", "Curling", 123, 123);
dinKlub.addAtlet(atlet3);
dinKlub.addAtlet(atlet4);
Klub.visAlleAtleter();
}
}
在这个示例中,当调用Klub.visAlleAtleter()
时,Java 无法确定是应该在minKlub
上调用方法还是在dinKlub
上调用方法。重要的是,你要将minKlub
和dinKlub
视为Klub
类的物理实例。
巧合的是,写Klub.someArbitraryMethod()
实际上是 Java 中常用的一种特性,其中someArbitraryMethod()
是静态声明的。这种写法只有在你将方法声明为static
时才可能:
public static void someArbitraryMethod() {...}
这意味着这个方法不属于Klub
类的任何物理实例,而是属于Klub
类本身。因此,执行
minKlub.someArbitraryMethod();
会导致编译错误。
英文:
The problem lies in the line Klub.visAlleAtleter();
in the main
method in Demo
. To correct this you have to replace it with minKlub.visAlleAtleter();
.
The reason for this is that according to your code, you have an instance of the class Klub
called minKlub
. The method visAlleAtleter()
is bound to any instance of the class Klub
, not the class Klub
itself.
EDIT:
To illustrate why your attempt could be an issue, consider this example:
public class Demo {
public static void main(String[] args) {
Klub minKlub = new Klub("SWU United");
Klub dinKlub = new Klub("SEU United")
Atlet atlet1 = new Atlet("Pan", "Tennis", 43.5, 21);
Atlet atlet2 = new Atlet("Peter", "Golf", 41.5, 29);
minKlub.addAtlet(atlet1);
minKlub.addAtlet(atlet2);
Atlet atlet3 = new Atlet("Mads", "Fotball", 123, 123);
Atlet atlet4 = new Atlet("Mikkelsen", "Curling", 123, 123);
dinKlub.addAtlet(atlet3);
dinKlub.addAtlet(atlet4);
Klub.visAlleAtleter();
}
}
In this example, when calling Klub.visAlleAtleter()
, it is impossible to know whether Java should call the method on minKlubb
or dinKlubb
. Its important that you see minKlub
and dinKlub
as physical instances of the Klub
class.
Coincidentally, writing Klub.someArbitraryMethod()
is actually a commonly used feature in java, where someArbitraryMethod()
is statically declared. This way of writing is only possible if you declare the method as static
:
public static void someArbitraryMethod() {...}
This means that the method doesn't belong to any physical instances of the class Klub
, but rather it belongs to the class Klub
itself. Doing
minKlubb.someArbitraryMethod();
will therefor cause a compilation error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论