在Java方法调用中使用指针作为参数
我有以下问题: 在方法调用中使用指针作为参数时,我得到了“error:identifier expected”错误 这是我的代码:在Java方法调用中使用指针作为参数,java,pointers,methods,parameters,identifier,Java,Pointers,Methods,Parameters,Identifier,我有以下问题: 在方法调用中使用指针作为参数时,我得到了“error:identifier expected”错误 这是我的代码: class Data { Person a = new Person("John"); Person b = new Person("Mary"); a.willHug(b); // Gets error: <identifier> at this line } class Person { private
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;
}
}
类数据{
人员a=新人(“约翰”);
人员b=新人(“玛丽”);
a、 willHug(b);//获取错误:在此行
}
班主任{
私有字符串名称;
私人拥抱;
人(字符串n){
this.name=n;
}
公共图书馆(p人){
hugs=p;
}
}
您需要在a
上的操作周围围绕一个方法,一个类方法、一个main()
方法,甚至一个构造函数:
class Data {
Person a = new Person("John");
Person b = new Person("Mary");
public Data() {
a.willHug(b);
}
}
您需要在
a
上的操作周围围绕一个方法,一个class方法、一个main()
方法,甚至一个构造函数:
class Data {
Person a = new Person("John");
Person b = new Person("Mary");
public Data() {
a.willHug(b);
}
}
您应该将此代码放入方法中以执行它: 例如,主要方法:
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
}
}
public static void main (String[] args) {
Person a = new Person("John");
Person b = new Person("Mary");
a.willHug(b);
}
类数据{
公共静态void main(字符串参数[]){
人员a=新人(“约翰”);
人员b=新人(“玛丽”);
a、 willHug(b);//获取错误:在此行
}
}
我认为您应该阅读SO的问题,以便更好地理解Java中参数是如何传递的
希望有帮助。您应该将此代码放入方法中以执行它: 例如,主要方法:
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
}
}
public static void main (String[] args) {
Person a = new Person("John");
Person b = new Person("Mary");
a.willHug(b);
}
类数据{
公共静态void main(字符串参数[]){
人员a=新人(“约翰”);
人员b=新人(“玛丽”);
a、 willHug(b);//获取错误:在此行
}
}
我认为您应该阅读SO的问题,以便更好地理解Java中参数是如何传递的
希望有帮助。您正在调用数据类定义中的方法?这是不正确的,您需要一个“main”来执行此操作,或者将其放在另一个方法中。您正在调用数据类定义中的方法吗?这是不正确的,您需要一个“main”来执行此操作,或者将其放在另一个方法中。您需要将代码放在main方法中:
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
}
}
public static void main (String[] args) {
Person a = new Person("John");
Person b = new Person("Mary");
a.willHug(b);
}
同样在Java中,我们不称这些指针,它们只是变量。变量具有对特定对象实例或基元值的引用。您需要将代码放在主方法中:
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
}
}
public static void main (String[] args) {
Person a = new Person("John");
Person b = new Person("Mary");
a.willHug(b);
}
同样在Java中,我们不称这些指针,它们只是变量。变量具有对特定对象实例或基元值的引用。这里缺少一个方法(我介绍了一个名为foo()的方法):
类数据{
人员a=新人(“约翰”);
人员b=新人(“玛丽”);
公共图书馆{
a、 willHug(b);//获取错误:在此行
}
}
班主任{
私有字符串名称;
私人拥抱;
人(字符串n){
this.name=n;
}
公共图书馆(p人){
hugs=p;
}
}
您缺少一个方法(我引入了一个名为foo()的方法):
类数据{
人员a=新人(“约翰”);
人员b=新人(“玛丽”);
公共图书馆{
a、 willHug(b);//获取错误:在此行
}
}
班主任{
私有字符串名称;
私人拥抱;
人(字符串n){
this.name=n;
}
公共图书馆(p人){
hugs=p;
}
}
问题不是因为使用了指针(在Java中称为引用),而是因为这行:
a、 威尔胡格(b)
在任何方法之外。在该位置只能有声明或初始化块({})。问题不是因为使用了指针(在Java中称为引用),而是因为这行: a、 威尔胡格(b)
在任何方法之外。您只能在该位置使用声明或初始化块({})。谢谢!我用主法上了第三节课。通过同步创建数据类的对象并在主方法中调用该方法,问题得到了解决。再次感谢:)谢谢!我用主法上了第三节课。通过同步创建数据类的对象并在主方法中调用该方法,问题得到了解决。再次感谢:)