C # 8.0 interface to implement the default characteristics [Translation] C # 8.0 in a more flexible interface to the new features - the default interface

 

Article: [translated] C # 8.0 in a more flexible interface to the new features - the default interface

Original Sample code:

public interface IBook  
{  
    void AddBook(string bookName, string autherName);  
    void removeBook(string bookName);  
  
    void rateBook(int bookID)  
    {                          
        //default logic here  
        Console.WriteLine("\nExecuted the Default implementation in the interface");  
    }  
}  

 

java8 also provides a default implementation of the interface;

Reference: https: //baijiahao.baidu.com/s id = 1609018150369824405 & wfr = spider & for = pc?

Title: Interface's default method Java8

In Java8, the method comprising allowing the interface with the particular implementation, use default modification, such a method is the default method. E.g:

 

 The default method and inherit the parent class 3. interface conflict, then this time will choose the method in the parent class instead of the default interface methods. This is also called a class priority principle, it can ensure compatibility with the Java7. In other words, a default method implemented in the interface, it will not affect the writing of the code before Java8. So, we can not be defined in an interface toString () and equals () such an interface, because, according to the principle of priority class, Object of these methods will be retained.

 

Guess you like

Origin www.cnblogs.com/Tpf386/p/12038210.html