Java在从另一个类调用方法时找不到符号。

huangapple go评论66阅读模式
英文:

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&lt;Atlet&gt; atleter;

    public Klub(String navn) {
       this.navn = navn;
       atleter = new ArrayList&lt;&gt;();
    }
    
    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(&quot;SWU United&quot;);
       

        Atlet atlet1 = new Atlet(&quot;Pan&quot;, &quot;Tennis&quot;, 43.5, 21);
        Atlet atlet2 = new Atlet(&quot;Peter&quot;, &quot;Golf&quot;, 41.5, 29);
        Atlet atlet3 = new Atlet(&quot;Robby&quot;, &quot;Tennis&quot;, 43.5, 20);
        Atlet atlet4 = new Atlet(&quot;Niklas Klein&quot;, &quot;Maisball&quot;, 21.2, 23);
        Atlet atlet5 = new Atlet(&quot;Linni Maister&quot;, &quot;Maisball&quot;, 44.1, 32);
        Atlet atlet6 = new Atlet(&quot;Dennis Michael&quot;, &quot;Maisball&quot;, 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 + &quot; (&quot; + alder + &quot;) - &quot; + 
        sportsgren + &quot;: &quot; + pris + &quot;kr (&quot; + predSalgspris() + &quot;kr)&quot;);
    }
    
}

答案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();

造成这个问题的原因是根据你的代码,你有一个名为minKlubKlub类实例。方法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上调用方法。重要的是,你要将minKlubdinKlub视为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(&quot;SWU United&quot;);
        Klub dinKlub = new Klub(&quot;SEU United&quot;)
       
        Atlet atlet1 = new Atlet(&quot;Pan&quot;, &quot;Tennis&quot;, 43.5, 21);
        Atlet atlet2 = new Atlet(&quot;Peter&quot;, &quot;Golf&quot;, 41.5, 29);
        minKlub.addAtlet(atlet1);
        minKlub.addAtlet(atlet2);

        Atlet atlet3 = new Atlet(&quot;Mads&quot;, &quot;Fotball&quot;, 123, 123);
        Atlet atlet4 = new Atlet(&quot;Mikkelsen&quot;, &quot;Curling&quot;, 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.

huangapple
  • 本文由 发表于 2020年9月25日 03:46:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64053399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定