抽象工厂模式

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  Comp
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
//定义泛型 存放Computer类型的数据
            List<Computer> computer = new List<Computer>();
            NormalComputer c1 
= new NormalComputer("aaa""Intel""立式");
            NormalComputer c2 
= new NormalComputer("bbb""AMD""卧式");
            NotebookComputer c3 
= new NotebookComputer("ccc""Intel2G"2000);
            NotebookComputer c4 
= new NotebookComputer("ddd""AMD3000+"5000);

            computer.Add(c1);
            computer.Add(c2);
            computer.Add(c3);
            computer.Add(c4);

            
foreach (Computer c in computer)
            
{
                c.show();
                Console.WriteLine();
            }

        }

    }

    
/// <summary>
    
/// 定义抽象类Computer
    
/// </summary>

    abstract class Computer
    
{
        
string type;
        
string CPU;

        
protected Computer(string type, string CPU)
        
{
            
this.type = type;
            
this.CPU = CPU;
        }


        
/// <summary>
        
/// 虚方法
        
/// </summary>

        public virtual void show()
        
{
            Console.WriteLine(
"类型:" + type);
            Console.WriteLine(
"CPU:" + CPU);
        }

    }


    
/// <summary>
    
/// 定义NormalComputer继承自Computer
    
/// </summary>

    class NormalComputer : Computer
    
{
        
string t;

        
/// <summary>
        
/// 调用基类的构造方法来初始化子类的参娄CPU
        
/// </summary>
        
/// <param name="type"></param>
        
/// <param name="CPU"></param>
        
/// <param name="t"></param>

        public NormalComputer(string type, string CPU, string t)
            : 
base(type, CPU)
        
{
            
this.t = t;
        }


        
/// <summary>
        
/// 重写基类构造方法
        
/// </summary>

        public override void show()
        
{
            
base.show();
            Console.WriteLine(
"机箱类型:" + t);
        }

    }



    
class NotebookComputer : Computer
    
{
        
int capacity;

        
public NotebookComputer(string type, string CPU, int capacity)
            : 
base(type, CPU)
        
{
            
this.capacity = capacity;
        }


        
public override void show()
        
{
            
base.show();
            Console.WriteLine(
"电池容量:" + capacity);
        }

    }


}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值