gpt4 book ai didi

java - 抽象类错误。原因: actual and formal arguments differ in length

转载 作者:行者123 更新时间:2023-12-02 06:32:15 24 4
gpt4 key购买 nike

我是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 就无法调用默认的父类(super class)构造函数。

Cliente 中添加一个构造函数,该构造函数显式调用 Pessoa 中的父类(super class)构造函数。

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

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

关于java - 抽象类错误。原因: actual and formal arguments differ in length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19960080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com