[英]How to use public method in another class
I have a class named PointOfSale which have a public method getTpid, but I failed when I want to call this method in my main class, I can only call "TPID_FIELD_NUMBER", how to solve this problem?我有一个名为 PointOfSale 的 class,它有一个公共方法 getTpid,但是当我想在我的主 class 中调用此方法时失败了,我只能调用“TPID_FIELD_NUMBER”,如何解决这个问题?
PointOfSale ps = new PointOfSale();
ps.TPID_FIELD_NUMBER; //correct
ps.getTpid();// error
public final class PointOfSale implements PointOfSaleOrBuilder {
public static final int TPID_FIELD_NUMBER = 1;
private int tpid_;
@java.lang.Override
public int getTpid() {
return tpid_;
}
}
public interface PointOfSaleOrBuilder {
int getTpid();
}
public static void main(String[] args) {
PointOfSale ps = new PointOfSale();
ps.getTpid();
}
I think that you just need to add () at the end of your call.我认为您只需要在通话结束时添加 () 即可。
ps.TPID_FIELD_NUMBER is a call for a constant. ps.TPID_FIELD_NUMBER 是对常量的调用。 If you want to call a method, you should call like this: ps.getTpid();
如果你想调用一个方法,你应该这样调用: ps.getTpid();
The issue is with the line "ps.TPID_FIELD_NUMBER;".问题在于“ps.TPID_FIELD_NUMBER;”行。 This will give "Not a statement" error.
这将给出“Not a statement”错误。 You should assign it to a variable for successful execution.
您应该将其分配给变量以成功执行。
Your code should look like,您的代码应如下所示,
public static void main(String[] args) {
PointOfSale ps = new PointOfSale();
int num = ps.TPID_FIELD_NUMBER;
ps.getTpid();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.