我有一个名为“ConstituentSet”的类。它有一种方法,即“getNucleusInConstSet()”,其输出来自“Proposition”类。新类“Proposition”有另一个方法,即“getProperty()”。我想知道“ConstituentSet”类中我的“命题核心”的属性是什么。但我不知道该怎么做。
我写如下,但它不起作用。 (ConstituentSet.getNucleusInConstSet()).getProperty())
public class ConstituentSet{
// Constructor
private Proposition nucleusInConstSet;
public Proposition getNucleusInConstSet() {
return nucleusInConstSet;
}
}
public class Proposition{
//Constructor
private Property property;
public Property getProperty() {
return this.type;
}
}
最佳答案
你有:
(ConstituentSet.getNucleusInConstSet()).getProperty()
但是您需要调用 ConstituentSet
的实例
例如
ConstituentSet cs = new ConstituentSet();
cs.getNucleusInConstSet().getProperty();
请注意,这种习惯用法(链式方法调用)可能会很痛苦。如果您的方法之一返回 null,则很难理解它是哪一个(不使用调试器)。另请注意,a().b().c().d()
形式的调用是破坏封装的一种微妙形式(a
表明它具有b
,表明它有一个c
等)
关于java - 依次调用不同类的 get 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17702259/