When should I use interfaces and abstract classes ?, Interface abstract class

Source: Internet
Author: User

When should I use interfaces and abstract classes ?, Interface abstract class

Take database operations as an example:
The Insert Update Select statement must be used for database operations. Therefore, the Insert Update Select statement is used as an interface.

However, the content of each function operation is different. Therefore, make an abstract class to inherit the interface and then implement the specific methods of the abstract class in the abstract class derived class.

Object-oriented means to understand some code as entities. an object has its own attributes and behaviors. for example, if you perform this operation on the database, you need to regard the database as an object and its behavior can be addition, deletion, modification, and query. therefore, you should declare a class to perform these operations.
Of course, considering the characteristics of various businesses, you can also abstract these classes and declare an interface class as a database interface class. Its implementation class is the specific operation of various businesses ..

1. An interface is a set of rules. It specifies a set of rules that must be owned by the class or interface that implements this interface.
2. the difference between an abstract class and an interface lies in the motivation for use. Abstract classes are used for code reuse, and interfaces are used for polymorphism.

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

 1 public class NormalPlayer  2 {  3   public int ID  4   {  5     get;  6     set;  7   }  8   public string FirstName  9   { 10     get; 11     set; 12   } 13   public string LastName 14   { 15     get; 16     set; 17   } 18   public decimal WeekSalary 19   { 20     get; 21     set; 22   } 23   public string GetFullName() 24   { 25     return this.FirstName + " " + this.LastName; 26   } 27   public decimal GetDaySalary() 28   { 29     return WeekSalary / 7; 30   } 31 } 32 public class SubPlayer 33 { 34   public int ID 35   { 36     get; 37     set; 38   } 39   public string FirstName 40   { 41     get; 42     set; 43   } 44   public string LastName 45   { 46     get; 47     set; 48   } 49   public decimal MonthSalary 50   { 51     get; 52     set; 53   } 54   public string GetFullName() 55   { 56     return this.FirstName + " " + this.LastName; 57   } 58   public decimal GetWeekSalary() 59   { 60     return MonthSalary / 4; 61   } 62 } 

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.

 1 public class BasePlayer  2 {  3   public int ID  4   {  5     get;  6     set;  7   }  8   public string FirstName  9   { 10     get; 11     set; 12   } 13   public string LastName 14   { 15     get; 16     set; 17   } 18 19   public string GetFullName() 20   { 21     return this.FirstName + " " + this.LastName; 22   } 23 } 

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

 1 public class NormalPlayer: BasePlayer  2 {  3   public decimal WeekSalary  4   {  5     get;  6     set;  7   }  8   public decimal GetDaySalary()  9   { 10     return WeekSalary / 7; 11   } 12 } 13 public class SubPlayer : BasePlayer 14 { 15   public decimal MonthSalary 16   { 17     get; 18     set; 19   } 20   public decimal GetWeekSalary() 21   { 22     return MonthSalary / 4; 23   } 24 } 

Next, 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.
View Source code printing help

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.

Understanding:

1. A non-abstract method can exist in an abstract class, but an interface can only have the existence of an abstract method to declare a method without implementing the class called abstract class ), it is used to create a class that reflects some basic behaviors and declare methods for this class, but it cannot implement this class in this class ....

2. An abstract class cannot be a new object directly. It can only be inherited and can be inherited in a single way. That is to say, it abstracts public things. sub-classes can call the methods of parent classes, or expand their own functions, just like parent businesses. The interface can be inherited multiple times, which is the biggest difference from the abstract class. That is to say, an interface can be used by anyone who wants to use it as a public product. Instead of an abstract class, it cannot be called without its subclass. Hope to help you

3. Use an interface when describing a set of methods to use abstract classes when describing a virtual object

 

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.