1、c#中的访问修饰符
public :公开的公共的
private:私有的,只能在当前类的内部访问
protected:受保护的,只能在当前类的内部以及该类的子类中访问。
internal:只能在当前项目中访问。在同一个项目中,internal和public的权限是一样。
protected internal:protected+internal
1)、能够修饰类的访问修饰符只有两个:public、internal。
2)、可访问性不一致。 子类的访问权限不能高于父类的访问权限,会暴漏父类的成员。
2、设计模式 设计这个项目的一种方式。
3、简单工厂设计模式
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 Program10 {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 ///22 /// 简单工厂的核心 根据用户的输入创建对象赋值给父类23 /// 24 /// 25 ///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 NoteBook45 {46 public abstract void SayHello();47 }48 49 public class Lenovo : NoteBook50 {51 public override void SayHello()52 {53 Console.WriteLine("我是联想笔记本,你联想也别想");54 }55 }56 57 58 public class Acer : NoteBook59 {60 public override void SayHello()61 {62 Console.WriteLine("我是鸿基笔记本");63 }64 }65 66 public class Dell : NoteBook67 {68 public override void SayHello()69 {70 Console.WriteLine("我是戴尔笔记本");71 }72 }73 74 public class IBM : NoteBook75 {76 public override void SayHello()77 {78 Console.WriteLine("我是IBM笔记本");79 }80 }81 }
4、值类型在复制的时候,传递的是这个值得本身。 引用类型在复制的时候,传递的是对这个对象的引用。
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 Program10 {11 static void Main(string[] args)12 {13 //值类型:int double char decimal bool enum struct 14 //引用类型:string 数组 自定义类 集合 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 Person64 {65 private string _name;66 public string Name67 {68 get { return _name; }69 set { _name = value; }70 }71 }72 }
5、序列化:就是将对象转换为二进制
反序列化:就是将二进制转换为对象
作用:传输数据。
序列化: 1)、将这个类标记为可以被序列化的。
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 Program11 {12 static void Main(string[] args)13 {14 //要将p这个对象 传输给对方电脑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 //接收对方发送过来的二进制 反序列化成对象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 Person45 {46 private string _name;47 48 public string Name49 {50 get { return _name; }51 set { _name = value; }52 }53 54 55 private char _gender;56 57 public char Gender58 {59 get { return _gender; }60 set { _gender = value; }61 }62 63 private int _age;64 65 public int Age66 {67 get { return _age; }68 set { _age = value; }69 }70 }71 }
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 Program10 {11 static void Main(string[] args)12 {13 14 }15 }16 17 public interface IFlyable18 {19 //接口中的成员不允许添加访问修饰符 ,默认就是public20 void Fly();21 string Test();22 //不允许写具有方法体的函数23 24 // string _name;25 string Name26 {27 get;28 set;29 }30 31 }32 }
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 _11_自动属性和普通属性 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 }14 }15 public class Person16 {17 private string _name;18 19 public string Name20 {21 get { return _name; }22 set { _name = value; }23 }24 25 ///26 /// 自动属性27 /// 28 public int Age29 {30 get;31 set;32 }33 }34 }
3)接口特点
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接口特点 8 { 9 class Program10 {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:IFlyable19 {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 : IFlyable36 {37 public void Fly()38 {39 Console.WriteLine("鸟在飞");40 }41 }42 43 public interface IFlyable44 {45 //不允许有访问修饰符 默认为public46 //方法、自动属性47 void Fly();48 }49 50 51 52 public interface M153 {54 void Test1();55 }56 57 public interface M258 {59 void Test2();60 }61 62 public interface M363 {64 void Test3();65 }66 67 68 public interface SupperInterface : M1, M2, M369 { 70 71 }72 73 public class Car : SupperInterface74 {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 }
4)接口练习
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接口练习 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 //麻雀会飞 鹦鹉会飞 鸵鸟不会飞 企鹅不会飞 直升飞机会飞14 //用多态来实现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 Bird27 {28 public double Wings29 {30 get;31 set;32 }33 public void EatAndDrink()34 {35 Console.WriteLine("我会吃喝");36 }37 }38 39 public class MaQue : Bird,IFlyable40 {41 42 public void Fly()43 {44 Console.WriteLine("麻雀会飞");45 }46 }47 48 public class YingWu : Bird, IFlyable,ISpeak49 {50 51 52 public void Fly()53 {54 Console.WriteLine("鹦鹉会飞");55 }56 57 public void Speak()58 {59 Console.WriteLine("鹦鹉可以学习人类说话");60 }61 }62 63 64 public class TuoBird : Bird65 { 66 67 }68 69 public class QQ : Bird70 { 71 72 }73 74 75 public class Plane : IFlyable76 {77 public void Fly()78 {79 Console.WriteLine("直升飞机转动螺旋桨飞行");80 }81 }82 83 84 85 public interface IFlyable86 {87 void Fly();88 89 }90 91 public interface ISpeak92 {93 void Speak();94 }95 96 97 }
5)显示实现接口
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_显示实现接口 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 //显示实现接口就是为了解决方法的重名问题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 : IFlyable25 {26 public void Fly()27 {28 Console.WriteLine("鸟飞会");29 }30 ///31 /// 显示实现接口32 /// 33 void IFlyable.Fly()34 {35 Console.WriteLine("我是接口的飞");36 }37 38 }39 40 public interface IFlyable41 {42 void Fly();43 }44 }
6)总结
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_总结 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 //什么时候用虚方法来实现多态?14 //什么使用用抽象类来实现多态?15 //什么时候用接口来实现多态?16 17 //真的鸭子会游泳 木头鸭子不会游泳 橡皮鸭子会游泳18 19 ISwimming swim = new XPDuck();//new RealDuck();20 swim.Swim();21 Console.ReadKey();22 }23 }24 25 public class RealDuck:ISwimming26 {27 28 public void Swim()29 {30 Console.WriteLine("真的鸭子靠翅膀游泳");31 }32 }33 34 public class MuDuck 35 { 36 37 }38 39 public class XPDuck : ISwimming40 {41 42 public void Swim()43 {44 Console.WriteLine("橡皮鸭子飘着游泳");45 }46 }47 48 public interface ISwimming49 {50 void Swim();51 }52 }