在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();
}
}
通過以上方式,不同類之間可以方便地傳遞參數。根據具體的需求和場景,選擇合適的方式來傳遞參數。