c#.net中类的覆写(override)
public class mybase
...{
public virtual string meth1()
...{
return "mybase-meth1";
}
public virtual string meth2()
...{
return "mybase-meth2";
}
public virtual string meth3()
...{
return "mybase-meth3";
}
}
class myderived : mybase
...{
// overrides the virtual method meth1 using the override keyword:
public override string meth1()
...{
return "myderived-meth1";
}
// explicitly hide the virtual method meth2 using the new
// keyword:
public new string meth2()
...{
return "myderived-meth2";
}
// because no keyword is specified in the following declaration
// a warning will be issued to alert the programmer that
// the method hides the inherited member mybase.meth3():
public string meth3()
...{
return "myderived-meth3";
}
public static void main()
...{
myderived md = new myderived();
mybase mb = (mybase) md;
system.console.writeline(mb.meth1());
system.console.writeline(mb.meth2());
system.console.writeline(mb.meth3());
}
}
新闻热点
疑难解答
图片精选