C#中没有找到适合的方法来重写问题

来源:百度知道 编辑:UC知道 时间:2024/06/09 10:35:56
初学者。。代码如下,运行的时候提示没有找到适合的方法来重写,请帮帮忙。。
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=PC-200812221325.\\SQLEXPRESS;AttachDbFilename=D:\\新建文件夹\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True ");
}
protected void btnlogin_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=PC-200812221325.\\SQLEXPRESS;AttachDbFilename=D:\\新建文件夹\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True ");
cn.Open();
SqlCommand cm = new SqlCommand("select * from user1 where username=" + txtusername.Text, cn);
SqlDataReader dr = cm.ExecuteReader();
if (dr.Read())
{
if (dr["userpwd&q

重写是指重写基类的方法,在基类中的方法必须有修饰符virtual,而在子类的方法中必须指明override。
格式:
基类中:
public virtual void myMethod()
{
}
子类中:
public override void myMethod()
{
}
重写以后,用基类对象和子类对象访问myMethod()方法,结果都是访问在子类中重新定义的方法,基类的方法相当于被覆盖掉了。如下例子:
using System;
class a
{
int x=1;
public virtual void PrintFields()
{
Console.WriteLine("x={0}",x);
}
}

class b:a
{
int y=2;
public override void PrintFields()
{
Console.WriteLine("y={0}",y);

}

}

class c
{
public static void Main()
{
b me=new b();
me.PrintFields();
a y=new b();
y.PrintFields();
}
}
以上代码运行结果:
y=2
y=2

如果把上面代码中的override去掉
那么运行的时候是不会有错误,但是会有个警告,因为编译器不知道你是要重写该方法,还是隐藏该方法。如果重写那么就加override,如果是隐藏那么就加new,其实