预定义类型 int i1=12等(最后转为Int32 i1=new Int32(),i1=12)

基元类型:Int32等

值类型继承于ValueType ---------Object

 

Equals方法

Equals方法是定义在Object类的一个虚方法

在 Object的虚方法定义的规则的时比较对象的地址(如果想自定义,需在类中重写object’类中的虚方法)

在ValueType类中重写了Equals方法,定义的规则就是比较值类型中的所有字段的值

 

Sealed

Sealed修饰的类叫做密封类,密封类不能被继承

String 是密封类

密封类中不能有虚方法,因为密封类不能被继承

 

多态

同一种行为,不同的人具有不同的表演形式,这就是多态

同一种方法 不同的子类具有不同的表现

就是将子类看作一个父类  不管这个子类到底是那个子类,可以确定的是这些子类都有与父类同名的方法

 

如何实现  多态的表现形式

第一种表现形式 将父类类型作为参数

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 多态2
 8 {
 9     class Animal
10     {
11         public virtual void Say()
12         {
13             Console.WriteLine("未知动物在叫~~~~~");
14         }
15 
16     }
17     class Dog : Animal
18     {
19         public override void Say()
20         {
21             Console.WriteLine("狗仔在叫,旺旺~~~~");
22         }
23     }
24     class Cat : Animal
25     {
26         public override void Say()
27         {
28             Console.WriteLine("猫在叫,喵喵~~~~");
29         }
30     }
31 
32     class Program
33     {
34         static void Main(string[] args)
35         {
36             CallAnimal(new Animal());
37             CallAnimal(new Dog());
38             CallAnimal(new Cat());
39             Console.ReadKey();
40         }
41         /// <summary>
42         /// 让动物叫
43         /// </summary>
44         /// <param name="aim">动物</param>
45         static void CallAnimal(Animal aim)
46         {
47             aim.Say();
48         }
49 
50     }
51 }

 

第二种表现形式,将父类类型作为返回值(工厂方法)项目代码在20121213(宠物商店)

 

View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace 打折
  7 {
  8     /// <summary>
  9     /// 打折类
 10     /// </summary>
 11     class Discount:Normal
 12     {
 13         /// <summary>
 14         ///  折扣率
 15         /// </summary>
 16         private float rate;
 17 
 18         public Discount(double yuanJia,float rate)
 19             :base(yuanJia)
 20         {
 21             this.rate = rate;
 22         }
 23 
 24         public override double GetPrice()
 25         {
 26             return base.yuanJia * this.rate;
 27         }
 28     }
 29     class ManJian : Normal
 30     {
 31         private double man;
 32         private double jian;
 33 
 34         public ManJian(double man, double jian, double yuanJia)
 35             : base(yuanJia)
 36         {
 37             this.man = man;
 38             this.jian = jian;
 39         }
 40 
 41         public override double GetPrice()
 42         {
 43             if (yuanJia >= man)
 44             {
 45                 return yuanJia - jian;
 46             }
 47             return yuanJia;
 48         }
 49     }
 50     /// <summary>
 51     ///  打折类的父类
 52     /// </summary>
 53     class Normal
 54     {
 55         //原价
 56         protected double yuanJia;
 57 
 58         public Normal(double yuanJia)
 59         {
 60             this.yuanJia = yuanJia;
 61         }
 62 
 63         public virtual double GetPrice()
 64         {
 65             return this.yuanJia;
 66         }
 67     }
 68     /// <summary>
 69     ///  打折工厂
 70     /// </summary>
 71     class DiscountFactory
 72     {
 73         public Normal GetDis(string type,double yuanJia)
 74         {
 75             //1普通 2七折 3八折 4满300减100 5 满500减200
 76             Normal dis = null;
 77             switch (type)
 78             {
 79                 case "1":
 80                     dis = new Normal(yuanJia);
 81                     break;
 82                 case "2":
 83                     dis = new Discount(yuanJia,0.7f);
 84                     break;
 85                 case "3":
 86                     dis = new Discount(yuanJia, 0.8f);
 87                     break;
 88                 case "4":
 89                     dis = new ManJian(300, 100, yuanJia);
 90                     break;
 91                 case "5":
 92                     dis = new ManJian(500, 200, yuanJia);
 93                     break; 
 94                 default:
 95                     dis = null;
 96                     break; 
 97             }
 98             return dis;
 99 
100         }
101     }
102     class Program
103     {
104         static void Main(string[] args)
105         {
106             //七折 八折  满300减100  满500减200  普通
107             Console.WriteLine("请输入原价:");
108             double yuanJia = double.Parse(Console.ReadLine());
109             Console.WriteLine("请选择打折类型:1普通 2七折 3八折 4满300减100 5 满500减200");
110             string type = Console.ReadLine();
111             Normal dis = new DiscountFactory().GetDis(type,yuanJia);
112             Console.WriteLine(dis.GetPrice());
113             Console.ReadKey(); 
114         }
115     }
116 }

 

 

posted on 2012-12-13 23:24  陈谨  阅读(119)  评论(0编辑  收藏  举报