9. 调用 base 构造函数 public class Student:Person
{
private uint id;
//调用 Person 构造函数
public Student(string name,uint age,uint id):base(name,age)
{
this.id = id;
Console.WriteLine(id);
}
} :base 关键字将调用 Person 类构造函数9
10. 演示public class Person
{
public string _name;
public uint _age;
public Person(string name, uint age)
{
this._name = name;
this._age = age;
Console.WriteLine(_name);
Console.WriteLine(_age);
}
}public class Student:Person
{
private uint _id;
public Student(string name, uint age, uint id):base(name, age)
{
this._id = id;
Console.WriteLine(_id);
}
}还将调用 Base 构造函数static void Main(string[] args)
{
//构造 Student
Student objStudent = new Student("XYZ", 45, 001);
}10
11. 关键字 overrideClass Base
{
// 成员变量
int basevar;
// 成员函数
GetInfo()
{
// 定义
}
…….
…….Class Derived : Base
{
// 成员变量
int derivedvars;
// 成员函数
override GetInfo()
{
// 定义
}
…….
…….基类派生类base 方法的新实现11
34. 委托Multiply(int,int)
{
….
}
Divide(int,int)
{
….
}
在运行时确定调用哪种方法委托和方法必须具有相同的签名---
public delegate Call(int n1, int n2);
---34
35. 定义委托 2-1 [访问修饰符] delegate 返回类型 委托名(); 语法class Delegates
{
public delegate int Call(int n1, int n2);
class Math
{
public int Multiply(int n1, int n2)
{
// 实现
}
public int Divide(int n1, int n2)
{// 实现
}
}将方法与委托关联起来class TestDelegates
{
static void Main()
{ Call objCall;
Math objMath = new Math();
objCall = new Call(objMath.Multiply);
}
}35
36. 定义委托 2-2class Delegates
{
// 委托定义
public delegate int Call(int n1, int n2);
class Math
{
// 乘法方法
public int Multiply(int n1, int n2)
{
return n1*n2;
}
// 除法方法
public int Divide(int n1, int n2)
{ if(n2!=0)
return n1/n2;
}
}static void Main(string[] args)
{
// 委托的对象
Call objCall;
// Math 类的对象
Math objMath = new Math();
// 将方法与委托关联起来
objCall = new Call(objMath.Multiply);
// 将委托实例化
result = objCall(4, 3);
System.Console.WriteLine("结果为 {0}", result);
} 将方法与委托关联起来36