在Java中使用指针作为参数调用参数

use*_*083 1 java parameters methods pointers identifier

我有以下问题:

当在方法调用中使用指针作为参数时,我得到"错误:标识符预期"错误.

这是我的代码:

class Data {
    Person a = new Person("John");
    Person b = new Person("Mary");

    a.willHug(b);       // Gets error: <identifier> at this line
}

class Person {
    private String name;
    private Person hugs;

    Person(String n){
        this.name = n;
    }

    public void willHug(Person p) {
        hugs = p;
    }    
}
Run Code Online (Sandbox Code Playgroud)

Cac*_*nta 5

您应该将此代码放在方法中以执行它:

例如,主要方法:

class Data {

    public static void main(String args[]){
         Person a = new Person("John");
         Person b = new Person("Mary");

         a.willHug(b);       // Gets error: <identifier> at this line

    }
}
Run Code Online (Sandbox Code Playgroud)

我想你应该阅读这个 SO的问题,以便更好地理解如何在Java中传递参数.

希望能帮助到你.