“的真正目的是什么?”;“基础”;c#中的关键字?
因此,对于我的应用程序的每个页面中一些常用的可重用方法所使用的基类“的真正目的是什么?”;“基础”;c#中的关键字?,c#,keyword,base,C#,Keyword,Base,因此,对于我的应用程序的每个页面中一些常用的可重用方法所使用的基类 public class BaseClass:System.Web.UI.Page { public string GetRandomPasswordUsingGUID(int length) { string guidResult = System.Guid.NewGuid().ToString(); guidResult = guidResult.Replace("-", string.E
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);
}
}
所以如果我想用这个方法,我会
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);
}
}
它符合我的要求,但有一个“Base”关键字处理c#中的基类。。。
我真的想知道什么时候应该在派生类中使用base关键字
任何一个好的例子…当链接构造函数时,或者当您想要访问基类中已在当前类中重写或隐藏的成员(方法、属性、任何内容)时,
base
关键字用于引用基类。比如说,
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();
将输出
I'm B
I'm A
当您重写派生类中的方法,但只想在原始功能的基础上添加其他功能时,将使用Base 例如:
// Calling the Area base method:
public override void Foo()
{
base.Foo(); //Executes the code in the base class
RunAdditionalProcess(); //Executes additional code
}
public class Foo
{
public virtual void Baz()
{
Console.WriteLine("Foo.Baz");
}
}
public class Bar : Foo
{
public override void Baz()
{
Console.WriteLine("Bar.Baz");
}
public override void Test()
{
base.Baz();
Baz();
}
}
如果类及其基类中有相同的成员,则调用基类成员的唯一方法是使用
base
关键字:
protected override void OnRender(EventArgs e)
{
// do something
base.OnRender(e);
// just OnRender(e); will cause StakOverFlowException
// because it's equal to this.OnRender(e);
}
base
关键字用于访问基类中已被子类中的成员覆盖(或隐藏)的成员
例如:
// Calling the Area base method:
public override void Foo()
{
base.Foo(); //Executes the code in the base class
RunAdditionalProcess(); //Executes additional code
}
public class Foo
{
public virtual void Baz()
{
Console.WriteLine("Foo.Baz");
}
}
public class Bar : Foo
{
public override void Baz()
{
Console.WriteLine("Bar.Baz");
}
public override void Test()
{
base.Baz();
Baz();
}
}
调用Bar.Test
将输出:
Foo.Baz;
Bar.Baz;
当您
覆盖
一项功能,但仍希望覆盖的功能同时出现时,您将使用base
关键字
例如:
public class Car
{
public virtual bool DetectHit()
{
detect if car bumped
if bumped then activate airbag
}
}
public class SmartCar : Car
{
public override bool DetectHit()
{
bool isHit = base.DetectHit();
if (isHit) { send sms and gps location to family and rescuer }
// so the deriver of this smart car
// can still get the hit detection information
return isHit;
}
}
public sealed class SafeCar : SmartCar
{
public override bool DetectHit()
{
bool isHit = base.DetectHit();
if (isHit) { stop the engine }
return isHit;
}
}
可以使用base来填充对象基类的构造函数中的值 例如:
public class Class1
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime Birthday { get; set; }
public Class1(int id, string name, DateTime birthday)
{
ID = id;
Name = name;
Birthday = birthday;
}
}
public class Class2 : Class1
{
public string Building { get; set; }
public int SpotNumber { get; set; }
public Class2(string building, int spotNumber, int id,
string name, DateTime birthday) : base(id, name, birthday)
{
Building = building;
SpotNumber = spotNumber;
}
}
public class Class3
{
public Class3()
{
Class2 c = new Class2("Main", 2, 1090, "Mike Jones", DateTime.Today);
}
}
c#中“base”关键字的真正用途如下:假设您只想调用父类的参数化构造函数,那么您可以使用base并传递参数,请参见下面的示例 范例-
class Clsparent
{
public Clsparent()
{
Console.WriteLine("This is Clsparent class constructor");
}
public Clsparent(int a, int b)
{
Console.WriteLine("a value is=" + a + " , b value is=" + b);
}
}
class Clschild : Clsparent
{
public Clschild() : base(3, 4)
{
Console.WriteLine("This is Clschild class constructor");
}
}
class Program
{
static void Main(string[] args)
{
Clschild objclschild = new Clschild();
Console.Read();
}
}
通常,我们使用基类重用基类的子类中的属性或方法,因此不需要在子类中重复相同的属性和方法 现在,我们使用base关键字直接从基类调用构造函数或方法 范例
public override void ParentMethod()
{
base.ParentMethod(); //call the parent method
//Next code.
}
2) 范例
Base有两种用法
带变量的基。
using System;
class Parent
{
public string value = "Parent";
}
class Child : Parent
{
public string value = "Child";
public Child() {
Console.WriteLine(base.value);
Console.WriteLine(this.value);
}
}
class Program
{
static void Main(string[] args)
{
Child childObject = new Child();
}
}
演示
它不仅限于功能。当类(父类和子类)中存在歧义时,base关键字会澄清用户所谈论的方法/数据成员。
A.Foo()
是否会与base.Foo()
做相同的事情?@KyleDelaney No,A.Foo()是一个静态方法调用。由于A.Foo()不是静态的,如果这样做,您将得到一个错误代码>在重写的方法中调用它。+1表示if(isHit){删除i,它具有完全不同的含义};)@andrewWinn:这可能是C语言设计者无法将VB.NET的IsNot关键字引入C语言的主要原因。它将有一个booger的外观:-D嘿嘿