Basic knowledge of C #: Basics (6) Abstract classes and abstract methods

Source: Internet
Author: User
Tags file copy
In real-world projects, when we design a parent class, we often encounter this class that cannot determine its specific execution flow. 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)        {            //Do not know how to write, because the file may or may not be a folder, if it is compressed also extract        }    } public    enum FileType    {        Isunknown = 0,//type unknown        isfile = 1,//file        isdirectory =2,//folder        iscompression//compressed    }

This is a parent class, its copy method, how should it be written? Because the file exists in four states or even later, as needed, the copy method is not the same for different file types, and it is possible to do some special processing for a file depending on the project's needs. When you design this parent class, you cannot write code to the Copy method, just the person who inherits it, and writes different execution code as needed.
this way, a class has a method, but the method does not have a specific execution procedure, which is called an "abstract method."
The Copy method in the Afile class above is called an abstract method, but with a problem, if the Afile class is instantiated, the copy method is the behavior of the object, but the copy method is not yet known. This does not conform to the law of objective things. Therefore, this class cannot be instantiated, that is, when there are abstract methods in the class, the class cannot be instantiated, and such classes are called "abstract classes". An abstraction cannot be instantiated, but it is also a class. Abstract classes and abstract methods are decorated with the abstract keyword. The
can see that there are two methods in an abstract class: abstract and non-abstract methods.
Non-abstract methods, abstract classes are inherited, subclasses have non-abstract methods, can be used directly, you can override overrides.
Abstract class, overriding overrides must be overridden.
Modify the above file classes:

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,//type unknown isfile = 1,//file isdirectory =2,//folder IsCom pression//Compressed}//<summary>///File class    </summary> public class Fileinfo:afile {public FileInfo (string name, string path, FileType t ype): Copy method for base (name, path, type) {}///<summary>////&LT;/S ummary> public override void Copy (string destpath) {if (string. IsNullOrEmpty (DestPath)) {string SourcePath = this. Afilepath + this.                Name; At this point, name is the file name, with the suffix name, plus the path destpath + = this.                Name;                if (file.exists (SourcePath)) {file.copy (SourcePath, DestPath, true); }}}}///<summary> folder class///</summary> public class Filedirectoryinfo        : afile {public 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; The file name is the folder name, plus the folder path DestPath + = this.                Name;                if (directory.exists (SourcePath)) {copydirectory (SourcePath, DestPath); }}}///<summary>///Copy Folder method///</summary> private void Co Pydirectory (String SourcePath, String destpath) {try {if (!                Directory.Exists (DestPath)) {directory.createdirectory (destpath);                } DirectoryInfo DirectoryInfo = new DirectoryInfo (SourcePath); foreach (FileSystemInfo fileInfo in Directoryinfo.getfilesysteminfos ()) {string subfile    Ordirectoryname = 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{}}}

This completes the inheritance of the abstract class and implements it. But if a subclass inherits an abstract class, but does not implement an abstract method, the subclass will also exist as an abstract class. Classes with abstract methods are called abstract classes, and for some cases, classes that do not have abstract methods can also be defined as abstract classes using the abstract keyword, which indicates that the class cannot be abstracted and must be inherited.

The above is the basic knowledge of C #: Basic knowledge (6) The content of abstract classes and abstract methods, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.