


C# basic knowledge compilation: basic knowledge (6) abstract classes and abstract methods
In actual projects, when we design a parent class, we often encounter that the class cannot determine its specific execution process. For example, I design a file class:
public class AFile { private string name = string.Empty; private string path = string.Empty; private FileType type = FileType.IsUnknown; public string Name { get { return name; } } public string Path { get { return path; } } public FileType Type { get { return type; } } public AFile(string name, string path, FileType type) { this.name = name; this.path = path; this.type = type; } public void Copy(string destPath) { //不知道怎么写,因为可能是文件还可能是文件夹,如果是压缩的还要解压 } } public enum FileType { IsUnknown = 0,//类型不明 IsFile = 1,//文件 IsDirectory =2,//文件夹 IsCompression//压缩的 }
This is a parent class. How should I write its copy method? Because files exist in four states and may be added later as needed, the copying methods are different for different file types, and some special processing may be done for certain files according to project needs. In this way, when redesigning this parent class, you cannot write code for the copy method. Only whoever inherits it can override this method and write different execution codes as needed.
In this way, a class has a certain method, but the method has no specific execution process. Such a method is called an "abstract method".
The Copy method in the above AFile class is called an abstract method, but there is a problem. If the AFile class is instantiated, the Copy method will be the behavior of this object, but in fact the Copy method is not sure yet. This is inconsistent with the objective laws of things. Therefore, this class cannot be instantiated, which means that when there is an abstract method in the class, this class cannot be instantiated. Such a class is called an "abstract class". Abstract cannot be instantiated, but it is still a class. Abstract classes and abstract methods are modified with the abstract keyword.
As you can see, there are two methods in abstract classes: abstract methods and non-abstract methods.
Non-abstract methods, abstract classes are inherited, subclasses have non-abstract methods, which can be used directly or overridden.
Abstract class must be overridden and rewritten.
Modify the above file class:
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace YYS.CSharpStudy.MainConsole { public abstract class AFile { private string name = string.Empty; private string path = string.Empty; private FileType type = FileType.IsUnknown; public string Name { get { return name; } } public string AFilePath { get { return path; } } public FileType Type { get { return type; } } public AFile(string name, string path, FileType type) { this.name = name; this.path = path; this.type = type; } public abstract void Copy(string destPath); } public enum FileType { IsUnknown = 0,//类型不明 IsFile = 1,//文件 IsDirectory =2,//文件夹 IsCompression//压缩的 } /// <summary> /// 文件类 /// </summary> public class FileInfo : AFile { public FileInfo(string name, string path, FileType type) : base(name, path, type) { } /// <summary> /// 文件的拷贝方法 /// </summary> public override void Copy(string destPath) { if (string.IsNullOrEmpty(destPath)) { string sourcePath = this.AFilePath + this.Name; //此时name是文件名,带有后缀名,加起来是文件路径 destPath += this.Name; if (File.Exists(sourcePath)) { File.Copy(sourcePath, destPath, true); } } } } /// <summary> /// 文件夹类 /// </summary> public class FileDirectoryInfo : AFile { public FileDirectoryInfo(string name, string path, FileType type) : base(name, path, type) { } /// <summary> /// 文件的拷贝方法 /// </summary> public override void Copy(string destPath) { if (string.IsNullOrEmpty(destPath)) { string sourcePath = this.AFilePath + this.Name; //此时文件名是文件夹名,加起来是文件夹路径 destPath += this.Name; if (Directory.Exists(sourcePath)) { CopyDirectory(sourcePath, destPath); } } } /// <summary> /// 拷贝文件夹的方法 /// </summary> private void CopyDirectory(string sourcePath, string destPath) { try { if (!Directory.Exists(destPath)) { Directory.CreateDirectory(destPath); } DirectoryInfo directoryInfo = new DirectoryInfo(sourcePath); foreach (FileSystemInfo fileInfo in directoryInfo.GetFileSystemInfos()) { string subFileOrDirectoryName = Path.Combine(destPath, fileInfo.Name); if (fileInfo is DirectoryInfo) { this.CopyDirectory(fileInfo.FullName, subFileOrDirectoryName); } else { if (File.Exists(sourcePath)) { File.Copy(sourcePath, destPath, true); } } } } catch{} } } }
In this way, the inheritance and implementation of the abstract class are completed. But if the subclass inherits the abstract class but does not implement the abstract method, then the subclass will also exist as an abstract class. A class with abstract methods is called an abstract class. In some cases, a class without abstract methods can also be defined as an abstract class using the abstract keyword, which indicates that the class cannot be abstracted and must be inherited.
The above is the compilation of C# basic knowledge: Basic knowledge (6) Abstract classes and abstract methods. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.
