系统内置委托
Action()、Func()都不能接受参数,所以很少用,一般都是和泛型一起使用。如下
系统内置泛型委托
Func<参数1,返回值类型> 委托名 //Func
Action<参数1> 委托名 //无返回值,可以接受多个参数
注意!!千万不要将委托的参数搞混!!
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProductFactoryDemo
{
class Program
{
static void Main(string[] args)
{
WrapFactory wrapFactory = new WrapFactory();
FoodFactory foodFactory = new FoodFactory();
Logeer logeer = new Logeer();
// 注意!!!! 【委托实例】的参数列表中,参数一定是方法!方法!方法!
Func<string, Product> fun1 = new Func<string, Product>(foodFactory.makePizza); //有返回值
Action<Product> log = new Action<Product>(logeer.log); //无返回值
Box box_pizza = wrapFactory.ProductBox(fun1,log); //方法1:自己显示创建委托对象,然后将委托对象作为参数
//Box box_pizza = wrapFactory.ProductBox(foodFactory.makePizza,logeer.log); //方法2:直接将方法作为参数,系统隐式创建委托对象
Box box_coke = wrapFactory.ProductBox(foodFactory.makeCoke,logeer.log); //把不同的方法封装在一个委托类型的对象中
{
Console.WriteLine(box_coke.Product.Name);
Console.WriteLine(box_pizza.Product.Name);
}
}
}
class Logeer{
public void log(Product product)
{
Console.WriteLine($"{DateTime.UtcNow}生产了《{product.Brand}》的{product.Name},价格是{product.Price}");
}
}
class Product
{
public string Name { get; set; } //公有属性
public double Price { get; set; }
public string Brand { get; set; }
}
class Box
{
public Product Product { get; set; } //箱子里包装的产品
}
//包装类
class WrapFactory
{
//模板方法,将委托作为参数,提升代码复用性
//用来将打包好的盒子返回
public Box ProductBox(Func<string,Product> getProductDel,Action<Product> LogCallBack)
//Func<参数1,返回值类型> //Func<T>参数列表的最后一位,一定是返回值类型
//Action<参数1>
{
Box box = new Box();
Product product = getProductDel("kfc");
if (product.Price > 20)
{
LogCallBack(product);
}
box.Product = product;
return box;
}
}
class FoodFactory
{
public Product makePizza(string brand) //把不同的方法封装在一个委托类型的对象中
{
Product pizza = new Product();
pizza.Brand = brand;
pizza.Name = "Pizza!";
pizza.Price = 50;
return pizza;
}
public Product makeCoke(string company)
{
Console.WriteLine("@@@ {0}",company);
Product product = new Product();
product.Name = "Coke";
product.Price = 3;
return product;
}
}
}
``
c#中的有些委托可以用接口方法去替换
而java中完全去掉了委托,改用接口来替代委托
``
namespace ProductFactoryDemo
{
class Program
{
static void Main(string[] args)
{
Imake makePizza = new MakePizza();
Imake makeCoke= new MakeCoke();
WrapFactory wrapFactory = new WrapFactory();
Box box1=wrapFactory.ProductBox(makePizza);
Box box2 = wrapFactory.ProductBox(makeCoke);
Console.WriteLine(box1.Product.Name);
Console.WriteLine(box2.Product.Name);
}
}
class Logeer{
public void log(Product product)
{
Console.WriteLine($"{DateTime.UtcNow}生产了《{product.Brand}》的{product.Name},价格是{product.Price}");
}
}
class Product
{
public string Name { get; set; } //公有属性
public double Price { get; set; }
public string Brand { get; set; }
}
class Box
{
public Product Product { get; set; } //箱子里包装的产品
}
class WrapFactory
{
public Box ProductBox(Imake imake)
{
Product product = imake.makeFood();
Box box = new Box();
box.Product = product;
return box;
}
}
interface Imake
{
Product makeFood();
}
class MakePizza : Imake
{
public Product makeFood()
{
Product pizza = new Product();
pizza.Name = "Pizza!";
pizza.Price = 50;
return pizza;
}
}
class MakeCoke:Imake {
public Product makeFood()
{
Product product = new Product();
product.Name = "Coke";
product.Price = 3;
return product;
}
}
}
`