【问题标题】:Intellij cannot resolve method even though it is public (Java)Intellij 无法解析方法,即使它是公共的(Java)
【发布时间】:2021-10-08 11:43:21
【问题描述】:

我用谷歌搜索了很多,使缓存无效,但我似乎找不到答案。作为背景,这是一个使用约束求解的时间表系统。我有一个名为Period 的自定义类,带有公共方法getStart()getEnd()。我还有一个抽象类Constraint<V, D> 和一个子类DemAvailConstratint<Demonstrator, Period>。正是在这个子类中(同样在另一个子类中),PeriodDemonstrator 中的方法无法解析。

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


【解决方案1】:

经过各种尝试使缓存失效、更改 JDK 等,我删除了 DemAvailConstraint 类声明中的 &lt;Demonstrator, Period&gt; 部分。我想我知道为什么会这样。添加那段代码使DemonstratorPeriod 成为占位符类型,而不是引用我的自定义类。这就是该类无法访问我的方法的原因。我在抽象类Constraint 中保留了占位符代码&lt;V, D&gt;(变量和域),现在我想知道是否真的需要在子类中指定这些类型。

【讨论】:

    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 2014-01-03
    • 2016-12-02
    相关资源
    最近更新 更多