Visual C#Complex中构造函数代码的等价物

时间:2013-10-19 13:29:48

标签: c# java class constructor

大家好我在网上搜索如何在C#中实现这个java代码,但我没有运气。我创建了一个超类和一个带参数的基类任何人都可以帮助我。我想用C#实现这段代码。

class People
{
    public String first_name;

    public String last_name;

    public People(String fname, String lname)
    {
        this.first_name = fname;

        this.last_name = lname;
    }
}

class Student extends People
{
    Public int studentID;

    public Student(String fname,String lname,int studid)
    {
       super(fname,lname);

       this.studentID = studid;
    }

}

 Student newStud = new Student('Jessica','Doe','123);

这里是我想用c#实现的代码,而不是调用people类并实例化它,我只想调用继承自people类的学生类。我是C#的新手,任何人都可以帮助我。

2 个答案:

答案 0 :(得分:3)

请检查:

abstract class People
{
    public string FirstName;
    public string LastName;

    public People(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
}

sealed class Student : People
{
    public int StudentId;

    public Student(string firstName, string lastName, int studendId)
        : base(firstName, lastName) //Calling the base class constructor
    {
        StudentId = studendId;
    }
}

答案 1 :(得分:1)

这太简单了,我认为

class People
{
    public String first_name;
    public String last_name;

    public People(string fname, string lname)
    {
        this.first_name = fname;
        this.last_name = lname;
    }
}

class Student : People
{
    public int studentID;
    public Student(string fname, string lname, int studid): base(fname,lname)
    {
        this.studentID = studid;
    }
}