亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

關于打印log的一些疑問

代碼:

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;

? ? ? ? }

? ? }

}

https://img1.sycdn.imooc.com//5ba304200001960807850310.jpg

我把2放在1前面debug就崩潰了,但是按照現在的1,2順序debug就能打印出log,請問下各位大神這是怎么回事呢?新手求解

正在回答

1 回答

2放在1前面獲取不到Price

0 回復 有任何疑惑可以回復我~
#1

星辰的淚 提問者

請問下為什么會獲取不到Price呢?
2018-10-18 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
C#面向對象編程
  • 參與學習       69207    人
  • 解答問題       160    個

本系列教程主要是,帶你學習C#面向對象編程的編程思想、編程技巧

進入課程

關于打印log的一些疑問

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號