0413.Net就业班第一天

抽象类

1.抽象成员必须标记为abstract,并且不能有任何实现。
2.抽象成员必须在抽象类中。
3.抽象类不能被实例化

4.子类继承抽象类后,必须把父类中的所有抽象成员都重写。
(除非子类也是一个抽象类,则可以不重写)

5.抽象成员的访问修饰符不能是private

6.在抽象类中可以包含实例成员。
并且抽象类的实例成员可以不被子类实现

7.抽象类是有构造函数的。虽然不能被实例化。

8、如果父类的抽象方法中有参数,那么。继承这个抽象父类的子类在重写父类的方法的时候必须传入对应的参数。

如果抽象父类的抽象方法中有返回值,那么子类在重写这个抽象方法的时候 也必须要传入返回值。

======
如果父类中的方法有默认的实现,并且父类需要被实例化,这时可以考虑将父类定义成一个普通类,用虚方法来实现多态。

如果父类中的方法没有默认实现,父类也不需要被实例化,则可以将该类定义为抽象类。

接口类

接口是一种规范。也是一种能力。
只要一个类继承了一个接口,这个类就必须实现这个接口中所有的成员

接口的功能要单一

为了多态。
接口不能被实例化。
也就是说,接口不能new(不能创建对象)
跟抽象类是一样的,因为创建对象也没意义。

接口中的成员不能加“访问修饰符”,接口中的成员访问修饰符为public,不能修改。
类中的成员默认的访问修饰符是private ,而接口中默认的访问修饰符是public

(默认为public)
接口中的成员不能有任何实现(“光说不做”,只是定义了一组未实现的成员)。

接口中只能有方法、属性、索引器、事件,不能有“字段”和构造函数。

接口与接口之间可以继承

接口并不能去继承一个类,而类可以继承接口 (接口只能继承于接口,而类既可以继承接口,也可以继承类)

实现接口的子类必须实现该接口的全部成员。

一个类可以同时继承一个类并实现多个接口,如果一个子类同时继承了父类A,并实现了接口IA,那么语法上A必须写在IA的前面。

class MyClass:A,IA{},因为类是单继承的。

显示实现接口的目的:解决方法的重名问题
什么时候显示的去实现接口:
当继承的接口中的方法和参数一摸一样的时候,要是用显示的实现接口

当一个抽象类实现接口的时候,需要子类去实现接口。

01进程

点击查看代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01进程
{
    class Program
    {
        static void Main(string[] args)
        {
            //存储着我们当前正在运行的进程
            //Process[] pro = Process.GetProcesses();
            //foreach (var item in pro)
            //{
            //    //item.Kill();不试的不是爷们
            //    Console.WriteLine(item.ProcessName);
            //}

            //使用进程来打开应用程序
            //Process.Start("notepad");//打开记事本
            //Process.Start("mspaint");//打开画图工具
            //Process.Start("iexplore", "http://www.baidu.com");
            //Process.Start("calc");//打开计算器


            //使用进程来打开文件


            //封装我们要打开的文件 但是并不去打开这个文件
            ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\SpringRain\Desktop\打开文件练习.exe");
            //创建进程对象
            Process pro = new Process();
            //告诉进程要打开的文件信息
            pro.StartInfo = psi;
            //调用函数打开
            pro.Start();
            Console.ReadKey();
        }
    }
}

02打开文件练习

点击查看代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02打开文件练习
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入要打开的文件所在的路径");
            string filePath = Console.ReadLine();
            Console.WriteLine("请输入要打开的文件的名字");
            string fileName = Console.ReadLine();

            //通过简单工厂设计模式返回父类

            BaseFile bf = GetFile(filePath, fileName);
            if (bf != null)
            {
                bf.OpenFile();
            }
            Console.ReadKey();
        }

        static BaseFile GetFile(string filePath,string fileName)
        {
            BaseFile bf = null;
            string strExtension = Path.GetExtension(fileName);//3.txt
            switch (strExtension)
            { 
                case ".txt":
                    bf = new TxtFile(filePath, fileName);
                    break;
                case ".avi":
                    bf = new AviFile(filePath, fileName);
                    break;
                case ".mp4":
                    bf = new MP4File(filePath, fileName);
                    break;
            }
            return bf;
        }

    }

    class BaseFile
    { 
        //字段、属性、构造函数、函数、索引器
        private string _filePath;
        public string FilePath//ctrl+R+E
        {
            get { return _filePath; }
            set { _filePath = value; }
        }

        //自动属性 prop+两下tab
        public string FileName { get; set; }

        public BaseFile(string filePath, string fileName)
        {
            this.FilePath = filePath;
            this.FileName = fileName;
        }

      
        //设计一个函数  用来打开指定的文件
        public void OpenFile()
        {
            ProcessStartInfo psi = new ProcessStartInfo(this.FilePath + "\\" + this.FileName);
            Process pro = new Process();
            pro.StartInfo = psi;
            pro.Start();
        }
    }

    class TxtFile : BaseFile
    { 
        //因为子类会默认调用父类无参数的构造函数

        public TxtFile(string filePath, string fileName)
            : base(filePath, fileName)
        { }
    }


    class MP4File : BaseFile
    {
        public MP4File(string filePath, string fileName)
            : base(filePath, fileName)
        { }
    }

    class AviFile : BaseFile
    {
        public AviFile(string filePath, string fileName)
            : base(filePath, fileName)
        { }
    }
}

06里氏转换

点击查看代码
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)
        {
            Person person = new Student();

            //if (person is Teacher)
            //{
            //    ((Teacher)person).TeacherSayHello();
            //}
            //else
            //{
            //    Console.WriteLine("转换失败");
            //}

            Student s = person as Student;//将person转换为student对象
            if (s != null)
            {
                s.StudentSayHello();
            }
            else
            {
                Console.WriteLine("转换失败");
            }
            Console.ReadKey();
            //is as
        }
    }
    class Person
    {
        public void PersonSayHello()
        {
            Console.WriteLine("我是父类");
        }
    }

    class Student : Person
    {
        public void StudentSayHello()
        {
            Console.WriteLine("我是学生");
        }
    }

    class Teacher : Person
    {
        public void TeacherSayHello()
        {
            Console.WriteLine("我是老师");
        }
    }
}

07虚方法

点击查看代码
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)
        {
            //员工九点打卡  经理11点打卡 程序猿不打卡

           // Employee emp = new Employee();
           //// emp.DaKa();
           // Manager m = new Manager();
           // //m.DaKa();
           // Programmer p = new Programmer();
           // //p.DaKa();

          //  Employee e = m;

            //Employee[] emps = { emp, m, p };
            //for (int i = 0; i < emps.Length; i++)
            //{
            //    //if (emps[i] is Manager)
            //    //{
            //    //    ((Manager)emps[i]).DaKa();  
            //    //}
            //    //else if (emps[i] is Programmer)
            //    //{
            //    //    ((Programmer)emps[i]).DaKa();
            //    //}
            //    //else
            //    //{
            //    //    emps[i].DaKa();
            //    //}
            //    emps[i].DaKa();
            //}

            Employee emp = new Programmer();//new Manager(); //new Employee();
            emp.DaKa();


            Console.ReadKey();
        }
    }

    class Employee
    {
        public virtual void DaKa()
        {
            Console.WriteLine("员工九点打卡");
        }
    }

    class Manager : Employee
    {
        public override void DaKa()
        {
            Console.WriteLine("经理11点打卡");
        }
    }

    class Programmer : Employee
    {
        public override void DaKa()
        {
            Console.WriteLine("程序猿不打卡");
        }
    }
}

08抽象类

点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _08抽象类
{
    class Program
    {
        static void Main(string[] args)
        {
            //抽象类不允许创建对象
            Animal a = new Cat(); //new Dog();
            a.Bark();
            Console.ReadKey();
        }
    }

    abstract class Animal
    {
        public abstract void Bark();

        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        int _age;

        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }

        public Animal(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }
        public Animal()
        {

        }
    }

    class Dog : Animal
    {

        public override void Bark()
        {
            Console.WriteLine("狗狗旺旺的叫");
        }
    }

    class Cat : Animal
    {
        public override void Bark()
        {
            Console.WriteLine("猫咪喵喵的叫");
        }
    }
}

10移动设备练习

点击查看代码
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)
        {
            MobileDisk md = new MobileDisk();
            MP3 mp3 = new MP3();
            UDisk u = new UDisk();


            Computer cpu = new Computer();
            Telphone tel = new Telphone();
            cpu.MS = tel;//u;//mp3;//将MP3插到了电脑上
            cpu.CpuWrite();
            cpu.CpuRead();
          //  mp3.PlayMusic();
            Console.ReadKey();
        }
    }

    abstract class MobileStorage
    {
        public abstract void Read();
        public abstract void Write();
    }

    class Telphone : MobileStorage
    {
        public override void Read()
        {
            Console.WriteLine("手机在读取数据");
        }

        public override void Write()
        {
            Console.WriteLine("手机在写入数据");
        }
    }


    class MobileDisk : MobileStorage
    {
        public override void Read()
        {
            Console.WriteLine("移动硬盘在读取数据");
        }

        public override void Write()
        {
            Console.WriteLine("移动硬盘在写入数据");
        }
    }


    class UDisk : MobileStorage
    {
        public override void Read()
        {
            Console.WriteLine("U盘在读取数据");
        }

        public override void Write()
        {
            Console.WriteLine("U盘在写入数据");
        }
    }

    class MP3 : MobileStorage
    {
        public override void Read()
        {
            Console.WriteLine("Mp3在读取数据");
        }

        public override void Write()
        {
            Console.WriteLine("Mp3在写入数据");
        }

        public void PlayMusic()
        {
            Console.WriteLine("Mp3可以自己播放音乐");
        }


    }


    class Computer
    {
        public MobileStorage MS
        {
            get;
            set;
        }

        public void CpuRead()
        {
            this.MS.Read();
        }

        public void CpuWrite()
        {
            this.MS.Write();
        }
    }
}

11接口的使用

点击查看代码
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)
        {
            //实现多态
            IKouLan kl = new Driver();//new Teacher();//new Student();
            kl.KouLan();
            Console.ReadKey();
        }
    }

    class Person
    {
        public void CHLSS()
        {
            Console.WriteLine("人类可以吃喝拉撒睡");
        }
    }

    class NoTuiPerson : Person
    {

    }

    class Student : Person, IKouLan
    {
        public void KouLan()
        {
            Console.WriteLine("学生可以扣篮");
        }
    }
    class Teacher : Person, IKouLan
    {

        public void KouLan()
        {
            Console.WriteLine("老师也可以扣篮");
        }
    }

    class Driver : Person, IKouLan
    {
        public void KouLan()
        {
            Console.WriteLine("司机也可以扣篮");
        }
    }

    interface IKouLan
    {
        void KouLan();
    }


    class NBAPlayer : Person
    {
        void KouLan()
        {
            Console.WriteLine("NBA球员可以扣篮");
        }
    }
}

12接口案例

点击查看代码
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)
        {
            //麻雀 会飞 企鹅不会飞 鸵鸟不会飞 鹦鹉会飞 飞机 会飞

            IFly[] flys = { new MaBird(), new YingWu(), new Plane() };

            for (int i = 0; i < flys.Length; i++)
            {
                flys[i].Fly();
            }
            Console.ReadKey();
        }
    }

    class Bird
    {
        public void CHLSS()
        {
            Console.WriteLine("鸟都会吃喝拉撒睡");
        }
    }

    interface IFly
    {
         void Fly();
        // void DownEggs();
    }

    class MaBird : Bird, IFly
    {

        public void Fly()
        {
            Console.WriteLine("麻雀会飞");
        }
    }

    class TuoBird : Bird
    { }

    class YingWu : Bird, IFly
    {
        public void Fly()
        {
            Console.WriteLine("鹦鹉也会飞");
        }
    }
    class QQ : Bird
    {

    }

    class Plane : IFly
    {
        public void Fly()
        {
            Console.WriteLine("飞机也会飞");
        }
    }


    interface IStrong
    {
        void Strong();
    }

    interface NKWC
    {
        void NKWC();
    }


    interface ISupperMan : IFly, IStrong, NKWC
    { }


    class Student : ISupperMan
    {
        public void Fly()
        {
            throw new NotImplementedException();
        }

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

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

}

13显示实现接口

点击查看代码
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)
        {
            I1 i = new Person();
            i.Test();
            Person person = new Person();
            person.Test();
            Console.ReadKey();
        }
    }
    class Person:I1
    {
        public void Test()
        {
            Console.WriteLine("这个Test函数是属于Person的");
        }

        //显示实现接口:告诉编译器 这个函数才是接口的  不是类的
        void I1.Test()
        {
            Console.WriteLine("显示实现接口的Test函数");
        }
    }

    interface I1
    {
        void Test();
    }
}

14抽象类实现接口

点击查看代码
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)
        {
            I1 i = new PersonDaughter();//new PersonSon(); //new Person();
            i.Test();
            Console.ReadKey();
        }
    }

    abstract class Person:I1
    {
        public void Test()
        {
            Console.WriteLine("抽象类实现接口");
        }
    }

    class PersonSon : Person
    { }

    class PersonDaughter : Person
    { }


    interface I1
    {
        void Test();
    }
}

15接口登记案例

点击查看代码
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)
        {
            //Person需要登记
            //House房子也需要登记
            //汽车也需要登记
            //财产登记

            //最后写一个函数 来实现以上物品的登记

            DengJi(new Person());
            DengJi(new House());
            DengJi(new Car());
            DengJi(new Money());
            Console.ReadKey();
        }

        static void DengJi(IDengJi dj)
        {
            dj.DengJi();
        }

    }

    interface IDengJi
    {
        void DengJi();
    }
    class Person : IDengJi
    {
        public void DengJi()
        {
            Console.WriteLine("人出生就要登记");
        }
    }

    class House : IDengJi
    {
        public void DengJi()
        {
            Console.WriteLine("买房子也要登记");
        }
    }


    class Car : IDengJi
    {
        public void DengJi()
        {
            Console.WriteLine("买汽车也要登记");
        }
    }

    class Money : IDengJi
    {
        public void DengJi()
        {
            Console.WriteLine("财产也要登记");
        }
    }
}

posted @   快考试了吧  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示