base关键字的说明(学习使用)
2005-12-26 11:05
316 查看
从本例中大家可以看出继承和重载的使用,各人感觉简明易懂
。在第二个例子中大家可以看出如何指定在创建派生类实例时调用的基类构造函数。
base 关键字用于从派生类中访问基类的成员:
调用基类上已被其他方法重写的方法。
指定创建派生类实例时应调用的基类构造函数。
基类访问只能在构造函数、实例方法或实例属性访问器中进行。
从静态方法中使用 base 关键字是错误的。
示例
在本例中,基类
1
// keywords_base.cs
2
// Accessing base class members
3
using System;
4
public class Person
5
{
6
protected string ssn = "444-55-6666";
7
protected string name = "John L. Malgraine";
8
9
public virtual void GetInfo()
10
{
11
Console.WriteLine("Name: {0}", name);
12
Console.WriteLine("SSN: {0}", ssn);
13
}
14
}
15
class Employee: Person
16
{
17
public string id = "ABC567EFG";
18
19
public override void GetInfo()
20
{
21
// Calling the base class GetInfo method:
22
base.GetInfo();
23
Console.WriteLine("Employee ID: {0}", id);
24
}
25
}
26
27
class TestClass
{
28
public static void Main()
29
{
30
Employee E = new Employee();
31
E.GetInfo();
32
}
33
}
输出
。在第二个例子中大家可以看出如何指定在创建派生类实例时调用的基类构造函数。
base 关键字用于从派生类中访问基类的成员:
调用基类上已被其他方法重写的方法。
指定创建派生类实例时应调用的基类构造函数。
基类访问只能在构造函数、实例方法或实例属性访问器中进行。
从静态方法中使用 base 关键字是错误的。
示例
在本例中,基类
Person和派生类
Employee都有一个名为
Getinfo的方法。通过使用 base 关键字,可以从派生类中调用基类上的
Getinfo方法。
1
// keywords_base.cs
2
// Accessing base class members
3
using System;
4
public class Person
5
{
6
protected string ssn = "444-55-6666";
7
protected string name = "John L. Malgraine";
8
9
public virtual void GetInfo()
10
{
11
Console.WriteLine("Name: {0}", name);
12
Console.WriteLine("SSN: {0}", ssn);
13
}
14
}
15
class Employee: Person
16
{
17
public string id = "ABC567EFG";
18
19
public override void GetInfo()
20
{
21
// Calling the base class GetInfo method:
22
base.GetInfo();
23
Console.WriteLine("Employee ID: {0}", id);
24
}
25
}
26
27
class TestClass
{
28
public static void Main()
29
{
30
Employee E = new Employee();
31
E.GetInfo();
32
}
33
}
输出
Name: John L. Malgraine SSN: 444-55-6666 Employee ID: ABC567EFG 示例 本示例显示如何指定在创建派生类实例时调用的基类构造函数。 1 // keywords_base2.cs 2 using System; 3 public class MyBase 4 { 5 int num; 6 7 public MyBase() 8 { 9 Console.WriteLine("in MyBase()"); 10 } 11 12 public MyBase(int i ) 13 { 14 num = i; 15 Console.WriteLine("in MyBase(int i)"); 16 } 17 18 public int GetNum() 19 { 20 return num; 21 } 22 } 23 24 public class MyDerived: MyBase 25 { 26 // This constructor will call MyBase.MyBase() 27 public MyDerived() : base() 28 { 29 } 30 31 // This constructor will call MyBase.MyBase(int i) 32 public MyDerived(int i) : base(i) 33 { 34 } 35 36 public static void Main() 37 { 38 MyDerived md = new MyDerived(); 39 MyDerived md1 = new MyDerived(1); 40 } 41 } 输出 in MyBase() in MyBase(int i)本文出自 “努力学习的小熊” 博客,转载请与作者联系!
相关文章推荐
- base关键字的说明(学习使用)
- jQuery 学习第五课 Ajax 使用说明
- Valgrind使用说明(学习)
- jQuery学习2 选择器的使用说明
- C#继承中的静态构造函数和base关键字使用
- C/C++关键字 typedef 的使用说明
- c# override,new关键字区别与使用(学习笔记)
- 基于一道面试题,牢固掌握base关键字使用技巧
- Selenium2Library类库关键字使用说明
- TestLink学习四:TestLink1.9.13使用说明
- java学习笔记(六)关键字static的使用
- C#中SQL Server数据库连接池使用及连接字符串部分关键字使用说明
- jbpm4.4学习总结(三)常用接口使用说明 推荐
- sklearn学习记录一:官方使用说明
- InterBase/Firebird内置函数使用说明
- 学习NUnit测试3(比较详细的NUnit2.0使用说明[转])
- [javase学习笔记]-7.10 static关键字使用的注意细节
- 设计模式简单学习(1)--深入浅出关键字---base和this
- java中transient关键字使用学习
- libevent学习笔记【使用篇】——2. 获取(创建)event_base