【发布时间】:2021-10-08 11:43:21
【问题描述】:
我用谷歌搜索了很多,使缓存无效,但我似乎找不到答案。作为背景,这是一个使用约束求解的时间表系统。我有一个名为Period 的自定义类,带有公共方法getStart() 和getEnd()。我还有一个抽象类Constraint<V, D> 和一个子类DemAvailConstratint<Demonstrator, Period>。正是在这个子类中(同样在另一个子类中),Period 和 Demonstrator 中的方法无法解析。
在DemAvailConstraint 中,在this.period 上调用时显示错误的方法是getStart() 和getEnd(),但在其他Period 对象上调用时则不是。同样,在 satisfied() 方法中的 this.getDemonstrator() 上调用 isAvailable() 也会发生这种情况。这是Demonstrator 中的公共方法。知道为什么会发生这种情况吗?
这里是类(为了清楚起见删除了一些方法):
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);
}
}
【问题讨论】:
-
你确定你已经导入了你自己的
Period类吗?因为还有一个java.time.Period,它没有那些方法 -
请提供示例项目。
-
感谢@QBrute - 我将其更改为
Lab,但问题仍然存在。它还会影响Demonstrator。 @Andrey,您是指整个项目吗?那里有很多类,而且它们都没有这个问题 - 你能澄清一下你还需要什么吗?
标签: java intellij-idea polymorphism constraints visibility