什么是真正的"目的;基地"关键字在C#中? [英] What really is the purpose of "base" keyword in c#?
问题描述
因此,对于在我的应用程序的每个页面的一些可重复使用的黎民方法中使用的基类...
Thus for used base class for some commom reusable methods in every page of my application...
public class BaseClass:System.Web.UI.Page
{
public string GetRandomPasswordUsingGUID(int length)
{
string guidResult = System.Guid.NewGuid().ToString();
guidResult = guidResult.Replace("-", string.Empty);
return guidResult.Substring(0, length);
}
}
所以,如果我想用这个方法,我只想这样做,
So if i want to use this method i would just do,
public partial class forms_age_group : BaseClass
{
protected void Page_Load(object sender, EventArgs e)
{
//i would just call it like this
string pass = GetRandomPasswordUsingGUID(10);
}
}
据我想要做什么,但有一个基地关键字,在C#...
我真的很想知道我在派生类中应使用base关键字....
It does what i want but there is a "Base" keyword that deals with base class in c# ... I really want to know when should use base keyword in my derived class....
任何与基类交易很好的例子...
Any good example...
推荐答案
的基
关键字用来指基类构造函数链时,或当您要访问已被覆盖或隐藏在当前类的基类成员(方法,属性,任何东西)。例如,
The base
keyword is used to refer to the base class when chaining constructors or when you want to access a member (method, property, anything) in the base class that has been overridden or hidden in the current class. For example,
class A {
protected virtual void Foo() {
Console.WriteLine("I'm A");
}
}
class B : A {
protected override void Foo() {
Console.WriteLine("I'm B");
}
public void Bar() {
Foo();
base.Foo();
}
}
通过这些定义,
new B().Bar();
将输出
would output
I'm B
I'm A
这篇关于什么是真正的"目的;基地"关键字在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!