Intellij 无法解析方法,即使它是 public (Java)
Intellij cannot resolve method even though it is public (Java)
我用谷歌搜索了很多次,使缓存无效,但我似乎找不到答案。对于背景,这是一个使用约束求解的时间表系统。我有一个名为 Period
的自定义 class,使用 public 方法 getStart()
和 getEnd()
。我还有一个摘要classConstraint<V, D>
和一个子classDemAvailConstratint<Demonstrator, Period>
。正是在这个subclass(另一个类似)中Period
和Demonstrator
中的方法无法解析。
在 DemAvailConstraint
中,显示错误的方法是 getStart()
和 getEnd()
在 this.period
上调用时,但在其他 Period
对象上调用时则不是。同样,当在 satisfied()
方法中调用 this.getDemonstrator()
时,isAvailable()
也会发生这种情况。这是 Demonstrator
中的 public 方法。知道为什么会这样吗?
以下是 classes(为清楚起见删除了一些方法):
public abstract class Constraint<V, D> {
private V variable;
public Constraint(V variable) {
this.variable = variable;
}
public abstract boolean satisfied();
public abstract int score();
protected V getVariable() {
return this.variable;
}
protected void setVariable(V variable) {
this.variable = variable;
}
}
public class DemAvailConstraint<Demonstrator, Period> extends Constraint {
private Period period;
private ArrayList<AvailBlock> periodAsBlocks;
public DemAvailConstraint(Demonstrator demonstrator, Period period) {
super(demonstrator);
this.period = period;
periodAsBlocks = new ArrayList<>();
periodAsBlocks.add( AvailBlock.from(this.period.getStart()) );
while ( periodAsBlocks.get( periodAsBlocks.size() - 1 ).getEnd().isBefore(this.period.getEnd()) ) {
periodAsBlocks.add( AvailBlock.from(periodAsBlocks.get( periodAsBlocks.size() - 1 ).getEnd()) );
}
}
@Override
public boolean satisfied() {
// now we check if the demonstrator is available for each block
for (AvailBlock block : this.periodAsBlocks) {
if ( !this.getDemonstrator().isAvailable(block) ) return false;
}
return true;
}
...
public Demonstrator getDemonstrator() {
return (Demonstrator) super.getVariable();
}
}
public class Period {
private String name;
private LocalDateTime start;
// duration in seconds
private int duration;
private Lecturer lecturer;
private ArrayList<Demonstrator> dems;
private ArrayList<Skill> skills;
/**
* Basic constructor. Use the factory method from() to make a period from AvailBlock objects.
* @param name
* @param start
* @param duration
* @param lecturer
*/
public Period(String name, LocalDateTime start, int duration, Lecturer lecturer) {
this.name = name;
this.lecturer = lecturer;
this.dems = new ArrayList<>();
this.skills = new ArrayList<>();
this.duration = duration;
this.start = start;
}
/**
* Static factory method that can create a period from a list of consecutive AvailBlock objects.
* @param name
* @param blocks
* @param lecturer
* @return
*/
public static Period from(String name, List<AvailBlock> blocks, Lecturer lecturer)
throws IllegalArgumentException {
if ( !AvailBlock.isConsecutive(blocks) ) {
throw new IllegalArgumentException( "Non-consecutive AvailBlock objects." );
}
ArrayList<AvailBlock> blocksSorted = new ArrayList<>(blocks);
Collections.sort(blocksSorted);
LocalDateTime start = blocksSorted.get(0).getStart();
int duration = blocksSorted.size();
return new Period(name, start, duration, lecturer);
}
...
public LocalDateTime getStart() {
return start;
// more getters and setters ...
public LocalDateTime getEnd() {
return this.start.plusSeconds(this.duration);
}
}
在尝试使缓存无效、更改 JDK 等各种尝试后,我删除了 DemAvailConstraint
class 声明中的 <Demonstrator, Period>
部分。我想我知道为什么会这样。添加那段代码使 Demonstrator
和 Period
成为占位符类型,而不是引用我的自定义 classes。这就是 class 无法访问我的方法的原因。我在摘要 class Constraint
中保留了占位符代码 <V, D>
(变量和域),我现在想知道我是否真的需要在子 [=18] 中指定这些类型=].
我用谷歌搜索了很多次,使缓存无效,但我似乎找不到答案。对于背景,这是一个使用约束求解的时间表系统。我有一个名为 Period
的自定义 class,使用 public 方法 getStart()
和 getEnd()
。我还有一个摘要classConstraint<V, D>
和一个子classDemAvailConstratint<Demonstrator, Period>
。正是在这个subclass(另一个类似)中Period
和Demonstrator
中的方法无法解析。
在 DemAvailConstraint
中,显示错误的方法是 getStart()
和 getEnd()
在 this.period
上调用时,但在其他 Period
对象上调用时则不是。同样,当在 satisfied()
方法中调用 this.getDemonstrator()
时,isAvailable()
也会发生这种情况。这是 Demonstrator
中的 public 方法。知道为什么会这样吗?
以下是 classes(为清楚起见删除了一些方法):
public abstract class Constraint<V, D> {
private V variable;
public Constraint(V variable) {
this.variable = variable;
}
public abstract boolean satisfied();
public abstract int score();
protected V getVariable() {
return this.variable;
}
protected void setVariable(V variable) {
this.variable = variable;
}
}
public class DemAvailConstraint<Demonstrator, Period> extends Constraint {
private Period period;
private ArrayList<AvailBlock> periodAsBlocks;
public DemAvailConstraint(Demonstrator demonstrator, Period period) {
super(demonstrator);
this.period = period;
periodAsBlocks = new ArrayList<>();
periodAsBlocks.add( AvailBlock.from(this.period.getStart()) );
while ( periodAsBlocks.get( periodAsBlocks.size() - 1 ).getEnd().isBefore(this.period.getEnd()) ) {
periodAsBlocks.add( AvailBlock.from(periodAsBlocks.get( periodAsBlocks.size() - 1 ).getEnd()) );
}
}
@Override
public boolean satisfied() {
// now we check if the demonstrator is available for each block
for (AvailBlock block : this.periodAsBlocks) {
if ( !this.getDemonstrator().isAvailable(block) ) return false;
}
return true;
}
...
public Demonstrator getDemonstrator() {
return (Demonstrator) super.getVariable();
}
}
public class Period {
private String name;
private LocalDateTime start;
// duration in seconds
private int duration;
private Lecturer lecturer;
private ArrayList<Demonstrator> dems;
private ArrayList<Skill> skills;
/**
* Basic constructor. Use the factory method from() to make a period from AvailBlock objects.
* @param name
* @param start
* @param duration
* @param lecturer
*/
public Period(String name, LocalDateTime start, int duration, Lecturer lecturer) {
this.name = name;
this.lecturer = lecturer;
this.dems = new ArrayList<>();
this.skills = new ArrayList<>();
this.duration = duration;
this.start = start;
}
/**
* Static factory method that can create a period from a list of consecutive AvailBlock objects.
* @param name
* @param blocks
* @param lecturer
* @return
*/
public static Period from(String name, List<AvailBlock> blocks, Lecturer lecturer)
throws IllegalArgumentException {
if ( !AvailBlock.isConsecutive(blocks) ) {
throw new IllegalArgumentException( "Non-consecutive AvailBlock objects." );
}
ArrayList<AvailBlock> blocksSorted = new ArrayList<>(blocks);
Collections.sort(blocksSorted);
LocalDateTime start = blocksSorted.get(0).getStart();
int duration = blocksSorted.size();
return new Period(name, start, duration, lecturer);
}
...
public LocalDateTime getStart() {
return start;
// more getters and setters ...
public LocalDateTime getEnd() {
return this.start.plusSeconds(this.duration);
}
}
在尝试使缓存无效、更改 JDK 等各种尝试后,我删除了 DemAvailConstraint
class 声明中的 <Demonstrator, Period>
部分。我想我知道为什么会这样。添加那段代码使 Demonstrator
和 Period
成为占位符类型,而不是引用我的自定义 classes。这就是 class 无法访问我的方法的原因。我在摘要 class Constraint
中保留了占位符代码 <V, D>
(变量和域),我现在想知道我是否真的需要在子 [=18] 中指定这些类型=].