学习语法时书上说:
构造函数执行顺序是:system.object->基类匹配构造函数->派生类构造器...我写了如下代码,为何无法执行基类构造函数基类:
public class LoginCheck
{
    protected string username, userpassword;
    protected string tablename;
    protected string test;
    public LoginCheck(string userId, string userPwd,string userinfotable)
    {
        username = userId; //设置断点
        userpassword = userPwd;
        tablename = userinfotable;
        test = "gogo";
    }//其他代码
}派生类:
public class UserLoginCheck : LoginCheck
{
    public UserLoginCheck(string userId, string userPwd, string userinfotable)
    {
        username = userId;
        userpassword = userPwd;
        tablename = userinfotable;
    }
}
执行构造函数代码:
UserLoginCheck ulc = new UserLoginCheck("a", "b", "c");
为什么没有先执行基类的匹配签名的构造函数LoginCheck()?导致我无法在派生类中取到变量test的值

解决方案 »

  1.   

    system.object->基类匹配构造函数->派生类构造器...基类匹配构造函数为默认无参构造函数LoginCheck()
      

  2.   

    public UserLoginCheck(string userId, string userPwd, string userinfotable) : base(userId,userPwd,userinfotable)
    {
      //....}
      

  3.   

    构造函数的执行顺序是:
    父类->子类 继承了多少层都是这样
    析构函数正好相反
    子类->父类 构造就是生产一个新的东西,析构就像清理一个东西.这就像有了父亲才有你一样.
      

  4.   

    就像思归老大讲的,要加上: base
      

  5.   

    为何要加base,语法书上说,只要有匹配签名的构造器,就应该先执行基类的构造器,也就是说要执行顺序是:
    System.object.object();
    base: LoginCheck.LoginCheck(string userId, string userPwd,string userinfotable);
    derived: UserLoginCheck.UserLoginCheck(string userId, string userPwd, string userinfotable)加base只是要自定义执行顺序的时候才用到啊
    搞不懂了
      

  6.   

    默认的构造函数是无参的
    你自己写的构造函数是默认构造函数的重载函数
    所以必须要用base: 传值
      

  7.   

    加base和执行顺序没有任何关系,执行顺序永远是楼主第一个帖子中说的顺序,给子类的构造器指定所继承的父类的构造器,执行该子类的构造器才会执行指定的你类的构造器,否则执行的是父类的默认构造器,因为构造器也可以重载,一个类可以有很多个的构造器,如果你不指定子类的某个构造器继承自哪个父类的构造器,那它只能执行默认的构造器,总不能把父类的所有构造器全执行吧
      

  8.   

    你要传参数给基类,估计要用
    public UserLoginCheck(string userId, string userPwd, string userinfotable)
        {
           base(userId,userPwd,userinfotable);
        }
      

  9.   

    public class mybaseclass
    {
    public mybaseclass()
    {}

    public mybaseclasee(int i)
    {}
    }
    public class myderivedclass : mybaseclass
    {
    public myderivedclass()
    {}

    public myderivedclass(int i)
    {}

    public myderivedclass(int i,int j)
    {}
    }
    情况一:
    myderivedclass myobj = new myderivedclass();
    1、system.object.object()
    2、mybaseclass.mybaseclass()
    3、myderivedclass.myderivedclass()
    情况二:
    myderivedclass myobj = new myderivedclass(4);
    1、system.object.object()
    2、mybaseclass.mybaseclass(int i)
    3、myderivedclass.myderivedclass(int i)情况三
    myderivedclass myobj = new myderivedclass(4,8);
    1、system.object.object()
    2、mybaseclass.mybaseclass()
    3、myderivedclass.myderivedclass(int i,int j)
    这时C#经典入门page176上的示列,大家看看正确与否
      

  10.   

    加base只是要自定义执行顺序的时候才用到〈-------
      

  11.   

    UP
    base()是在你没有给类定义构造函数,编译系统会自动生成一个默认的构造函数,一般是
    public LoginCheck():base(){}
    但在你public LoginCheck(string userId, string userPwd,string userinfotable){}
    这样来定义~~那就必需要用base来传值进去`~
    要不就不能取到值.