Why are abstract classes used, when are they used, and when?

Source: Internet
Author: User
Tags define abstract

Why are abstract classes used, when are they used, and when?

Assume there are two categories: one is the main player and the other is the substitute player.

    public class NormalPlayer
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public decimal WeekSalary { get; set; }
        public string GetFullName()
        {
            return this.FirstName + " " + this.LastName;
        }
        public decimal GetDaySalary()
        {
            return WeekSalary/7;
        }
    }
    public class SubPlayer
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public decimal MonthSalary { get; set; }
        public string GetFullName()
        {
            return this.FirstName + " " + this.LastName;
        }
        public decimal GetWeekSalary()
        {
            return MonthSalary/4;
        }
    }

 

We found that NormalPlayer and SubPlayer share the same attributes and methods. Of course, they also have different attributes and methods. Abstract The common parts of the two classes into a base class.

    public class BasePlayer
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        
        public string GetFullName()
        {
            return this.FirstName + " " + this.LastName;
        }
    }

 

Then let the previous two classes derive from this base class.

    public class NormalPlayer: BasePlayer
    {
        public decimal WeekSalary { get; set; }
        public decimal GetDaySalary()
        {
            return WeekSalary/7;
        }
    }
    public class SubPlayer : BasePlayer
    {
        public decimal MonthSalary { get; set; }
        public decimal GetWeekSalary()
        {
            return MonthSalary/4;
        }
    }

 

Then, we found that the daily and weekly salary calculation methods of NormalPlayer and SubPlayer can be abstracted and put into the base class as virtual methods.

    public class BasePlayer
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        
        public string GetFullName()
        {
            return this.FirstName + " " + this.LastName;
        }
        public virtual decimal GetSalary()
        {
            throw new NotImplementedException();
        }
    }

 

In the two Derived classes NormalPlayer and SubPlayer, you must override the virtual method of the base class.

    public class NormalPlayer: BasePlayer
    {
        public decimal WeekSalary { get; set; }
// Get the daily salary
        public override decimal GetSalary()
        {
            return WeekSalary / 7;
        }
    }
    public class SubPlayer : BasePlayer
    {
        public decimal MonthSalary { get; set; }
// Obtain the weekly salary
        public override decimal GetSalary()
        {
            return MonthSalary / 4;
        }
    }

 

However, in actual situations, BasePlayer is just an abstract class and we do not want to instantiate this class. At this time, we can design BasePlayer as abstract class. In addition, the abstract class provides an abstract Method for Calculating salaries. Once an abstract method without a method body is declared in the base class, all classes derived from this abstract class must implement or override the abstract methods in the base class.

    public abstract class BasePlayer
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        
        public string GetFullName()
        {
            return this.FirstName + " " + this.LastName;
        }
        public abstract decimal GetSalary();
    }

 

It can be seen that when two or more classes have duplicates, We can abstract a base class. If we want this base class to not be instantiated, you can design this base class as an abstract class.

 

 


Why abstract classes? How to declare an abstract class in c?

Use the keyword abstract in C # To define abstract classes and abstract methods.
Classes that cannot be initialized are called abstract classes. They only provide partial implementation, but the other class can inherit it and create them.
.

"An abstract class that contains one or more pure virtual functions cannot be instantiated.
An abstract class can only be used by interfaces and as the base classes of other classes. "-C ++
Programming Language by Stroustrup Chapter13.2

Abstract classes can be used for classes, methods, attributes, indexers, and events.
In a class declaration, this class tends to be used as the base class of other classes.
A member is marked as abstract or included in an abstract class, which must be implemented by its derived class.

What is the role of an abstract class in C? When is an abstract class used?

Abstract class:

Abstract classes are special classes, but they cannot be instantiated. In addition, they have other characteristics of the class. It is important that abstract classes can include abstract methods, which are not supported by common classes. Abstract METHODS can only be declared in abstract classes and do not contain any implementations. The Derived classes must overwrite them. In addition, an abstract class can be derived from an abstract class. It can overwrite the abstract methods of the base class or overwrite them. If not, its derived class must overwrite them.

Abstract classes can be used to design large module units.
Reference: blog.sina.com.cn/s/blog_4aaeba510100c4y6.html

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.