使用继承时如何创建对象?

How do i create objects when working with inheritance?

所以,我有 superclass 角色,和 subclasses,Estudiante 和 Docente。 属性 nombre、cedula、mail 是我想在 Estudiante 和 Docente 上拥有的属性,因为它们都有,但由于 Estudiante 和 Docente 都是 Persona 的,我可以使用继承。所有这些对象都有它们的 get/set 和 tostring 方法。 我发布的最后一个代码是我拥有的 UI。我想按一个按钮并创建一个学生,但我不能,因为它告诉我我给出了比我能给出的更多的论点,所以..我该怎么做?我希望我能很好地解释自己。

我还没有尝试过任何东西,因为我真的不知道该尝试什么。 我第一次编写该代码时并没有考虑使用这个超级 class 角色,但我被告知我绝对必须那样做。

public class Estudiante extends Persona{
    private int numero;
    private int semestre;
public class Docente extends Persona {    
    private int anoingreso;
public class Persona {
    private String nombre;
    private int cedula;
    private String mail;
private void BotonCrearEstudianteActionPerformed(java.awt.event.ActionEvent evt) {                                                     
        Estudiante=new Estudiante(NombreEstudiante,CedulaEstudiante,MailEstudiante,NumeroEstudiante,SemestreEstudiante);

我希望在这种情况下创建一个 Estudiante,但我也打算创建 Docente,然后与这两个中的许多人组成团队,但我不能,因为我之前说过太多论据。

在每个 class 中,您还需要有一个构造函数 - 基本上是您用来定义如何创建这些对象之一的构造函数。像这样:

public class Persona{
    private String nombre;
    private int cedula;
    private String mail;


    public Estudiante(/*Insert the parameters you need, but do not call them by the same thing as your instance variables above*/){
       /*this block will execute when you create an Estudiante object*/
    }
}

但是当你到达 child classes(即 Estudiante 和 Docente)时,你可以在构造函数中使用 super() 方法,它随叫随到 运行 父 class' 构造函数。

Give this a read too.