c#自学之路第十八天

一、笔记

1、多态

实现多态的三种方法:1.虚方法 2.抽象类 3.接口

1.虚方法:
步骤:
1.将父类的方法标记为虚方法,使用关键子virtual。表示这个函数可以被子类重新写一遍。
2.将子类的方法用关键字 override标记

2.抽象类

  1. 当父类的方法不知道如何去实现的时候,可以考虑将父类写成抽象类,将方法写成抽象方法,使用关键字abstract
    2.子类的方法用关键字 override标记
    注意:1.抽象方法不允许有方法体;
    2.抽象类不能创建对象;
    3.子类中必须实现父类的所有抽象成员
    4.抽象类中可以有非抽象的成员,但是抽象成员必须再抽象类中
    5.抽象类中可以有虚方法

3.接口 当一个类想继承多个父类时应该想到用接口 子类必须实现接口中的函数; 语法: 【public】interface I…able(这是接口名) {
成员; }

注意:1.接口中的成员不允许添加访问修饰符,默认就是public
2.不允许有方法体,也就是不能实现函数
3.接口中不能写字段,可以写自动属性(不能写普通属性)
4.接口不能被实例化(接口,抽象类,静态类)
本质:接口中只能有方法

特点: 接口是一种规范 只要一个类继承了一个接口,这个类就必须实现这个接口中所有的成员

为了多态,接口不能被实例化 也就是说,接口不能new(不能创建对象)

接口中的成员不能加“访问修饰符”,接口中的成员访问修饰符默认为public,不能修改 接口中的成员不能有任何实现
接口中只能有方法、属性、索引器、事件,不能有字段和构造函数 接口与接口之间可以继承,并且可以多继承
接口不能继承一个类,但是类可以继承接口(接口只能继承接口,而类既可以继承接口,也可以继承类) 实现接口的子类必须实现该接口的全部成员
一个类可以同时继承一个类并实现多个接口,如果一个类同时继承了父类A,并实现了接口A,那么语法上必须写在IA的前面 类是单继承的

2.显示实现接口

当类和接口的方法相同时,区分调用类的方法还是接口的方法 见代码

3.部分类 partical

4.MD5加密

涉及到密码都要加密,不能逆解密 见代码,且只能用此方法

二、代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态
{
    class Program
    {
        static void Main(string[] args)
        {
            //让一个对象表现出多种状态(类型)
            //实现多态的3种手段:1.虚方法 2.抽象类 3.接口
            Chinese cn1 = new Chinese("韩梅梅");
            Chinese cn2 = new Chinese("李雷");
            Japanse j1 = new Japanse("属下军");
            Japanse j2 = new Japanse("进别字");
            Korea k1 = new Korea("京休闲");
            Korea k2 = new Korea("陷井秀");
            American a1 = new American("科比布莱恩特");
            American a2 = new American("奥尼尔");
            Person[] pers = { cn1,cn2,j1,j2,k1,k2,a1,a2};
            for(int i=0;i<pers.Length;i++)
            {
                pers[i].SayHello();//全部调用的是父类的Sayhello方法
               分别显示各类
               //if(pers[i] is Chinese)
               // {
               //     ((Chinese)pers[i]).SayHello();
               // }
               //else if(pers[i] is Japanse)
               // {
               //     ((Japanse)pers[i]).SayHello();
               // }
               //else if(pers[i] is Korea)
               // {
               //     ((Korea)pers[i]).SayHello();
               // }
               //else if(pers[i] is American)
               // {
               //     ((American)pers[i]).SayHello();
               // }
            }
            Console.ReadKey();
        }
    }

    public class Person
    {
        private string _name;//字段
        public string Name//属性
        {
            get { return _name; }
            set { _name = value; }
        }
        public Person(string name)//构造方法
        {
            this.Name = name;
        }
        public virtual void  SayHello()//函数
        {
            Console.WriteLine("我是人类");
        }
    }

    public class Chinese:Person
    {
        public Chinese(string name):base(name)
        {

        }

        public override void SayHello()
        {
            Console.WriteLine("我是中国人,我叫{0}", this.Name);
        }
    }

    public class Japanse:Person
    {
        public Japanse(string name):base(name)
        {

        }
        public override void SayHello()
        {
            Console.WriteLine("我是日本人,我叫{0}", this.Name);
        }
    }

    public class Korea : Person
    {
        public Korea(string name) : base(name)
        {

        }
        public override void SayHello()
        {
            Console.WriteLine("我是韩国人,我叫{0}", this.Name);
        }
    }

    public class American : Person
    {
        public American(string name) : base(name)
        {

        }
        public override void SayHello()
        {
            Console.WriteLine("我是美国人,我叫{0}", this.Name);
        }
    }

}

2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态之抽象类
{
    class Program
    {
        static void Main(string[] args)
        {
            //狗狗会叫 猫咪会叫
            Animal a = new Dog();
            a.Bark();
            Animal b = new Cat();
            b.Bark();
            Console.ReadKey();
        }
    }
    public abstract class Animal
    {
        public abstract void Bark();
        public abstract string Name
        {
            get;
            set;
        }
    }
    public class Dog:Animal
    {
        public override string Name { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        public override void Bark()
        {
            Console.WriteLine("狗狗汪汪叫");
        }
    }
    public class Cat:Animal
    {
        public override string Name { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        public override void Bark()
        {
            Console.WriteLine("猫咪喵喵叫");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口
{
    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();
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口的语法特征
{
    class Program
    {
        static void Main(string[] args)
        {
            IFlyable a = new Bird();
            a.Fly();
            Console.ReadKey();
        }
    }

    public class Person:IFlyable
    {
        public void Fly()
        {
            Console.WriteLine("人类在飞");
        }
    }
    public class Bird:IFlyable
    {
        public void Fly()
        {
            Console.WriteLine("鸟在飞");
        }
    }
    public interface IFlyable
    {
        void Fly();
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //麻雀会飞 鹦鹉会飞,鸵鸟不会飞,企鹅不会飞,直升机会飞
            //用多态来实现
            //需要方法、抽象类、接口
            IFlyable a = new YingWu();
            a.Fly();
            Console.ReadKey();

        }
    }
    public class Bird:IFlyable
    {
        public double Wings
        {
            get;
            set;
        }
        public void EatAndDrink()
        {
            Console.WriteLine("我会吃喝");
        }
        public void Fly()
        {
            Console.WriteLine("会飞");
        }
    }
    public class MaQue:Bird,IFlyable
    {
        public void Fly()
        {
            Console.WriteLine("麻雀会飞");
        }
    }
    public class YingWu:Bird,IFlyable
    {
        
        public void Fly()
        {
            Console.WriteLine("鹦鹉会飞");
        }
    }
    public interface IFlyable
    {
        void Fly();
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 模拟移动硬盘_U盘
{
    class Program
    {
        static void Main(string[] args)
        {
            //用多态来实现 将移动硬盘或者U盘插入电脑显示
            //MobileDisk md = new MobileDisk();
            //USBDisk u = new USBDisk();
            //MP3Disk mp3= new MP3Disk();
            //Computer cpu = new Computer();
            //cpu.CpuRead(u);
            //cpu.CpuWrite(u);
            //Console.ReadKey();

            MobileStorage ms = new MobileDisk();//new USBDisk();
            Computer cpu = new Computer();
            cpu.MS = ms;
            cpu.CpuRead();
            cpu.CpuWrite();
            //Computer cpu = new Computer();
            //cpu.CpuRead(ms);
            //cpu.CpuWrite(ms);
            Console.ReadKey();
        }
    }
    /// <summary>
    /// 抽象的父类
    /// </summary>
    public abstract class MobileStorage
    {
        public abstract void Read();
        public abstract void Write();
    }
    public class MobileDisk:MobileStorage
    {
        public override void Read()
        {
            Console.WriteLine("移动硬盘在读取数据");
        }
        public override void Write()
        {
            Console.WriteLine("移动硬盘在写数据");

        }
    }

    public class USBDisk:MobileStorage
    {
        public override void Read()
        {
            Console.WriteLine("usb在读取数据");
        }
        public override void Write()
        {
            Console.WriteLine("usb在写入数据");
        }

    }

    public class MP3Disk:MobileStorage
    {
        public override void Write()
        {
            Console.WriteLine("MP3在写数据");
        }
        public override void Read()
        {
            Console.WriteLine("MP3在读数据");
        }
        public void PlayMusic()
        {
            Console.WriteLine("MP3自己可以播放音乐");
        }
    }

    public class Computer
    {
        private MobileStorage _ms;
        public MobileStorage MS
        {
            get { return _ms; }
            set { _ms = value; }
        }
        public void CpuRead()
        {
            MS.Read();
        }
        public void CpuWrite()
        {
            MS.Write();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态之抽象类练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //使用多态求矩形的面积和周长以及园的面积和周长
            Square shape = new  Square(5,6);
            double area= shape.GetArea();
            double perimeter = shape.GetPerimeter();
            Console.WriteLine("这个形状的面积是{0},周长是{1}", area, perimeter);
            Console.ReadKey();
        }

    }
    public abstract class Shape
    {
        public abstract double GetArea();
        public abstract double GetPerimeter();
    }
    public class CirCle:Shape
    {
        private double _r;
        public double R
        {
            get { return _r; }
            set { _r = value; }
        }
        public CirCle(double R)
        {
            this.R = _r;
        }
        public override double GetArea()
        {
            return Math.PI * this.R * this.R;
        }
        public override double GetPerimeter()
        {
            return 2*Math.PI*this.R;
        }
    }

    public class Square:Shape
    {
        private double _height;
        private double _width;
        public double Height
        {
            get { return _height; }
            set { _height = value; }
        }
        public double Width
        {
            get { return _width; }
            set { _width = value; }
        }
        public Square(double Height,double Width)
        {
            this.Height = _height;
            this.Width = _width;
        }
        public override double GetArea()
        {
            return this.Height*this.Width ;
        }
        public override double GetPerimeter()
        {
            return (this.Width+this.Height)*2;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 显示实现接口
{
    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("鸟会飞");
        }
        void IFlyable.Fly()
        {
            Console.WriteLine("我是接口的飞");
        }
    }
    public interface IFlyable
    {
        void Fly();
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace MD5加密
{
    class Program
    {

        static void Main(string[] args)
        {
            //123
            //202cb962ac59075b964b07152d234b70
            string s = Getmd5("123");
            Console.WriteLine(s);
            Console.ReadKey();
        }
        public static string Getmd5(string str)
        {
            //创建MD5对象
            MD5 md5 = MD5.Create();
            //开始加密
            //需要将字符处转换成字节数组
            byte[] buffer = Encoding.Default.GetBytes(str);
            //返回一个加密好的字节数组
            byte[] MD5Buffer= md5.ComputeHash(buffer);

            //将字节数组转换成字符串
            //字节数组转换成字符串的3中方法
            //1.将字节数组中的每个元素按照指定的编码格式解析成字符串
            //2.直接将字串ToString()
            //3.将字节数组中的每个元素ToString
            string s = "";
            for(int i=0;i<MD5Buffer.Length;i++)
            {
                s += MD5Buffer[i].ToString("x2");//“x”表示将其转换为16进制,2表示对其
            }
            return s;
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逆火重生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值