使用委派调用对象的方法

简介: 委派即可以调用静态类方法,也可以调用对象方法。如下面的类Person定义了两个私有域来存储一个人的名字和年龄   1 public class Person 2 { 3 4 // declare two private fields 5   private string...

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

}

}
相关文章
|
2天前
|
存储 Java 编译器
类、对象、方法
摘要: 本文介绍了面向对象编程的概念,以京东购买手机为例,展示了如何通过分类和参数选择商品,强调软件与现实生活的对应关系。柯南三步走揭示了京东如何通过搜索和筛选帮助用户找到所需商品,而这一切背后的编程思想即为面向对象编程。面向对象编程涉及抽象、自定义类型和实例化对象等步骤,其中自定义类型(如Java中的类)用于封装现实生活中的复杂数据。文章还讲解了如何定义类、实例化对象以及访问权限修饰符、构造方法、this关键字、方法的使用,强调了方法参数和返回值在不同数据类型上的处理差异。整个讨论旨在阐明Java中面向对象编程的基本原理和实践应用。
15 5
|
1月前
调用反射类的方法
调用反射类的方法
16 3
|
1月前
调用反射类的指定方法
调用反射类的指定方法
12 3
|
1月前
|
存储 安全 Java
调用链跨线程传递 ThreadLocal 对象对比
说起本地线程专属变量,大家首先会想到的是 JDK 默认提供的 ThreadLocal,用来存储在整个链路中都需要访问的数据,并且是线程安全的。由于在落地全链路压测的过程中,一个基本并核心的功能需求是流量标记需要在整个链路中进行传递,那么线程上下文环境成为解决这个问题最合适的技术。
43 2
调用链跨线程传递 ThreadLocal 对象对比
|
10月前
|
程序员 容器
C++11之委派构造函数
C++11之委派构造函数
51 0
|
11月前
|
Java API Spring
反射:替对象执行方法
反射:替对象执行方法
反射:替对象执行方法
|
存储 缓存 安全
类的加载机制以及类、对象初始化的详细过程
java类的生命周期包括加载、连接(验证、准备、解析)、初始化、使用、卸载五个阶段。初始化的顺序是怎样的呢?
127 0
类的加载机制以及类、对象初始化的详细过程
【错误记录】反射时调用方法及成员报错 ( 执行反射方法 | 设置反射的成员变量 | 设置方法/成员可见性 )
【错误记录】反射时调用方法及成员报错 ( 执行反射方法 | 设置反射的成员变量 | 设置方法/成员可见性 )
157 0
如何通过反射调用对象的方法?
import java.lang.reflect.Method; class MethodInvokeTest { public static void main(String[] args) throws Exception { ...
1088 0