抽象类错误.原因:实际论证和正式论证的长度不同



我是Java编程的初学者,明天有考试,但还是看不懂抽象类,因为每个抽象类都给我无穷无尽的错误,我看过书,在网上搜索过,但我对自己感到非常失望。

好吧,这是我最新的练习:应该祝贺周年纪念日!

这是抽象基类

abstract class Pessoa
{
    private int dia, mes, ano;
    Pessoa (int dia, int mes, int ano)
    {
        this.dia = dia;
        this.mes = mes;
        this.ano = ano;
    }
    public void setDia(int dia){ this.dia = dia;}
    public void setMes(int mes){ this.mes = mes;}
    public void setAno(int ano){ this.ano = ano;}
    public int getDia(){return dia;}
    public int getMes(){ return mes;}
    public int getAno(){ return ano;}
    abstract int aniversario();
}

这个是继承方法的派生类

import java.util.Date;
class Cliente extends Pessoa 
{
    int aniversario()
       {
       int d = data.get(Calendar.DAY_OF_MONTH);
       int m = data.get(Calendar.MONTH);
       if ( d== dia && m == mes)
            return "Parabéns pelo seu aniversário! ";
    }
}

错误是:

constructor Pessoa in class Pessoa cannot be applied to given types;
required: java.lang.String,int,java.lang.String,int,int,int
found: no arguments
reason: actual and formal argument lists differ in length
the operator that you use cannot be used for the
type of value you are using it for. You are either
using the wrong type here, or the wrong operator.

也许很明显,但我看不到!(拜托,对不起英语不好)

Pessoa中没有不带参数的默认构造函数。 如果您不显式调用默认构造函数(没有参数),则每个子类都会隐式调用。 但是在Cliente中没有这样的显式调用,如果没有默认的超类构造函数,Java就不能调用。

Cliente 中添加一个构造函数,该构造函数在 Pessoa 中显式调用超类构造函数。

public Cliente(int dia, int mes, int ano)
{
    super(dia, mes, ano);
}

这个问题发生在类的构造函数上;它与Pessoa abstract无关。

你需要显式调用超类的构造函数

class Cliente extends Pessoa 
{
    Cliente(int dia, int mes, int ano) {
      super(dia, mes, ano);
    }
    int aniversario()
       {
       int d = data.get(Calendar.DAY_OF_MONTH);
       int m = data.get(Calendar.MONTH);
       if ( d== dia && m == mes)
            return "Parabéns pelo seu aniversário! ";
    }
}

相关内容

最新更新