多态、接口
1、c#中的訪問修飾符
public :公開的公共的
private:私有的,只能在當(dāng)前類的內(nèi)部訪問
protected:受保護(hù)的,只能在當(dāng)前類的內(nèi)部以及該類的子類中訪問。
internal:只能在當(dāng)前項(xiàng)目中訪問。在同一個(gè)項(xiàng)目中,internal和public的權(quán)限是一樣。
protected internal:protected+internal
1)、能夠修飾類的訪問修飾符只有兩個(gè):public、internal。
2)、可訪問性不一致。 子類的訪問權(quán)限不能高于父類的訪問權(quán)限,會暴漏父類的成員。
?
2、設(shè)計(jì)模式 設(shè)計(jì)這個(gè)項(xiàng)目的一種方式。
?
3、簡單工廠設(shè)計(jì)模式
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _03簡單工廠設(shè)計(jì)模式 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 Console.WriteLine("請輸入您想要的筆記本品牌"); 14 string brand = Console.ReadLine(); 15 NoteBook nb = GetNoteBook(brand); 16 nb.SayHello(); 17 Console.ReadKey(); 18 } 19 20 21 /// <summary> 22 /// 簡單工廠的核心 根據(jù)用戶的輸入創(chuàng)建對象賦值給父類 23 /// </summary> 24 /// <param name="brand"></param> 25 /// <returns></returns> 26 public static NoteBook GetNoteBook(string brand) 27 { 28 NoteBook nb = null; 29 switch (brand) 30 { 31 case "Lenovo": nb = new Lenovo(); 32 break; 33 case "IBM": nb = new IBM(); 34 break; 35 case "Acer": nb = new Acer(); 36 break; 37 case "Dell": nb = new Dell(); 38 break; 39 } 40 return nb; 41 } 42 } 43 44 public abstract class NoteBook 45 { 46 public abstract void SayHello(); 47 } 48 49 public class Lenovo : NoteBook 50 { 51 public override void SayHello() 52 { 53 Console.WriteLine("我是聯(lián)想筆記本,你聯(lián)想也別想"); 54 } 55 } 56 57 58 public class Acer : NoteBook 59 { 60 public override void SayHello() 61 { 62 Console.WriteLine("我是鴻基筆記本"); 63 } 64 } 65 66 public class Dell : NoteBook 67 { 68 public override void SayHello() 69 { 70 Console.WriteLine("我是戴爾筆記本"); 71 } 72 } 73 74 public class IBM : NoteBook 75 { 76 public override void SayHello() 77 { 78 Console.WriteLine("我是IBM筆記本"); 79 } 80 } 81 } View Code?
4、值類型在復(fù)制的時(shí)候,傳遞的是這個(gè)值得本身。 ?? 引用類型在復(fù)制的時(shí)候,傳遞的是對這個(gè)對象的引用。 ??
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _04值類型和引用類型 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //值類型:int double char decimal bool enum struct 14 //引用類型:string 數(shù)組 自定義類 集合 object 接口 15 16 //值傳遞和引用傳遞 17 //int n1 = 10; 18 //int n2 = n1; 19 //n2 = 20; 20 //Console.WriteLine(n1); 21 //Console.WriteLine(n2); 22 //Console.ReadKey(); 23 24 //Person p1 = new Person(); 25 //p1.Name = "張三"; 26 //Person p2 = p1; 27 //p2.Name = "李四"; 28 //Console.WriteLine(p1.Name); 29 //Console.ReadKey(); 30 31 //Person p = new Person(); 32 //p.Name = "張三"; 33 //Test(p); 34 //Console.WriteLine(p.Name); 35 //Console.ReadKey(); 36 37 //string s1 = "張三"; 38 //string s2 = s1; 39 //s2 = "李四"; 40 //Console.WriteLine(s1); 41 //Console.WriteLine(s2); 42 //Console.ReadKey(); 43 44 int number = 10; 45 TestTwo(ref number); 46 Console.WriteLine(number); 47 Console.ReadKey(); 48 } 49 //int n=number; 50 public static void TestTwo(ref int n) 51 { 52 n += 10; 53 } 54 55 //Person pp=p; 56 public static void Test(Person pp) 57 { 58 Person p = pp; 59 p.Name = "李四"; 60 } 61 } 62 63 public class Person 64 { 65 private string _name; 66 public string Name 67 { 68 get { return _name; } 69 set { _name = value; } 70 } 71 } 72 } View Code?
5、序列化:就是將對象轉(zhuǎn)換為二進(jìn)制 ??
反序列化:就是將二進(jìn)制轉(zhuǎn)換為對象 ??
作用:傳輸數(shù)據(jù)。 ?
序列化: ? 1)、將這個(gè)類標(biāo)記為可以被序列化的。 ? ?
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.IO; 7 using System.Runtime.Serialization.Formatters.Binary; 8 namespace _05序列化和反序列化 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 //要將p這個(gè)對象 傳輸給對方電腦 15 //Person p = new Person(); 16 //p.Name = "張三"; 17 //p.Age = 19; 18 //p.Gender = '男'; 19 //using (FileStream fsWrite = new FileStream(@"C:\Users\SpringRain\Desktop\111.txt", FileMode.OpenOrCreate, FileAccess.Write)) 20 //{ 21 // //開始序列化對象 22 // BinaryFormatter bf = new BinaryFormatter(); 23 // bf.Serialize(fsWrite, p); 24 //} 25 //Console.WriteLine("序列化成功"); 26 //Console.ReadKey(); 27 28 //接收對方發(fā)送過來的二進(jìn)制 反序列化成對象 29 Person p; 30 using (FileStream fsRead = new FileStream(@"C:\Users\SpringRain\Desktop\111.txt", FileMode.OpenOrCreate, FileAccess.Read)) 31 { 32 BinaryFormatter bf = new BinaryFormatter(); 33 p = (Person)bf.Deserialize(fsRead); 34 } 35 Console.WriteLine(p.Name); 36 Console.WriteLine(p.Age); 37 Console.WriteLine(p.Gender); 38 Console.ReadKey(); 39 } 40 } 41 42 43 [Serializable] 44 public class Person 45 { 46 private string _name; 47 48 public string Name 49 { 50 get { return _name; } 51 set { _name = value; } 52 } 53 54 55 private char _gender; 56 57 public char Gender 58 { 59 get { return _gender; } 60 set { _gender = value; } 61 } 62 63 private int _age; 64 65 public int Age 66 { 67 get { return _age; } 68 set { _age = value; } 69 } 70 } 71 } View Code?
6、partial部分類
?
7、sealed密封類 不能夠被其他類繼承,但是可以繼承于其他類。 ?
?
8、接口 [public] interface I..able { ?成員; }
1)語法特征
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _10接口的語法特征 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 14 } 15 } 16 17 public interface IFlyable 18 { 19 //接口中的成員不允許添加訪問修飾符 ,默認(rèn)就是public 20 void Fly(); 21 string Test(); 22 //不允許寫具有方法體的函數(shù) 23 24 // string _name; 25 string Name 26 { 27 get; 28 set; 29 } 30 31 } 32 } View Code2)自動(dòng)屬性和普通屬性
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _11_自動(dòng)屬性和普通屬性 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 } 14 } 15 public class Person 16 { 17 private string _name; 18 19 public string Name 20 { 21 get { return _name; } 22 set { _name = value; } 23 } 24 25 /// <summary> 26 /// 自動(dòng)屬性 27 /// </summary> 28 public int Age 29 { 30 get; 31 set; 32 } 33 } 34 } View Code3)接口特點(diǎn)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _12接口特點(diǎn) 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 IFlyable fly = new Bird();//new Person();// new IFlyable(); 14 fly.Fly(); 15 Console.ReadKey(); 16 } 17 } 18 public class Person:IFlyable 19 { 20 public void Fly() 21 { 22 Console.WriteLine("人類在飛"); 23 } 24 } 25 26 27 public class Student 28 { 29 public void Fly() 30 { 31 Console.WriteLine("人類在飛"); 32 } 33 } 34 35 public class Bird : IFlyable 36 { 37 public void Fly() 38 { 39 Console.WriteLine("鳥在飛"); 40 } 41 } 42 43 public interface IFlyable 44 { 45 //不允許有訪問修飾符 默認(rèn)為public 46 //方法、自動(dòng)屬性 47 void Fly(); 48 } 49 50 51 52 public interface M1 53 { 54 void Test1(); 55 } 56 57 public interface M2 58 { 59 void Test2(); 60 } 61 62 public interface M3 63 { 64 void Test3(); 65 } 66 67 68 public interface SupperInterface : M1, M2, M3 69 { 70 71 } 72 73 public class Car : SupperInterface 74 { 75 76 public void Test1() 77 { 78 throw new NotImplementedException(); 79 } 80 81 public void Test2() 82 { 83 throw new NotImplementedException(); 84 } 85 86 public void Test3() 87 { 88 throw new NotImplementedException(); 89 } 90 } 91 92 } View Code4)接口練習(xí)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _13接口練習(xí) 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //麻雀會飛 鸚鵡會飛 鴕鳥不會飛 企鵝不會飛 直升飛機(jī)會飛 14 //用多態(tài)來實(shí)現(xiàn) 15 //需方法、抽象類、接口 16 17 18 IFlyable fly = new Plane();//new MaQue();//new YingWu(); 19 fly.Fly(); 20 Console.ReadKey(); 21 22 23 } 24 } 25 26 public class Bird 27 { 28 public double Wings 29 { 30 get; 31 set; 32 } 33 public void EatAndDrink() 34 { 35 Console.WriteLine("我會吃喝"); 36 } 37 } 38 39 public class MaQue : Bird,IFlyable 40 { 41 42 public void Fly() 43 { 44 Console.WriteLine("麻雀會飛"); 45 } 46 } 47 48 public class YingWu : Bird, IFlyable,ISpeak 49 { 50 51 52 public void Fly() 53 { 54 Console.WriteLine("鸚鵡會飛"); 55 } 56 57 public void Speak() 58 { 59 Console.WriteLine("鸚鵡可以學(xué)習(xí)人類說話"); 60 } 61 } 62 63 64 public class TuoBird : Bird 65 { 66 67 } 68 69 public class QQ : Bird 70 { 71 72 } 73 74 75 public class Plane : IFlyable 76 { 77 public void Fly() 78 { 79 Console.WriteLine("直升飛機(jī)轉(zhuǎn)動(dòng)螺旋槳飛行"); 80 } 81 } 82 83 84 85 public interface IFlyable 86 { 87 void Fly(); 88 89 } 90 91 public interface ISpeak 92 { 93 void Speak(); 94 } 95 96 97 } View Code5)顯示實(shí)現(xiàn)接口
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _14_顯示實(shí)現(xiàn)接口 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //顯示實(shí)現(xiàn)接口就是為了解決方法的重名問題 14 IFlyable fly = new Bird(); 15 fly.Fly(); 16 Bird bird = new Bird(); 17 bird.Fly(); 18 19 Console.ReadKey(); 20 } 21 } 22 23 24 public class Bird : IFlyable 25 { 26 public void Fly() 27 { 28 Console.WriteLine("鳥飛會"); 29 } 30 /// <summary> 31 /// 顯示實(shí)現(xiàn)接口 32 /// </summary> 33 void IFlyable.Fly() 34 { 35 Console.WriteLine("我是接口的飛"); 36 } 37 38 } 39 40 public interface IFlyable 41 { 42 void Fly(); 43 } 44 } View Code6)總結(jié)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _15_總結(jié) 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //什么時(shí)候用虛方法來實(shí)現(xiàn)多態(tài)? 14 //什么使用用抽象類來實(shí)現(xiàn)多態(tài)? 15 //什么時(shí)候用接口來實(shí)現(xiàn)多態(tài)? 16 17 //真的鴨子會游泳 木頭鴨子不會游泳 橡皮鴨子會游泳 18 19 ISwimming swim = new XPDuck();//new RealDuck(); 20 swim.Swim(); 21 Console.ReadKey(); 22 } 23 } 24 25 public class RealDuck:ISwimming 26 { 27 28 public void Swim() 29 { 30 Console.WriteLine("真的鴨子靠翅膀游泳"); 31 } 32 } 33 34 public class MuDuck 35 { 36 37 } 38 39 public class XPDuck : ISwimming 40 { 41 42 public void Swim() 43 { 44 Console.WriteLine("橡皮鴨子飄著游泳"); 45 } 46 } 47 48 public interface ISwimming 49 { 50 void Swim(); 51 } 52 } View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/liuslayer/p/4478905.html
總結(jié)
- 上一篇: 居家生活|装修避免的坑
- 下一篇: android微信认证失败怎么办,微信登