00解释
1、c#中的访问修饰符
public :公开的公共的
private:私有的,只能在当前类的内部访问
protected:受保护的,只能在当前类的内部以及该类的子类中访问。
internal:只能在当前项目中访问。在同一个项目中,internal和public的权限是一样。
protected internal:protected+internal
1)、能够修饰类的访问修饰符只有两个:public、internal。
2)、可访问性不一致。
子类的访问权限不能高于父类的访问权限,会暴漏父类的成员。
2、设计模式
设计这个项目的一种方式。
3、简单工厂设计模式
4、值类型在复制的时候,传递的是这个值得本身。
引用类型在复制的时候,传递的是对这个对象的引用。
5、序列化:就是将对象转换为二进制
反序列化:就是将二进制转换为对象
作用:传输数据。
序列化:
1)、将这个类标记为可以被序列化的。
6、partial部分类
7、sealed密封类
不能够被其他类继承,但是可以继承于其他类。
8、接口
[public] interface I..able
{
成员;
}
1、复习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace _01复习
{
class Program
{
static void Main(string[] args)
{
/*
泛型集合
* List<T>
* Dictionary<T,T>
* 装箱和拆箱
* 装箱:将值类型转换为引用类型
* 拆箱:将引用类型转换为值类型
* 我们应该尽量避免在代码中发生装箱或者拆箱
* 文件流
* FileStream StreamReader和StreamWriter
* 多态:虚方法、
* //抽象类、接口
* 虚方法:
* 抽象类:
*/
//List<int> list = new List<int>();
//Dictionary<int, string> dic = new Dictionary<int, string>();
//dic.Add(1, "张三");
//dic[2] = "李四";
//foreach (KeyValuePair<int,string> kv in dic)
//{
// Console.WriteLine("{0}-----{1}",kv.Key,kv.Value);
//}
//Console.ReadKey();
//File FileStream StreamReader StreamWriter
//using (FileStream fsRead = new FileStream("0505远程版机器码.txt", FileMode.OpenOrCreate, FileAccess.Read))
//{
// byte[] buffer = new byte[1024 * 1024 * 5];
// //本次读取实际读取到的字节数
// int r = fsRead.Read(buffer, 0, buffer.Length);
// //将字节数组中的每个元素按照我们指定的编码格式解析成字符串
// string s = Encoding.Default.GetString(buffer, 0, r);
// Console.WriteLine(s);
//}
//Console.ReadKey();
//using (FileStream fsWrite = new FileStream(@"C:\Users\SpringRain\Desktop\new.txt", FileMode.Append, FileAccess.Write))
//{
// string s = "abc";
// byte[] buffer=Encoding.UTF8.GetBytes(s);
// fsWrite.Write(buffer,0,buffer.Length);
//}
//Console.WriteLine("OK");
//Console.ReadKey();
//虚方法和抽象类
//老师可以起立 学生也可以起立 校长也可以起立
//声明父类去指定子类对象
Person p = new HeadMaster();//new Teacher();//new Student();
p.StandUp();
Console.ReadKey();
}
}
public abstract class Person
{
public abstract void StandUp();
}
public class Student : Person
{
public override void StandUp()
{
Console.WriteLine("学生起立,说老师好");
}
}
public class Teacher : Person
{
public override void StandUp()
{
Console.WriteLine("老师起立,说校长好");
}
}
public class HeadMaster : Person
{
public override void StandUp()
{
Console.WriteLine("校长起来说领导好");
}
}
}
2、访问修饰符
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _02访问修饰符
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
}
}
public class Person
{
//protected string _name;
//public int _age;
//private char _gender;
//internal int _chinese;
//protected internal int _math;
}
public class Student : Person
{
}
//public class Student:Person
//{
//}
}
3、简单工厂设计模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03简单工厂设计模式
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入您想要的笔记本品牌");
string brand = Console.ReadLine();
NoteBook nb = GetNoteBook(brand);
nb.SayHello();
Console.ReadKey();
}
/// <summary>
/// 简单工厂的核心 根据用户的输入创建对象赋值给父类
/// </summary>
/// <param name="brand"></param>
/// <returns></returns>
public static NoteBook GetNoteBook(string brand)
{
NoteBook nb = null;
switch (brand)
{
case "Lenovo": nb = new Lenovo();
break;
case "IBM": nb = new IBM();
break;
case "Acer": nb = new Acer();
break;
case "Dell": nb = new Dell();
break;
}
return nb;
}
}
public abstract class NoteBook
{
public abstract void SayHello();
}
public class Lenovo : NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是联想笔记本,你联想也别想");
}
}
public class Acer : NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是鸿基笔记本");
}
}
public class Dell : NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是戴尔笔记本");
}
}
public class IBM : NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是IBM笔记本");
}
}
}
4、值类型和引用类型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04值类型和引用类型
{
class Program
{
static void Main(string[] args)
{
//值类型:int double char decimal bool enum struct
//引用类型:string 数组 自定义类 集合 object 接口
//值传递和引用传递
//int n1 = 10;
//int n2 = n1;
//n2 = 20;
//Console.WriteLine(n1);
//Console.WriteLine(n2);
//Console.ReadKey();
//Person p1 = new Person();
//p1.Name = "张三";
//Person p2 = p1;
//p2.Name = "李四";
//Console.WriteLine(p1.Name);
//Console.ReadKey();
//Person p = new Person();
//p.Name = "张三";
//Test(p);
//Console.WriteLine(p.Name);
//Console.ReadKey();
//string s1 = "张三";
//string s2 = s1;
//s2 = "李四";
//Console.WriteLine(s1);
//Console.WriteLine(s2);
//Console.ReadKey();
int number = 10;
TestTwo(ref number);
Console.WriteLine(number);
Console.ReadKey();
}
//int n=number;
public static void TestTwo(ref int n)
{
n += 10;
}
//Person pp=p;
public static void Test(Person pp)
{
Person p = pp;
p.Name = "李四";
}
}
public class Person
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
}
5、序列化和反序列化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace _05序列化和反序列化
{
class Program
{
static void Main(string[] args)
{
//要将p这个对象 传输给对方电脑
//Person p = new Person();
//p.Name = "张三";
//p.Age = 19;
//p.Gender = '男';
//using (FileStream fsWrite = new FileStream(@"C:\Users\SpringRain\Desktop\111.txt", FileMode.OpenOrCreate, FileAccess.Write))
//{
// //开始序列化对象
// BinaryFormatter bf = new BinaryFormatter();
// bf.Serialize(fsWrite, p);
//}
//Console.WriteLine("序列化成功");
//Console.ReadKey();
//接收对方发送过来的二进制 反序列化成对象
Person p;
using (FileStream fsRead = new FileStream(@"C:\Users\SpringRain\Desktop\111.txt", FileMode.OpenOrCreate, FileAccess.Read))
{
BinaryFormatter bf = new BinaryFormatter();
p = (Person)bf.Deserialize(fsRead);
}
Console.WriteLine(p.Name);
Console.WriteLine(p.Age);
Console.WriteLine(p.Gender);
Console.ReadKey();
}
}
[Serializable]
public class Person
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private char _gender;
public char Gender
{
get { return _gender; }
set { _gender = value; }
}
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
}
}
7、部分类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _06部分类
{
class Program
{
static void Main(string[] args)
{
}
}
public partial class Person
{
private string _name;
public void Test()
{
}
}
public partial class Person
{
public void Test(string name)
{
// _name
}
}
}
8、密封类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _07密封类
{
class Program
{
static void Main(string[] args)
{
}
}
public sealed class Person:Test
{
}
public class Test
{
}
}
9、重写ToString__方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _08重写ToString__方法
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
Console.WriteLine(p.ToString());
Console.ReadKey();
}
}
public class Person
{
public override string ToString()
{
return "Hello World";
}
}
}
10、接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _09接口
{
class Program
{
static void Main(string[] args)
{
//接口就是一个规范、能力。
}
}
public class Person
{
public void CHLSS()
{
Console.WriteLine("我是人类,我可以吃喝拉撒睡");
}
}
public class NBAPlayer
{
public void KouLan()
{
Console.WriteLine("我可以扣篮");
}
}
public class Student : Person,IKouLanable
{
public void KouLan()
{
Console.WriteLine("我也可以扣篮");
}
}
public interface IKouLanable
{
void KouLan();
}
}
11、接口的语法特征
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10接口的语法特征
{
class Program
{
static void Main(string[] args)
{
}
}
public interface IFlyable
{
//接口中的成员不允许添加访问修饰符 ,默认就是public
void Fly();
string Test();
//不允许写具有方法体的函数
// string _name;
string Name
{
get;
set;
}
}
}
12、自动属性和普通属性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _11_自动属性和普通属性
{
class Program
{
static void Main(string[] args)
{
}
}
public class Person
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
/// <summary>
/// 自动属性
/// </summary>
public int Age
{
get;
set;
}
}
}
13、接口特点
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _12接口特点
{
class Program
{
static void Main(string[] args)
{
IFlyable fly = new Bird();//new Person();// new IFlyable();
fly.Fly();
Console.ReadKey();
}
}
public class Person:IFlyable
{
public void Fly()
{
Console.WriteLine("人类在飞");
}
}
public class Student
{
public void Fly()
{
Console.WriteLine("人类在飞");
}
}
public class Bird : IFlyable
{
public void Fly()
{
Console.WriteLine("鸟在飞");
}
}
public interface IFlyable
{
//不允许有访问修饰符 默认为public
//方法、自动属性
void Fly();
}
public interface M1
{
void Test1();
}
public interface M2
{
void Test2();
}
public interface M3
{
void Test3();
}
public interface SupperInterface : M1, M2, M3
{
}
public class Car : SupperInterface
{
public void Test1()
{
throw new NotImplementedException();
}
public void Test2()
{
throw new NotImplementedException();
}
public void Test3()
{
throw new NotImplementedException();
}
}
}
14、接口练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _13接口练习
{
class Program
{
static void Main(string[] args)
{
//麻雀会飞 鹦鹉会飞 鸵鸟不会飞 企鹅不会飞 直升飞机会飞
//用多态来实现
//需方法、抽象类、接口
IFlyable fly = new Plane();//new MaQue();//new YingWu();
fly.Fly();
Console.ReadKey();
}
}
public class Bird
{
public double Wings
{
get;
set;
}
public void EatAndDrink()
{
Console.WriteLine("我会吃喝");
}
}
public class MaQue : Bird,IFlyable
{
public void Fly()
{
Console.WriteLine("麻雀会飞");
}
}
public class YingWu : Bird, IFlyable,ISpeak
{
public void Fly()
{
Console.WriteLine("鹦鹉会飞");
}
public void Speak()
{
Console.WriteLine("鹦鹉可以学习人类说话");
}
}
public class TuoBird : Bird
{
}
public class QQ : Bird
{
}
public class Plane : IFlyable
{
public void Fly()
{
Console.WriteLine("直升飞机转动螺旋桨飞行");
}
}
public interface IFlyable
{
void Fly();
}
public interface ISpeak
{
void Speak();
}
}
15、显示实现接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _14_显示实现接口
{
class Program
{
static void Main(string[] args)
{
//显示实现接口就是为了解决方法的重名问题
IFlyable fly = new Bird();
fly.Fly();
Bird bird = new Bird();
bird.Fly();
Console.ReadKey();
}
}
public class Bird : IFlyable
{
public void Fly()
{
Console.WriteLine("鸟飞会");
}
/// <summary>
/// 显示实现接口
/// </summary>
void IFlyable.Fly()
{
Console.WriteLine("我是接口的飞");
}
}
public interface IFlyable
{
void Fly();
}
}
16、总结
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _15_总结
{
class Program
{
static void Main(string[] args)
{
//什么时候用虚方法来实现多态?
//什么使用用抽象类来实现多态?
//什么时候用接口来实现多态?
//真的鸭子会游泳 木头鸭子不会游泳 橡皮鸭子会游泳
ISwimming swim = new XPDuck();//new RealDuck();
swim.Swim();
Console.ReadKey();
}
}
public class RealDuck:ISwimming
{
public void Swim()
{
Console.WriteLine("真的鸭子靠翅膀游泳");
}
}
public class MuDuck
{
}
public class XPDuck : ISwimming
{
public void Swim()
{
Console.WriteLine("橡皮鸭子飘着游泳");
}
}
public interface ISwimming
{
void Swim();
}
}
17、GUID产生一个不会重复的编号
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _17GUID
{
class Program
{
static void Main(string[] args)
{
//产生一个不会重复的编号
Console.WriteLine(Guid.NewGuid().ToString());
Console.WriteLine(Guid.NewGuid().ToString());
Console.WriteLine(Guid.NewGuid().ToString());
Console.WriteLine(Guid.NewGuid().ToString());
Console.WriteLine(Guid.NewGuid().ToString());
Console.ReadKey();
}
}
}