简体   繁体   English

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

[英]Java cannot find symbol when calling a method from another class

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.我试图从我的演示类调用 klub 类中的方法 show,并在演示类(从我的 klub 类中)调用方法 visAlleAtleter,但我收到一个错误,说它找不到符号,然后方法的名称。 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和我的 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)");
    }
    
}

Method visAlleAtleter is not static so your can't call it as you do Klub.visAlleAtleter() .方法visAlleAtleter不是静态的,所以你不能像Klub.visAlleAtleter()那样调用它。 Just call method on object minKlub.visAlleAtleter() .只需在对象minKlub.visAlleAtleter()上调用方法。

The problem lies in the line Klub.visAlleAtleter();问题在于Klub.visAlleAtleter(); in the main method in Demo .Demomain方法中。 To correct this you have to replace it with minKlub.visAlleAtleter();要纠正此问题,您必须将其替换为minKlub.visAlleAtleter(); . .

The reason for this is that according to your code, you have an instance of the class Klub called minKlub .这样做的原因是,根据您的代码,您有一个名为minKlub Klub类的实例。 The method visAlleAtleter() is bound to any instance of the class Klub , not the class Klub itself.方法visAlleAtleter()绑定到类Klub任何实例,而不是类Klub本身。

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 .在这个例子中,当调用Klub.visAlleAtleter() ,不可能知道 Java 应该调用minKlubb还是dinKlubb上的方法。 Its important that you see minKlub and dinKlub as physical instances of the Klub class.minKlubdinKlub视为Klub类的物理实例很重要。

Coincidentally, writing Klub.someArbitraryMethod() is actually a commonly used feature in java, where someArbitraryMethod() is statically declared.巧的是,写Klub.someArbitraryMethod()其实是java中常用的一个特性,其中someArbitraryMethod()是静态声明的。 This way of writing is only possible if you declare the method as static :仅当您将该方法声明为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.这意味着该方法不属于类Klub任何物理实例,而是属于类Klub本身。 Doing正在做

minKlubb.someArbitraryMethod();

will therefor cause a compilation error.因此会导致编译错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM