What are interfaces (entry) and interfaces (entry)
At school, teachers at school will always use some simple examples to explain what interfaces are)
These examples may look like this.
/// <Summary> /// an interface indicating animals /// </summary> public interface IAnimal {// <summary> // All animals must eat /// </summary> void Eat ();}
And then just take a small animal like this.
Public class Cat: IAnimal {public void Eat () {Console. WriteLine ("I'm a kitten, I want to Eat fish ")}}
Then the explanation of the interface is basically over. With some homework and exams, we only know that the interface is such a thing that does not implement any method and only defines the content of the method.
What is the specific purpose? Maybe we just think that when some objects can form tree trees like animals, we will place an interface at the root of the tree.
Then
The function of an interface is not embodied in small animals, vehicles, or human object trees.
First, let's talk about a simple interface design scenario: functional interfaces
Many objects are involved in our program, but these objects all share a common feature-sales, having a price, whether to participate in discount activities, and discount prices.
Then, we can design an interface for this "function ".
/// <Summary> /// indicates a function interface that can be used for sale. /// </summary> public interface ISaleable {/// <summary> /// price // /</summary> int Price {get ;} /// <summary> /// whether the discount is available /// </summary> bool CanDiscount {get ;} /// <summary> /// discounted price /// </summary> int DiscountedPrice {get ;}}
With such an interface, we can use this "interface" method.
For example, if we are a salesman, we should have at least one method, called salesman, except for the attributes such as names and employee numbers. Let's look at the code.
Public class Seller {void coupon (ISaleable good) {if (good. CanDiscount) {Console. Write ("Hello, you can enjoy discounts for your purchased products. The discount price is {0} ", good. discountedPrice);} else {Console. writeLine ("hello, the product you purchased did not take any discount, you can only purchase according to the original price {0}", good. price )}}}
Therefore, we have a functional interface that can use the type of this function. Now, let's create a few types as if we were in college.
Public class Car: ISaleable {public int Price {get {return 100000 ;}} public bool CanDiscount {get {return false ;}} /// <summary> /// the type cannot be discounted, therefore, the discount price is optional. // </summary> public int DiscountedPrice {get {throw new NotImplementedException () ;}} public class Compute: ISaleable {public int Price {get {return 5000 ;}} public bool CanDiscount {get {return true ;}} public int DiscountedPrice {get {return 5000*0.8 ;}}}
In this way, the salesman can sell these two items.
In the next article "what is an interface (Getting Started) -- enrich your program functions", you can also learn more interface usage.