在Java中,可以通过以下几种方式在不同类之间传递参数:
public class ClassA {
public void methodA(int param) {
// do something with param
}
}
public class ClassB {
public void methodB() {
int param = 10;
ClassA obj = new ClassA();
obj.methodA(param);
}
}
public class ClassA {
private int param;
public ClassA(int param) {
this.param = param;
}
}
public class ClassB {
public void methodB() {
int param = 10;
ClassA obj = new ClassA(param);
}
}
public class ClassA {
public int param;
}
public class ClassB {
public void methodB() {
ClassA obj = new ClassA();
obj.param = 10;
}
}
public class ClassA {
public int methodA() {
int param = 10;
return param;
}
}
public class ClassB {
public void methodB() {
ClassA obj = new ClassA();
int param = obj.methodA();
}
}
通过以上方式,不同类之间可以方便地传递参数。根据具体的需求和场景,选择合适的方式来传递参数。