委派即可以调用静态类方法,也可以调用对象方法。如下面的类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);

}

}

转载于:https://www.cnblogs.com/djcsch2001/archive/2011/05/07/2039977.html

使用委派调用对象的方法相关推荐

  1. java 调用对象的方法_JAVA调用对象方法的执行过程

    JAVA调用对象方法的执行过程: ①.编译器查看对象的声明类型和方法名.假设调用x.f(parameter),  且隐式参数x声明为C类型的对象,有可能在C对象中存在多个参数类型和参数个数不同的f的方 ...

  2. python创建对象后调用对象的方法,报错TypeError: getName() takes 0 positional arguments but 1 was given

    源码 ## TODO: Create multiple cars and visualize them height = 4 width = 6 world = np.zeros((height, w ...

  3. python 修改模板对象的属性_django小技巧之html模板中调用对象属性或对象的方法...

    环境:依赖最初test2数据库 python3版本 多python版本环境 进入,python3虚拟环境,新建项目test4: ]# cd py3/django-test1/ ]# django-ad ...

  4. Python基础教程:对象的方法

    1.对象的方法 其实可以看成是对象所拥有的函数. 调用对象的方法,和调用函数差不多,只要在前面加上 所属对象 和 一个点. 方法就可以看成是对象所拥有的函数.方法是属于这个对象的函数. # var是一 ...

  5. java 反射无参方法_java 反射 调用无参数方法?

    通过Java反射调用无参数方法,这是个测试用的例子,通过反射调用对象的方法,代码如下: import java.lang.reflect.Method; import java.lang.reflec ...

  6. PHP设计模式:工厂(静态方法里调用newobj的方法)单例(三私一公newself)注册树(使用静态变量数组存取对象)适配器(主体建立接口适配器丰富方法)

    工厂:使用工厂类的静态方法新建需要的对象 然后在外面直接调用静态方法新建对象 <?php//要创建对象实例的类 class MyObject{} //工厂类 class MyFactory{pu ...

  7. 【Groovy】闭包 Closure ( 闭包中调用 Groovy 脚本中的方法 | owner 与 delegate 区别 | 闭包中调用对象中的方法 )

    文章目录 一.闭包中调用 Groovy 脚本中的方法 二.owner 与 delegate 区别 三.闭包中调用 Groovy 对象中的方法 一.闭包中调用 Groovy 脚本中的方法 在 Groov ...

  8. 【Android NDK 开发】JNI 方法解析 ( C/C++ 调用 Java 方法 | 函数签名 | 调用对象方法 | 调用静态方法 )

    文章目录 I . 调用 Java 方法流程 II . 获取 jclass 对象 ( GetObjectClass ) III . 获取 jclass 对象 ( FindClass ) IV . JNI ...

  9. 明显调用的表达式前的括号必须具有指针函数类型_基于指针对象的方法

    " 本文来源于<The Go Programming Language>" 6.2. 基于指针对象的方法 当调用一个函数时,会对其每一个参数值进行拷贝,如果一个函数需要 ...

最新文章

  1. 深度学习--TensorFlow (1)单层感知器1 -- 实现单数据训练
  2. 第三天:创建型模式--建造者模式
  3. 【原】ASP.Net WebForm的发布(图解)
  4. A quick introduction to Google test
  5. pytorch 之 imagefloder的用法
  6. HTML页面打印分页标签样式
  7. 晨哥真有料丨对她越好,越难脱单!
  8. 【正交幅度调制 QAM】
  9. Flutter 移动端屏幕适配方案和制作
  10. 关于在IE浏览器下按钮或者链接点击时出现outline的解决方法
  11. 【车辆识别】基于matlab GUI小波和盒维数车型识别【含Matlab源码 727期】
  12. i.MX6ULL终结者RS232驱动测试及RS485测试
  13. Python爬取妹子图
  14. 云课堂智慧职教网页版登录入口_云课堂智慧职教网页版登录入口
  15. 开源XML数据集编辑器
  16. FastDB使用记录
  17. mysql 1236错误_MySQL 1236错误解决方法_MySQL
  18. file_put_contents() 利用技巧
  19. c++除法保留小数_小学数学整数和小数的应用题解答方法公式汇总,新学期必备...
  20. 微信公众号图灵机器人开发php,使用图灵api创建微信聊天机器人

热门文章

  1. python【力扣LeetCode算法题库】15- 三数之和
  2. python【蓝桥杯vip练习题库】ADV-304矩阵转置
  3. python【力扣LeetCode算法题库】219 -存在重复元素 II
  4. python【Matlibplot绘图库】-绘制三维图像
  5. 贪心入门——出租车费
  6. android读取raw文件示例
  7. toad dba suite for oracle 12.1,Toad for Oracle 12.1下载地址
  8. 企业网络推广专员浅析如何通过企业网络推广的方式提升网站权重?
  9. 企业网络推广——企业网络有推广专员如何做好基本的网站优化布局
  10. 网站在改版时如何降低对排名的影响呢?