C003C#继承多态

c3661.什么是继承?代码举例说明
比如:汽车继承车类拥有车会跑的方法还有一些属性

public     class Employee
    {
    public string ID { get; set; }
    public string name { get; set; }
    public int Age { get; set; }
    public Employee() { }
    public Employee(string ID, string name, int Age) {
        this.ID = ID;
        this.name = name;
        this.Age = Age;
    }
    }
 public    class SE:Employee
    {
     public int AirValue { get; set; }
     public SE(){}
     public SE(string ID, string name, int Age,int AirValue):base(ID,name,Age) {
        
         this.AirValue = AirValue;

     }

    }

2什么是多态?代码举例说明
多态指多种形态,同一行为作用于不同的对象有不同的操作。

public     class Employee
    {
    public string ID { get; set; }
    public string name { get; set; }
    public int Age { get; set; }
    public Employee() { }
    public Employee(string ID, string name, int Age) {
        this.ID = ID;
        this.name = name;
        this.Age = Age;
    }
    public virtual void Show(){

        Console.WriteLine("大家好,我是员工"+this.name);
    }
    }
public    class SE:Employee
    {
     public int AirValue { get; set; }
     public SE(){}
     public SE(string ID, string name, int Age,int AirValue):base(ID,name,Age) {
         this.ID = ID;
         this.name = name;
         this.Age = Age;
         this.AirValue = AirValue;

     }
     public override void Show()
     {

         Console.WriteLine("大家好我是程序员"+this.name);
     }
    }

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

public abstract  class PM
    {
      public static int num;
      public abstract void Add();
      public string Show();

    }

4:抽象类和接口的相同点和不同点?

抽象类 接口
要被类继承 要被类重写
单继承 多继承
抽象类可以包含实例方法 接口不可以包含实例方法
抽象类可以包含普通的属性 接口不可以包含实例方法
是对类的 抽象 是对行为的 抽象

相同点:都可以写抽象方法,规定了子类要重写方法
5:抽象方法和虚方法的不同点和相同点?
不同点

虚方法 抽象方法
用virtual修饰 用abstract 修饰
要有方法体 没有方法体
可以被子类override 必须被子类override
除了密封类外都可以定义 只能在抽象类中定义

相同:都在继承的继承上实现的多态
6:定义抽象类和抽象方法的关键字?
abstract
7.书本上XML那一章中有哪些方法?代码一一举例

 public static    class Add                                                           //读XML文件
    {
         public static void Reads(string path) {
             XmlDocument xml = new XmlDocument();
             xml.Load(path);
             XmlNode node = xml.DocumentElement;
             foreach (XmlNode item in node.ChildNodes)
             {
                 switch (item.Name)
                 {
                     case "ID":
                         Console.WriteLine("ID" + item.InnerText);
                         break;
                     case "Name":
                         Console.WriteLine("Name" + item.InnerText);
                         break;
                     default:
                         break;
                 }
             }
         }
    }
  public static void     Write(string path){                          
         XmlDocument doc = new XmlDocument();
         XmlDeclaration declar = doc.CreateXmlDeclaration("1.0","utf-8",null);
         XmlElement xml = doc.CreateElement("Type");
         xml.AppendChild(xml);
         xml.SetAttributeNode("Name","张三");
         doc.Save(path);
     }

8.书本上文件那一章中有哪些方法?代码一一举例

1.文件

public static void FiltW(string path) {                                                    //写文件
         FileStream myfs = new FileStream(path,FileMode.Create);
         StreamWriter mysw = new StreamWriter(myfs);
         string name = "HelloWolrd";
         mysw.Write(name);
         mysw.Close();
         myfs.Close();
     
     }
  public static void FiltR(string path)                                                               //读文件
     {
         FileStream myfs = new FileStream(path, FileMode.Create);
         StreamReader reat = new StreamReader(myfs);

         string name = reat.ReadToEnd();
         Console.WriteLine(name);
         reat.Close();
         myfs.Close();

     }
public static    class Files
    {
     public static bool Exist(string path) {              //判断文件是否存在
         if (!File.Exists(path))
         {
             Console.WriteLine("文件不存在");
             return false;
         }
         else {
             Console.WriteLine("文件存在");
             return true;
         }
     
     
     }


     public static void Copy(string path,string newpath)               //把文件内容复制到另一个地方
     {
        if( Exist( path)){
            File.Copy(path, newpath);
            Console.WriteLine("复制成功");
        }
     
     
     }

     public static void Move(string path,string newpath ) {                 //把文件移到另一个地址
             if (Exist(path)) {
             File.Move(path, newpath);
             Console.WriteLine("文件移动成功");
            
         
         
         }
     
     }

     public static void Delete(string path)                                      //删除文件
     {                 //把文件移到另一个地址
         if (Exist(path))
         {
             File.Delete(path);
             Console.WriteLine("文件删除成功");



         }

     }

    }

2.文件夹

 public static   class Directory
    {
     public static bool Exists(string path) {                           //判断文件夹是否存在
         if (!Directory.Exists(path))
         {
             Console.WriteLine("文件夹不存在");
             return false;
         }
         else {
             Console.WriteLine("文件夹存在");
             return true;
         }
     
     }


     public static void Move(string path, string newpath)              //判断文件夹是否存在
     {                 
        if(Exists( path)){
            Directory.Move(path, newpath);
            Console.WriteLine("文件夹移入成功");
        
        }
     
     
     }




     public static void Delete(string path)              //删除指定文件夹
     {
         if (Exists(path))
         {
             Directory.Delete(path);
             Console.WriteLine("文件夹删除成功");

         }


     }
    }

   public void FileINfo(){                                                                  //   复制文件
       FileInfo fi=new FileInfo(@"D:\temp\FileInfo.txt");
       Console.WriteLine("文件是否存在"+fi.Exists);
        Console.WriteLine("文件名"+fi.Name);
        Console.WriteLine("文件是否存在"+fi.Directory.Name);
        fi.Copy.To("E:\temp\FileInfo.txt");

}

public void Delete(){                                  //删除
	    FileInfo fi=new FileInfo(@"D:\temp\FileInfo.txt");
		if(fi.Exists){
			fi.Delete();
			    Console.WriteLine("删除成功");
}

}
public void Delete(){                                  //删除
	    FileInfo fi=new FileInfo(@"D:\temp\FileInfo.txt");
		if(fi.Exists){
			fi.MoveTo("D:\test.txt");
}
}
DirectoryInfo di=new DirectoryInfo(@"D:\test");
DirectoryInfo[]subDir=di.GetDirectories();                                   //返回当前目录的子目录
FileInfo[]fi=di.GetFiles();                                                                          //返回当前目录的文件列表

你可能感兴趣的:(C003C#继承多态)