多态:
能让一个对象有多种状态(类型)。
实现多态的方法:
- 虚方法
在父类中跟子类相同的方法的前面加关键字,父类的加virtual,子类的加override.
实现原理:其实还是调用的父类的方法,但是virtual表示此方法可被重写,override表示重写父类方法。最后调用时父类对象中装了什么类型就调用什么类型的虚函数。
//Person.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Person
{
public virtual void Func()
{
Console.WriteLine("This is a");
}
}
class One:Person
{
public override void Func()
{
Console.WriteLine("This is b");
}
}
class Two : Person
{
}
class Therr : Person
{
public new void Func() //new 关键字 代表Func重写
{
Console.WriteLine("This is d");
}
}
}
// main.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Person men1, men2, men3, men4, men5; //声明类
men1 = new One(); // One Two Three 都是实现类
men2 = new Two();
men3 = new Therr();
men1.Func();
men2.Func();
men3.Func(); //new代表重写,所以Fun会先找声明类的Fun发现Fun是虚函数,这就找Three类中
//有没有实现,发现没有实现,则执行父类
Therr Last = new Therr();
Last.Func();
Console.ReadKey();
}
}
}
- 抽象类
抽象类以abstract关键字修饰。
抽象成员必须在抽象类中。
抽象类中可以有具体成员,但是自己不能使用,供子类调用。
子类继承父类,重新方法时要在重写方法前加上关键字override
子类继承了抽象类,必须要把抽象类中的抽象成员重写,子类也是抽象类则可以不用重写。
抽象类的访问修饰符不能是private(只能在当前类中访问)。
抽象类有构造函数,但不能被实例化。
/* 抽象类是不能被创建, 因为是抽象的不是具体的,所以不能被创建 */
public abstract class Animal //当这个类不知道怎么实现时,可以先写成抽象类
{
/* 写抽象方法是为了让子类重写 */
public abstract void Func(); //当方法不知道怎么实现时可以写为抽象方法,抽象方法无函数体。
public abstract string _name; //抽象属性,需要子类实现 (子类实现快捷键 Alt+shift +F10)
{
get;
set;
}
}
Path: C-_learn\object\Nine --->用抽象类的方法实现各种形状求面积求周长
访问修饰符:
internal: 类的默认修饰符,只允许在当前项目中访问。在同一个项目中internal 跟 public作用是一样的。
protected internal: protected 的权限加上 internal的访问权限。
Note:
可访问性不一致:子类的访问权限大于父类时会出现这个错误。
子类的访问权限不能高于父类。
传输对象:
序列化:将对象转化为二进制。
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(流对象, 需要序列化的对象); //将需要转换的对象写入到流对象中,无返回值。
反序列化:将二进制转化为对象。
BinaryFormatter bf = new BinaryFormatter();
bf.Deserialize(流对象); //将流对象中的二进制转换为对象并放回。
部分类:
在同一个命名空间下新建两个相同的类名,需要使用partial关键字。
class partial Person
{
private stirng _name;
}
class partial Person
{
pulic void Myprint()
{
Console.WriteLine(_name); //部分类其实就是同一个类,可以相互访问
}
}
密封类:
密封类可以继承类,但是自己不能被继承。用sealed关键字修饰。
class Wangking
{
}
class sealed Person:Wangking //正确的, 密封类可以继承其它类
{
}
class Woman:Person //错误的,密封类不能被继承
{
}
- 接口(遵循的规范, 能力:有某种功能)
需要多继承时,使用接口。用interface 关键字修饰。
只能有方法的声明,不能有方法的实现(接口中不能有方法的实现 )。
接口中不能有访问修饰符,默认public
接口不能实例化对象。
接口不能继承类,只能继承接口。类可以继承接口/类。
一个子类继承了接口,必须要实现接口中的所有方法。
一个类继承了接口,其它类要继承这个类,继承时需要先写类,再写接口。
public interface IFlyable //接口的名字以 I 开头以ble结尾,表示I.....ble 什么的能力
{
// string name; //不能有字段,就意味着不能有构造函数。构造函数时初始化字段的。
void Lili(); //只能有声明不能有实现。
}
Path: C-_learn\object\Twelve
MD5加密:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string a = Md5Encryption("321");
Console.WriteLine(a);
}
static string Md5Encryption(string str)
{
/* 创建对象 */
MD5 md5 = MD5.Create();
/* 先将数组转换为字节数组 */
byte[] buffer = Encoding.GetEncoding("GBK").GetBytes(str);
/*开始加密*/
byte[] EncryData = md5.ComputeHash(buffer);
string data = null;
for (int i = 0; i < EncryData.Length; i++)
{
data += EncryData[i].ToString("x2"); //转换为16进制格式
}
return data;
}
}
}