无法初始化构造函数 [英] can not initialize constructor

查看:126
本文介绍了无法初始化构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主要是我试图初始化构造函数,但我不能

recDB.insert(new Record(22, "Mike", 2500));//------这是我的错误


in main i tried to initialze constructor but i could not

recDB.insert(new Record(22, "Mike", 2500));//------this is my error


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1
{
    public class Class1
    {
        public class Record
        {
            public int ssn; // Primary Key
            public String name;
            public double pay;
            Record(int newSSN, String newName, double newPay)
            {
                ssn = newSSN;
                name = newName;
                pay = newPay;
            }
        }
        //------------------------------
        public class RecordDB
        {
            private static int maxRecords = 200;
            private int numRecords = 0;
            private Record[] recordList = new Record[maxRecords];
            //-------------------------------------------
            public void insert(Record rec)
            {
                if (numRecords == maxRecords - 1)
                {
                    return;
                }
                printAll();
            }
            //-------------------------------------------
            public void delete(int newSSN)
            {
                printAll();
            }
            //-------------------------------------------
            //public Record search(int newSSN)
            public void search(int newSSN)
            {
                int first = 0, last = numRecords - 1;
                // Print All records
                printAll();

                //Record recx = new Record(30, "JohnX", 5000);
                //return recx;

            } // end of search function
            //------------------------------------------
            private void printAll() {
                Console.Write("---------------------------------");
                for (int i = 0; i < numRecords; i++)
                {
                    Console.Write("" + i + " " + recordList[i].ssn +
                    "  " + recordList[i].name + " " + recordList[i].pay);
                }
            }
            //------------------------------------------
        public static void main(String[] args)
        {
        RecordDB recDB = new RecordDB();

        recDB.insert(new Record(22, "Mike", 2500));//<-----------------Error

        }

        }
    }
}

推荐答案

将构造函数更改为此:

Change your constructor to this:

public Record(int newSSN, String newName, double newPay)



您可能还需要考虑将这些嵌套的类移出.

干杯.



You also might want to consider moving those nested classes out.

Cheers.


Khalid SabtanFlaviu 2写道:
Khalid SabtanFlaviu 2 wrote:

Record(int newSSN,String newName,double newPay)
{
ssn = newSSN;
名称= newName;
pay = newPay;
}

Record(int newSSN, String newName, double newPay)
{
ssn = newSSN;
name = newName;
pay = newPay;
}



我会尝试:



I would try with:

public Record(int newSSN, String newName, double newPay)
            {
                ssn = newSSN;
                name = newName;
                pay = newPay;
            }


这篇关于无法初始化构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆