1:什么是继承?代码举例说明
继承:子类继承父类,继承而得到的类称为派生类,被继承的类叫做基类
public class Humanity //基类
{
private string id;//身份证
private string name; //姓名
private string sex; //性别
private int age; //年龄
public Humanity(string id,string name,string sex,int age)
{
this.Id = id;
this.Name = name;
this.Sex = sex;
this.Age = age;
}
public string Id { get => id; set => id = value; }
public string Name { get => name; set => name = value; }
public string Sex { get => sex; set => sex = value; }
public int Age { get => age; set => age = value; }
}
public class Student:Humanity //派生类
{
//特有的属性
private string education;
public string Education { get => education; set => education = value; }
public Student(string id, string name, string sex, int age,string education) : base(id,name,sex,age)
{
this.Education = education;
}
}
2:什么是多态?代码举例说明
多态:指同一操作作用于不同对象时,可以有不同的解释,产生不同的结果
可以使用虚方法实现多态
public class Humanity //基类
{
public virtual void show()
{
Console.WriteLine($"身份证是{Id},姓名{Name},性别{Sex},年龄{Age}");
}
}
public class Student:Humanity //派生类
{
public override void show()
{
Console.WriteLine($"身份证是{Id},姓名{Name},性别{Sex},年龄{Age},学历{Education}");
}
}
3:什么是抽象类?代码举例说明
抽象类是用abstract关键字来定义,还要注意的是含有抽象方法的类一定是抽象类
public abstract class Humanity //抽象类
{
}
4:抽象类和接口的相同点和不同点?
相同点:抽象类和接口都不能实例化对象,继承抽象类和实现接口都要对其中的抽象方法全部实现
不相同点:抽象类中可以定义构造方法,可以有抽象方法和具体方法,接口不能定义构造方法,也没有具体方法
5:抽象方法和虚方法的不同点和相同点?
相同点:虚方法定义关键字是:virtual。抽象类定义关键词是:abstract
不相同点:在基类中,虚方法有方法体,抽象方法没有方法体
6:定义抽象类和抽象方法的关键字?
关键字:abstract
7:书本上XML那一章中有哪些方法?代码一一举例
Load()//读取xml方法
/// <summary>
/// 读取xml文档
/// </summary>
/// <param name="path">文件路径</param>
public void Read(string path)
{
XmlDocument xml = new XmlDocument();
xml.Load(path);
XmlNode xmlNode = xml.DocumentElement;
foreach (XmlNode node in xmlNode.ChildNodes)//循环输出
{
}
}
8:书本上文件那一章中有哪些方法?代码一一举例
Copy():复制
/// <summary>
/// 复制
/// </summary>
/// <param name="sourceFilePath">源文件路径</param>
/// <param name="pathTarget">目标路径</param>
public void Copy(string sourceFilePath, string pathTarget)
{
File.Copy(sourceFilePath, pathTarget);
}
Exists():检查文件是否存在
/// <summary>
/// 检查文件是否存在
/// </summary>
/// <param name="path">文件路径</param>
public void isExists(string path)
{
if (File.Exists(path))
{
Console.WriteLine("该文件存在");
}
}
Move():移动
/// <summary>
/// 移动
/// </summary>
/// <param name="sourceFilePath"></param>
/// <param name="pathTarget"></param>
public void Move(string sourceFilePath, string pathTarget)
{
File.Move(sourceFilePath, pathTarget);
}
Delete():删除
/// <summary>
/// 删除
/// </summary>
/// <param name="path">文件路径</param>
public void Del(string path)
{
File.Delete(path);
}
DirectoryInfo类两个重要的方法:GetDirectories()和GetFiles()
/// <summary>
/// 获取目录下的所有文件
/// </summary>
/// <param name="path">文件路径</param>
public void GetDirectory(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
DirectoryInfo[] directories = dir.GetDirectories(); //:返回数组目录
FileInfo[] files = dir.GetFiles();//返回数组文件
}