Home  >  Article  >  Backend Development  >  C# basic knowledge compilation: basic knowledge (6) abstract classes and abstract methods

C# basic knowledge compilation: basic knowledge (6) abstract classes and abstract methods

黄舟
黄舟Original
2017-02-11 13:14:301272browse

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 (m.sbmmt.com)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn