C# OOP 继承错误

本文关键字:错误 继承 OOP | 更新日期: 2023-09-27 17:56:26

错误 1 "WindowsFormsApplication3.student" 不包含采用 0 参数 C:''Users''Marius''Documents''Visual Studio 2012''Projects''pregatiret''WindowsFormsApplication3''Form1.cs 32 的构造函数

public class persoana
{
    public string nume;

    public int varsta;

    public persoana(string num, int var)
    {
        this.nume = num;
        this.varsta = var;
    }

  public class student : persoana
   {
   public string facultate;
   public int grupa;
   public student(string nume,int varsta,string fac,int grupa) : base(nume,varsta){
       this.facultate = fac;
       this.grupa = grupa;

    }
 public partial class Form1 : Form
{
    persoana y;
    student x;
    ArrayList listaStudenti = new ArrayList();
    ListViewItem itm;
    public Form1()
    {
        InitializeComponent();
    }
    private void inserareToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Form2 m = new Form2();
        m.ShowDialog();
        if (m.DialogResult == DialogResult.OK)
        {
            x = new student();           **//I GET ERROR HERE**
        }          
    }

C# OOP 继承错误

您需要

将无参数构造函数添加到student

public student()
{
}

当您声明一个类并且不添加任何构造函数时,它会从object类继承默认的无参数构造函数,但如果添加接受一些参数的构造函数,则默认构造函数将被忽略。您必须手动添加它。

构造函数需要您未提供的参数

   x = new student();  

期望参数

  public student(string nume,int varsta,string fac,int grupa) : base(nume,varsta){