第一天

  大概看了单例模式,大概理解就是系统中有且只能有一个实例,这就叫单例模式,比如windows的资源管理器等等,看了网上别人写的代码,发现最基础的都不知道了,就开始看C#高级语法,看了字段,属性,方法,其中封装字段,有的字段不能让别人随便赋值,必须在一个范围内,静态方法和实例方法的调用,写的代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 属性
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             People cqx=new People();
13             Console.WriteLine(cqx.Name);
14             //cqx.Age = 5;
15             Console.WriteLine(cqx.Age);
16 
17             Add S = new Add();
18             int DDD = S.Sum(52,12);
19             Console.WriteLine(DDD);
20 
21             string s =MySwap.Swap(3, 5);
22             Console.WriteLine(s);
23             Console.ReadKey();
24         }
25 
26         class MySwap
27         {
28             public static string Swap(int a,int b)
29             {
30                 int temp;
31                 temp = a;
32                 a = b;
33                 b = temp;
34                 return "a="+a+",b="+b;
35             }
36         }
37 
38         class Add
39         {
40             public int Sum(int a,int b)
41             {
42                 return a + b;
43             }
44         }
45 
46         class People
47         { 
48             private string name="陈擎霄";
49             public string Name
50             {
51                 get { return name; }
52                 set { name = value; }
53             }
54             private int age=22;
55 
56             public int Age
57             {
58                 get { return age; }
59                 set {
60                     if (value != 22)
61                     {
62                         age = 22;
63                     }
64                     else
65                     {
66                         age = value;
67                     }
68                 }
69             }
70 
71         }
72     }
73 }
View Code

方法的重载:同一个类中添加几个名字相同,参数与返回值不同的方法

代码如下:

 1        class Add
 2         {
 3             public int Sum(int a,int b)
 4             {
 5                 return a + b;
 6             }
 7 
 8             public string Sum(string a, string b)//基于不同类型参数的重载 
 9             {
10                 return a + b;
11             }
12 
13             public int Sum(int a, int b, int c)//相同类型不同参数个数的重载 
14             {
15                 return a + b + c;
16             }
17         }
View Code

刚才写的单例模式代码如下,不包含多线程,其实我也不懂:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Singleton
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Earth.GetInstance();
13             Console.ReadKey();
14         }
15     }
16 
17     class Earth
18     {
19         private static Earth instance;
20         private Earth()
21         {
22             Console.WriteLine("我是地球!");
23         }
24 
25         public static Earth GetInstance()
26         {
27             if (instance == null)
28             {
29                 instance = new Earth();
30             }
31             return instance;
32         }
33     }
34 }
View Code
原文地址:https://www.cnblogs.com/chenqingxiao/p/3462109.html