【问题标题】:c# oop inheritance errorc# oop继承错误
【发布时间】:2014-04-03 21:49:10
【问题描述】:

错误 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**
        }          
    }

【问题讨论】:

  • 您的学生班级没有空的构造函数。它需要一个 nume 和一个 varsta 等。
  • 谢谢。我还需要基类中的空构造函数。再次感谢您。

标签: c# oop inheritance


【解决方案1】:

您需要在student 类中添加一个无参数构造函数

public student()
{

}

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

【讨论】:

    【解决方案2】:

    你的构造函数需要你没有提供的参数

       x = new student();  
    

    期望参数

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

    【讨论】:

      猜你喜欢
      • 2013-08-05
      • 1970-01-01
      • 2016-06-25
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      • 1970-01-01
      相关资源
      最近更新 更多