C#----多态3

一、访问修饰符

1、c#中的访问修饰符

public : 公开的公共的
private: 私有的,只能在当前类的内部访问
protected: 受保护的,只能在当前类的内部以及该类的子类中访问。

internal: 只能在当前项目中访问。在同一个项目中,internal和public的权限是一样。
protected internal: 这两个关键字的功能组合是或的关联,也就说被protected internal修饰的成员既可以在继承链上的类里面被访问,也可以在同一个项目中使用。
 1)、能够修饰类的访问修饰符只有两个: public、 internal

 2)、可访问性不一致。
子类的访问权限不能高于父类的访问权限,会暴露父类的成员。

二、简单工厂设计模式

1.设计模式

设计这个项目的一种方式。

namespace Pratise
{
    class Mode
    {
        static void Main(string[] arg)
        {
            Console.WriteLine("请输入你想要的笔记本品牌");
            string brand = Console.ReadLine();
            NoteBook nb = GetNoteBook(brand);
            nb.Sayhello();
            Console.ReadKey();
        }
        ///
        /// 简单工厂的核心 根据用户的输入创建对象赋值给父类
        ///
        /// 电脑品牌
        /// 子类
        public static NoteBook GetNoteBook(string brand)
        {
            NoteBook nb = null;
            switch (brand)
            {
                case "Lenovo": nb = new Lenovo(); break;
                case "Acer": nb = new Acer(); break;
                case "Dell": nb = new Dell(); break;
                case "IBM": nb = new IBM(); 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("我的宏基笔记本,red gay");
        }
    }

    public class Dell : NoteBook
    {

        public override void Sayhello()
        {
            Console.WriteLine("我是戴尔笔记本,一带一路");
        }
    }

    public class IBM : NoteBook
    {

        public override void Sayhello()
        {
            Console.WriteLine("我的IBM笔记本");
        }
    }
}

三、值传递和引用类型传递

值类型: int double char decimal bool enum struct

引用类型: string  数组  自定义类  集合 object   接口

 值类型在复制的时候,传递的是这个值得本身。

引用类型在复制的时候,传递的是对这个对象的引用-

四、序列化和反序列化

序列化: 就是将对象转换为二进制。

反序列化: 就是将二进制转换为对象。

作用: 传输数据。

序列化:
1)、将这个类标记为可以被序列化的。类的上边[Serializable]

namespace Pratise
{
    //序列化与反序列化
    class Sbilze
    {
        static void Main(string[] arg)
        {
            要将P这个对象 传输给对方电脑
            //Person p = new Person();
            //p.Name = "张三";
            //p.Age = 19;
            //p.Gender = '男';
            写到桌面
            //using (FileStream fsWrite = new FileStream(@"C:\Users\Hey\Desktop\b.txt", FileMode.OpenOrCreate, FileAccess.Write))
            //{
            //    //开始序列化对象
            //    BinaryFormatter bf = new BinaryFormatter();
            //    bf.Serialize(fsWrite, p);
            //}
            //Console.WriteLine("序列化成功");
            //接收对方发送过来的二进制  反序列化成对象
            Person p;
            using (FileStream fsRead = new FileStream(@"C:\Users\Hey\Desktop\b.txt",FileMode.OpenOrCreate,FileAccess.Read))
            {
                BinaryFormatter bf = new BinaryFormatter();
                p = (Person)bf.Deserialize(fsRead);
            }
            Console.WriteLine(p.Name);
            Console.WriteLine(p.Gender);
            Console.WriteLine(p.Age);
            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; }
        }
    }
}

五、部分类和密封类

1.部分类

partial 使都为student类的一部分 ,方法和属性都共有

public partial class Student
    {
        private string _name;
    }
    public partial class Student{
        public void test(){
            _name = "ds";
        }
    }

2.密封类

sealed 标记类为密封类 

密封类不能被继承,但是密封类可以继承别的类。

public sealed class Teacher
    {
    }

六、接口

接口就是一个规范 能力。

只要一个类继承了一个接口,这个类就必须实现这个接口所有的成员。

为了多态。接口不能被实例化。也就是,接口不能new(不能创建对象)
接口中的成员不能加  “访问修饰符” ,接口中的成员访问修饰符为  pub1ic,不能修改。
   (默认为public)接口中的成员不能有任何实现 ( “光说不做”, 只是定义了一组未实现的成员)。
接口中只能有方法、属性、索引器、事件,不能有“字段”和构造函数。
接口与接口之间可以继承,并且可以多继承。
接口并不能去继承一个类,而类可以继承接口  ( 接口只能继承于接口,而类既可以继承接口,也可以继承类)

实现接口的子类必须实现该接口的全部成员。
一个类可以同时继承一个类并实现多个接口,如果一个子类同时继承了父类A,并实现了接口 IA,那么语法上A必须写在 IA的前面。
class MyClass:A, IA,因为类是单继承的。


显示实现接口的目的:解决方法的重名问题
什么时候显示的去实现接口:

当继承的借口中的方法和参数一摸一样的时候,要是用显示的实现接口
当一个抽象类实现接口的时候,需要子类去实现接口。
    class Port
    {
        static void Main(string[] arg)
        {
            //接口就是一个规范 能力。|
            Kko k = new Student();// Person();
            k.round();
            Console.ReadKey();
        }
    }

    public class Person:Kko
    {
        public void Talk()
        {
            Console.WriteLine("我是人类,我可以交流");
        }

        public void round()
        {
            Console.WriteLine("人类在转圈圈");
        }
    }

    //接口
    public interface Kko
    {
        //接口中的成员不允许添加访问修饰符,默认就是public
        void round();
        //不允许写具有方法体的函数
    }
    public interface K1
    {
        void test();
    }
    public interface K2
    {
        void test2();
    }
    public interface Superface : K1, K2, Kko
    {

    }
    public class Student : Person,Kko,K2
    {

        public void round()
        {
            Console.WriteLine("学生可以转圈圈");
        }
    }
    public class Kid : Superface
    {

        public void test()
        {
            throw new NotImplementedException();
        }

        public void round()
        {
            throw new NotImplementedException();
        }

        public void test2()
        {
            throw new NotImplementedException();
        }
    }

   


    class Fly
    {
        static void Main(string[] arg)
        {
            //麻雀会飞 鹦鹉会飞 鸵鸟不会飞 企鹅不会飞 直升飞机会飞
            //用多态来实现
            //需方法、抽象类、接口
            IFly fly = new YingWu();// Plane();
            fly.Fly();
            Console.ReadKey();
        }
    }
    public  class Bir
    {
        public double Wings
        {
            get;
            set;
        }
        public void Thing()
        {
            Console.WriteLine("我会正常鸟类都能干事");
        }
    }
    public interface IFly
    {
        void Fly();
    }
    public interface Talk
    {
        void Speak();
    }
    public class MaQue : Bir, IFly
    {

        public void Fly()
        {
            Console.WriteLine("I can fly ");
        }
    }
    public class YingWu : Bir,IFly,Talk
    {

        public void Fly()
        {
            Console.WriteLine("I can fly");
        }

        public void Speak()
        {
            Console.WriteLine("I can Communication with human");
        }
    }

    public class TuoBird : Bir
    {

    }
    public class QQ : Bir
    {

    }
    public class Plane : IFly
    {

        public void Fly()
        {
            Console.WriteLine("一直在飞行");
        }
    }

显示实现接口

 显示实现接口就是为了解决方法的重名问题

在类中默认访问符是private, 在接口中是public


    class Connector
    {
        static void Main(string[] arg)
        {
            //显示实现接口就是为了解决方法的重名问题
            IFyable f = new HAHA();
            f.Fly();
            HAHA ha = new HAHA();
            ha.Fly();
            Console.ReadKey();
        }
    }

    public class HAHA:IFyable
    {
        public void Fly()
        {
            Console.WriteLine("会飞的东西");
        }
        ///
        /// 显示实现接口
        ///
        void IFyable.Fly()
        {
            Console.WriteLine("我也会飞");
        }
    }
   public interface IFyable
    {
        void Fly();
    }

练习


    class Pratise
    {
        static void Main(string[] args)
        {
            //什么时候用虚方法来实现多态?   抽象出来的父类,知道怎么写这个方法,要创建一个父类对象
            //什么使用用抽象类来实现多态?  能够抽象出来一个父类并且共有的方法,不知道如何写这个方法
            //什么时候用接口来实现多态?    共同的行为 、能力

            //真的鸭子会游泳   木头鸭子不会游泳  橡皮鸭子会游泳
            ISwimming swim = new XPDuck(); //RealDuck();
            swim.Swimming();
            Console.ReadKey();

        }
    }

    public class RealDuck:ISwimming
    {

        public void Swimming()
        {
            Console.WriteLine("用脚游泳");
        }
    }
    public class MuDuck : RealDuck
    {

    }
    public class XPDuck : ISwimming
    {

        public void Swimming()
        {
            Console.WriteLine("会站在上面游泳");
        }
    }
    public interface ISwimming
    {
        void Swimming();
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值