In actual projects, when we design a parent class, we often encounter that this class cannot determine its specific execution process. For example, I designed 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 {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) {// I do not know how to write it, because it may be a file or a folder, decompress the file if it is compressed} public Enum filetype {isunknown = 0, // type unknown isfile = 1, // file isdirectory = 2, // folder iscompression // compressed}
This is a parent class. How should I write the copy method? Because the file may exist in four states and may be added later as needed, the copying method varies with different file types, and some special processing may be performed on a certain file according to the project requirements. In this way, you cannot write code to the copy method when designing this parent class. You only need to rewrite this method if you inherit it, and write different Execution Code as needed.
In this way, a class has a certain method, but this method does not have a specific execution process. Such a method is called an abstract method ".
In the above afile class, the copy method is called an abstract method, but there is a problem. If the afile class is instantiated, the copy method is the behavior of this object, but the copy method is not sure. This is not in line with the laws of objective things. Therefore, this class cannot be instantiated. That is to say, when there are abstract methods in the class, this class cannot be instantiated. Such classes are called "abstract classes ". Abstract cannot be instantiated, but it is still a class. Abstract classes and abstract methods are modified with abstract keywords.
As you can see, abstract classes have two methods: abstract methods and non-abstract methods.
Non-Abstract METHODS: abstract classes are inherited. subclasses have non-abstract methods. They can be directly used or overwritten.
Abstract class, must overwrite.
Modify the preceding 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 {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, // type unknown isfile = 1, // file isdirectory = 2, // folder iscompression // compressed} // <summary> // File class /// </Summary> public class fileinfo: afile {public fileinfo (string name, string path, filetype type): Base (name, Path, type) {}/// <summary> // file copy method /// </Summary> Public override void copy (string destpath) {If (string. isnullorempty (destpath) {string sourcepath = This. afilepath + this. name; // at this time, the name is the file name with a suffix, which adds up to the file path destpath + = This. name; If (file. exists (sourcepath) {file. copy (sourcepath, destpath, true) ;}}/// <summary> // folder class /// </Summary> public class filedirectoryinfo: afile {Pu BLIC filedirectoryinfo (string name, string path, filetype type): Base (name, path, type) {}/// <summary> /// file copy method /// </Summary> Public override void copy (string destpath) {If (string. isnullorempty (destpath) {string sourcepath = This. afilepath + this. name; // at this time, the file name is the folder name, and the total file name is the folder path destpath + = This. name; If (directory. exists (sourcepath) {copydirectory (sourcepath, destpath) ;}}/// <summa Ry> // Method for copying folders /// </Summary> private void copydirectory (string sourcepath, string destpath) {try {If (! Directory. exists (destpath) {directory. createdirectory (destpath);} 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 abstract class is inherited and implemented. However, if the subclass inherits the abstract class but does not implement the abstract method, this subclass will also exist as an abstract class. Classes with abstract methods are called abstract classes. In some cases, classes without abstract methods can also be defined as abstract keywords, which indicates that the class cannot be abstract and must be inherited.