星辰的泪
2018-09-20 10:22
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MorningExercise0920_Interface
{
class Program
{
static void Main(string[] args)
{
WrapFactory wrapProduct = new WrapFactory();
Logger logger = new Logger();
IProductFactory pizze = new PizzeFactory();
IProductFactory ToyCar = new ToyCarFactory();
Action<Product> log = new Action<Product>(logger.Log);
Box box1 = wrapProduct.WrapProduct(pizze, log);
Box box2 = wrapProduct.WrapProduct(ToyCar, log);
Console.WriteLine(box1.Product.Name);
Console.WriteLine(box2.Product.Name);
}
}
interface IProductFactory
{
Product Make();
}
class PizzeFactory : IProductFactory
{
public Product Make()
{
Product product = new Product();
product.Name = "Pizze";
product.Price = 78;
return product;
}
}
class ToyCarFactory : IProductFactory
{
public Product Make()
{
Product product = new Product();
product.Name = "ToyCar";
product.Price = 208;
return product;
}
}
class Logger
{
public void Log(Product product)
{
Console.WriteLine("TimeNow {0}\nProduct {1}\nPrice {2}", DateTime.Now, product.Name, product.Price);
Console.WriteLine("**************************************************************");
}
}
class Product
{
public string Name { get; set; }
public double Price { get; set; }
}
class Box
{
public Product Product { get; set; }
}
class WrapFactory
{
public Box WrapProduct(IProductFactory ipProductor, Action<Product> logCallback)
{
Box box1 = new Box();
box1.Product = ipProductor.Make();
if (box1.Product.Price >= 1)
{
logCallback(box1.Product);
}
return box1;
}
}
}
我把2放在1前面debug就崩溃了,但是按照现在的1,2顺序debug就能打印出log,请问下各位大神这是怎么回事呢?新手求解
2放在1前面获取不到Price
C#面向对象编程
68857 学习 · 153 问题
相似问题