学习语法时书上说:
构造函数执行顺序是: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的值
构造函数执行顺序是: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的值
解决方案 »
- asp点击Enter默认搜索按钮
- 邮件群发,有些进垃圾箱了。。。怎么办? 求高手帮助!
- 【求助!!!!!! 如何实现GridViewRow中,实现单击背景色发生改变,同时调用后台带String参数代码】
- b/s模式下,实现公文流转的思路
- 关于提示框的问题?
- <iewc:TreeNode 不是已知元素。原因可能是网站中存在编译错误,或者缺少web.config文件
- 在父页打开一个子叶后,当关闭父页时,如何判断子叶是否存在,如存在就关闭子叶
- 请教一个很郁闷的上传问题!
- Calendar ,改单个日期的颜色
- 请问怎样使DIV可以这样?
- 一个比较简单的问题,不过对于我来说有点难!!大家帮帮忙!
- 求一条SQL语句!急!麻烦各位了!
{
//....}
父类->子类 继承了多少层都是这样
析构函数正好相反
子类->父类 构造就是生产一个新的东西,析构就像清理一个东西.这就像有了父亲才有你一样.
System.object.object();
base: LoginCheck.LoginCheck(string userId, string userPwd,string userinfotable);
derived: UserLoginCheck.UserLoginCheck(string userId, string userPwd, string userinfotable)加base只是要自定义执行顺序的时候才用到啊
搞不懂了
你自己写的构造函数是默认构造函数的重载函数
所以必须要用base: 传值
public UserLoginCheck(string userId, string userPwd, string userinfotable)
{
base(userId,userPwd,userinfotable);
}
{
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上的示列,大家看看正确与否
错
base()是在你没有给类定义构造函数,编译系统会自动生成一个默认的构造函数,一般是
public LoginCheck():base(){}
但在你public LoginCheck(string userId, string userPwd,string userinfotable){}
这样来定义~~那就必需要用base来传值进去`~
要不就不能取到值.