C#003继承多态

01:什么是继承?代码举例说明

public class Person
    {
        public int Age { get; set; }

        public string Name { get; set; }
    }


public class Student:Person
    {
        public void SayHi()
        {
            Console.WriteLine(this.Age+this.Name);
        }
    }

02:什么是多态?代码举例说明

public class Teacher:Person
    {
        public override void SayHi()
        {
            Console.WriteLine("我是老师" + this.Age + this.Name);
        }
    }

public class Student:Person
    {
        public override void SayHi()
        {
            Console.WriteLine("我是学生"+this.Age+this.Name);
        }
    }

public class Person
    {
        public int Age { get; set; }

        public string Name { get; set; }

        public virtual void SayHi()
        {
            Console.WriteLine("你好~");
        }
    }

03:什么是抽象类?代码举例说明

public abstract class Car
    {
        public abstract void Run();
    }

04:抽象类和接口的相同点和不同点?
相同点:
都不能实例化
都可以被继承
可以包含属性和方法

不同点:
接口可以多实现,而抽象方法只能单继承

05:抽象方法和虚方法的不同点和相同点?
相同点:
都可以实现多态
都可以被子类重写

不同点:
抽象方法用abstract修饰,虚方法用virtual修饰
抽象方法必须被子类重写,除非子类也是个抽象类
抽象方法只能抽象类当中定义

06:定义抽象类和抽象方法的关键字?
  使用abstract关键字

07:书本上XML那一章中有哪些方法?代码一一举例
Load()----读取整个XML结构

static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("test.xml");
        }

08:书本上文件那一章中有哪些方法?代码一一举例
File类常用方法:
Exists(string path)  用于检查文件是否存在
Copy(string oldPath,string newPath) 将文件拷贝到指定路径
Move(string oldPath,string newPath) 将文件移动到指定路径
Delete(string path) 删除指定路径文件

Directory类常用方法:
Exists(string path)  用于检查指定文件夹在磁盘中是否存在
Move(string oldPath,string newPath) 将文件夹或目录移动到指定路径
Delete(string path) 删除指定目录

static void Main(string[] args)
        {
            string path = "B:\\ceshi.txt";
            string newPath = "D:\\ceshi.txt";
            CopyFile(path,newPath);
            DelFile(newPath);
            MoveFile(path, newPath);
            Console.ReadKey();
        }


        public static void CopyFile(string oldPath,string newPath)
        {
            try
            {
                if (!File.Exists(oldPath))
                {
                    Console.WriteLine("文件不存在!");
                    return;
                }
                File.Copy(oldPath,newPath);
                Console.WriteLine("复制成功!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("复制出现异常"+ex.Message);
                throw;
            }

        }

        public static void DelFile(string path)
        {
            try
            {
                if (!File.Exists(path))
                {
                    Console.WriteLine("文件不存在!");
                    return;
                }
                File.Delete(path);
                Console.WriteLine("删除成功!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("删除出现异常"+ex.Message);
                throw;
            }
        }

        public static void MoveFile(string oldPath, string newPath)
        {
            try
            {
                if (!File.Exists(oldPath))
                {
                    Console.WriteLine("文件不存在!");
                    return;
                }
                File.Move(oldPath, newPath);
                Console.WriteLine("移动成功!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("移动出现异常"+ex.Message);
                throw;
            }
        }

static void Main(string[] args)
        {
            string path = "B:\\";
            DirectoryInfo[] dir = FindD(path);
            Console.WriteLine("B盘目录:");
            foreach (DirectoryInfo info in dir)
            {
                Console.WriteLine(info.FullName);
            }
            Console.ReadKey();
        }


        public static DirectoryInfo[] FindD(string path)
        {
            DirectoryInfo info = new DirectoryInfo(path);
            DirectoryInfo[] dir = info.GetDirectories();
            return dir;
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值