public:公开的,共有的
private:私有的,只能在当前类的内部使用
Protected:受保护的,只能在当前类的内部以及该类的子类中使用
Internal:只能在当前程序集中访问
Protected internal:
1) 能够修饰类的访问修饰符只有两个:public和internal
2) 可访问性不一致
子类的访问权限不能高于父类,会暴露父类的成员
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _02访问修饰符 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 Person p = new Person(); 14 } 15 } 16 17 public class Person 18 { 19 //protected string _name; 20 //public int _age; 21 //private char _gender; 22 //internal int _chinese; 23 //protected internal int _math; 24 } 25 26 public class Student : Person 27 { 28 29 } 30 31 //public class Student:Person 32 //{ 33 34 //} 35 }
设计模式 设计这个项目的一种方式。
简单工厂设计模式
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简单工厂设计模式 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 /// 简单工厂的核心 根据用户的输入创建对象赋值给父类 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("我是联想笔记本,你联想也别想"); 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 }