[英]How do you pass an instance of a class as an argument for a method of that class?
Let's say I have this code. 假设我有这个代码。 When using an instance of ClassA, how do I pass the instance of the class to the method methodA
? 使用ClassA的实例时,如何将类的实例传递给方法methodA
?
public class ClassA {
public static int methodA(ClassA class) {
return 1;
}
}
//This is wrong but this is what I'm trying to do
public class Main {
public static void main(String [] args) {
ClassA classa = new ClassA();
classa.methodA(classa);
}
}
The way to achieve this is correct. 实现这一目标的方法是正确的。 Just use another name, because class
is a reserved keyword for class definition 只需使用其他名称,因为class
是类定义的保留关键字
public static int methodA(ClassA instance) {
return 1;
}
Your approach works but you must not use the keyword class
as a variable name. 您的方法有效,但您不能将关键字class
用作变量名。 Try 尝试
public class ClassA {
public static int methodA(ClassA clazz) {
return 1;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.