• 简单工厂模式小例子 写的测试过了


     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 笔记本品牌简单工厂模式
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Console.WriteLine("请输入电脑的品牌:");
    14             string brand = Console.ReadLine();
    15             NoteBook note = Factory.SelectNoteBook(brand);
    16             if (note != null)
    17             {
    18                 note.GetBrand();
    19             }
    20             else
    21             {
    22                 Console.WriteLine("没有该品牌...");
    23             }
    24             Console.ReadKey();
    25 
    26         }
    27     }
    28 
    29     abstract class NoteBook
    30     {
    31         public abstract void GetBrand();
    32     }
    33 
    34     class Lenevo:NoteBook
    35     {
    36         public override void GetBrand()
    37         {
    38             Console.WriteLine("我是联想电脑");
    39         }
    40     }
    41     class Acer : NoteBook
    42     {
    43         public override void GetBrand()
    44         {
    45             Console.WriteLine("我是宏碁电脑");
    46         }
    47     }
    48 
    49     class Factory
    50     {
    51         //在这里实现了简单工厂模式  需要一个父类对象,实例化子类对象,返回父类对象
    52         public static NoteBook SelectNoteBook(string brand)
    53         { 
    54             NoteBook note = null;
    55             switch (brand)
    56             {
    57                 case "联想":
    58                     note = new Lenevo();
    59                     break;
    60                 case "宏碁":
    61                     note = new Acer();
    62                     break;
    63                 default:
    64                     break;
    65             }
    66             return note;
    67         }
    68     }
    69
    70 }


    这是我刚学习的,本文如果有什么错误欢迎提出来。谢谢大家的赐教啦...
  • 相关阅读:
    MVC总结
    HTML+CSS总结
    常用正则表达式
    从你的全世界切过(胡说八道支持向量机SVM小故事)
    Beta分布
    贝叶斯决策理论
    Linux(Ubuntu)下载安装破解Matlab2016
    贝叶斯规则
    多元分布
    条件分布
  • 原文地址:https://www.cnblogs.com/zhuanglijie/p/4209494.html
Copyright © 2020-2023  润新知