简体   繁体   English

Java-范围内没有任何封闭类型的实例…

[英]Java - No enclosing instance of the type … is accessible in scope

I have this class: 我有这个课:

public class PetAssembly extends Global
{
    public Socket socket;
    public ConnectionManager user;

    public PetAssembly(Socket socket, ConnectionManager user)
    {
        this.socket = socket;
        this.user = user;
    }

    public void initPet()
    {
        sendPacket(socket, "0|PET|I|0|0|1");
        sendPacket(socket, "0|PET|S|1000|1|0|8000|50000|50000|1000|1000|50000|50000|" + (((user.user.getSpeed() * 30) / 100) + user.user.getSpeed()) + "|testPet");
    }
}

I want to use it: 我要使用它:

case "/pet":
      PetAssembly.this.initPet();
break;

But it gives me this error, how to fix it? 但这给了我这个错误,如何解决? I'm a beginner : No enclosing instance of the type PetAssembly is accessible in scope 我是一个初学者:作用No enclosing instance of the type PetAssembly is accessible in scope

PetAssembly.initPet() is an instance method. PetAssembly.initPet()是一个实例方法。 You first need to construct an object of PetAssembly (an instance of that class), and then have a reference to that object before you can invoke a method on it. 您首先需要构造一个PetAssembly对象(该类的实例),然后对该对象进行引用,然后才能在该对象上调用方法。

PetAssembly pa = new PetAssembly(socket, user); 
// Creates a new PetAssembly object
// and stores a reference to that in the variable pa.
pa.initPet(); 
// Calls the initPet() method on the PetAssembly object referred to by the variable pa.

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

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