使用委派调用对象的方法

    委派即可以调用静态类方法,也可以调用对象方法。如下面的类Person定义了两个私有域来存储一个人的名字和年龄

 

 
  
1 public class Person
2 {
3
4 // declare two private fields
5   private string name;
6 private int age;
7
8 // define a constructor
9 public Person( string name, int age)
10 {
11 this .name = name;
12 this .age = age;
13 }
14
15 // define a method that returns a string containing
16 // the person's name and age
17 public string NameAndAge()
18 {
19 return (name + " is " + age + " years old " );
20 }
21
22 }

接着在Main方法中创建Person对象

 

 
  
Person myPerson = new Person( " Jason Price " , 32 );

然后创建委托类并调用

 

 
  
DelegateDescription myDelegateDescription =
new DelegateDescription(myPerson.NameAndAge);

// call myPerson.NameAndAge() through myDelegateDescription
string personDescription = myDelegateDescription();
Console.WriteLine(
" personDescription = " + personDescription);

完整代码:

 
  
/*
Example12_3.cs illustrates the use of a delegate
that calls object methods
*/

using System;


// declare the DelegateCalculation delegate class
public delegate string DelegateDescription();


// declare the Person class
public class Person
{

// declare two private fields
private string name;
private int age;

// define a constructor
public Person( string name, int age)
{
this .name = name;
this .age = age;
}

// define a method that returns a string containing
// the person's name and age
public string NameAndAge()
{
return (name + " is " + age + " years old " );
}

}


// declare the Car class
public class Car
{

// declare two private fields
private string model;
private int topSpeed;

// define a constructor
public Car( string model, int topSpeed)
{
this .model = model;
this .topSpeed = topSpeed;
}

// define a method that returns a string containing
// the car's model and top speed
public string MakeAndTopSpeed()
{
return ( " The top speed of the " + model + " is " +
topSpeed
+ " mph " );
}

}


class Example12_3
{

public static void Main()
{

// create a Person object named myPerson
Person myPerson = new Person( " Jason Price " , 32 );

// create a delegate object that calls myPerson.NameAndAge()
DelegateDescription myDelegateDescription =
new DelegateDescription(myPerson.NameAndAge);

// call myPerson.NameAndAge() through myDelegateDescription
string personDescription = myDelegateDescription();
Console.WriteLine(
" personDescription = " + personDescription);

// create a Car object named myCar
Car myCar = new Car( " MR2 " , 140 );

// set myDelegateDescription to call myCar.MakeAndTopSpeed()
myDelegateDescription =
new DelegateDescription(myCar.MakeAndTopSpeed);

// call myCar.MakeAndTopSpeed() through myDelegateDescription
string carDescription = myDelegateDescription();
Console.WriteLine(
" carDescription = " + carDescription);

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值