0

私は 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.

おそらく明らかですが、私には見えません!(下手な英語で申し訳ありません)

4

2 に答える 2

2

Pessoa引数を取らないデフォルトのコンストラクターがありません。明示的に呼び出さない場合、すべてのサブクラスはデフォルトのコンストラクターを (引数なしで) 暗黙的に呼び出します。しかし、 にはそのような明示的な呼び出しはなくCliente、Java はデフォルトのスーパークラス コンストラクターがない場合、それを呼び出すことができません。

Clienteのスーパークラス コンストラクターを明示的に呼び出すコンストラクターを に追加しPessoaます。

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

この問題は、クラスのコンストラクターで発生しています。であることとは何の関係もありませんでしPessoaabstract

于 2013-11-13T17:13:57.677 に答える
0

スーパークラスのコンストラクターを明示的に呼び出す必要があります

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! ";
    }
}
于 2013-11-13T17:15:06.857 に答える