类不是抽象的,不会覆盖超类中的抽象方法 [英] Class is not abstract and does not override abstract method in superclass

查看:58
本文介绍了类不是抽象的,不会覆盖超类中的抽象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究遗传算法的通用实现.我正在尝试从抽象类 Individual 中为背包问题扩展一个特定的 Individual

I am working on a generic implementation of the Genetic Algorithm. I'm trying to extend a specific Individual for the knapsack problem from the abstract class Individual

package GenericGA;

public abstract class Individual {

    private double fitness;
    private double probability;


    public double getFitness() {
        return fitness;
    }

    public double getProbability() {
        return probability;
    }

    public void updateProb(double totalFitness){
        probability = fitness/totalFitness;
    }


    abstract void updateFitness();
    abstract Individual crossover(Individual partner);
    abstract void mutate(double mutationRate);

}

这是扩展类

package GenericGA.SpecificImplementations;


import GenericGA.Individual;

public class KnapsackIndividual extends Individual {


    void updateFitness(){

    }

    Individual crossover(Individual partner){
        return null;
    }

    void mutate(double mutationRate){

    }

    public static void main(String[] args){
        System.out.println("Hello");
    }

}

由于我使用的是 intellij 并且它给了我错误,我认为这可能是 IDE 的问题,所以我用 javac 编译并得到相同的错误:

Since I was using intellij and it was giving me the error, I thought that could be a problem of the IDE, so I compiled with javac and I get the same error:

GenericGA.SpecificImplementations.KnapsackIndividual is not abstract and does not override abstract method mutate(double) in GenericGA.Individual

没有拼写错误并且签名是正确的,在删除出现错误的 mutate 方法时,其他抽象方法也出现错误.

There are no spelling mistakes and the signatures are correct, and I get the error also for the other abstract methods when removing the mutate method that is giving the error.

此外,如果我在方法之上使用 @Override,它会说

Moreover if I use @Override on top of the methods, it says

Method does not override method from its superclass

我在这里遗漏了什么?

提前致谢.

推荐答案

看起来您的抽象 mutate 方法对子类不可见,因为它具有默认访问权限(即包私有),并且 KnapsackIndividual 子类位于不同的包中.

It looks like your abstract mutate method is not visible to the sub-class, since it has default access (which is package private), and the KnapsackIndividual sub-class is in a different package.

abstract方法改为:

protected abstract void mutate(double mutationRate);

并相应地更改覆盖方法:

and change the overriding method accordingly:

@Override
protected void mutate(double mutationRate){

}

这篇关于类不是抽象的,不会覆盖超类中的抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆