当前位置:网站首页>Interface oriented programming

Interface oriented programming

2022-07-07 16:41:00 Mori Ohashi

  • Pre knowledge : What is an interface
    • Interface ( english :interface), yes Java An abstract type in programming pre research , Is a collection of abstract methods , A class inherits an interface by way of , So as to realize the abstract method of inherited interface .
    • The difference between interfaces and ordinary classes
      • Class describes the properties and methods of an object , Interface defines the methods to be implemented by the class
      • Interface monthly construction method , Cannot be instantiated , But it can be realized , A class that implements an interface , All methods described in the interface must be implemented , Otherwise, it must be declared as an abstract class .
      • There can be variables in the interface , But the variables in the interface will be specified as public static final Variable
      • Interface supports multiple inheritance
      • The interface is implicitly abstract , Every method in the interface is implicitly abstract , The method in the interface is implicitly specified as public abstract
    • The difference between an interface and an abstract class
      • Abstract classes can provide partial implementations of certain methods
      • Member variables in abstract classes can be of various types , The member variables in an interface can only be public static final type
      • Interfaces cannot contain static code blocks and static methods , And abstract classes can .JDK1.8 after , Interfaces can have static methods and method bodies
      • A class can only inherit one abstract class , But you can implement multiple interfaces
      • Abstract classes are designed to extract some common features or default behaviors of a certain kind , To achieve the purpose of code reuse . The interface is designed to specify a certain standard , It emphasizes norms , The use of polymorphism is embodied in object-oriented language .
      • Branch theme
    • JDK8 -- Interface default method
      • Definition
        • Interfaces can have implementations , And you don't need an implementation class to implement its methods , When defining interfaces , Just prefix the method name with default Keyword to implement the default method
      • Grammar format
        • public interface MyInterface{
        • default void print(){
        • // I'm the default method
        •     }
        • }
      • Conflict resolution
        • Problem description : If a class inherits multiple interfaces , The default method with the same name is defined inside these interfaces
        • 1) The method in the class has the highest priority . A method declared in a class or parent class takes precedence over any method declared as the default .
        • 2) If you can't judge according to the first article , Then the sub interface has the highest priority : Function signatures are the same , The interface with the most specific implementation default method is preferred , That is, if B Inherited A, that B Just like A More specific .
        • 3) Last , If you still can't judge , Classes that inherit multiple interfaces must override and call the desired methods by displaying , The displayed selection uses the implementation of which default method .
    • JDK8-- Interface declaration static method
      • Grammar format
        • public interface Vehicle {
        •    default void defaultMethod(){
        •       System.out.println(' I'm the default method !');
        •    }
        •     // Static methods
        •    static void staticMethod(){
        •       System.out.println(' I'm a static method !!!');
        •    }
        • }
  • Pre knowledge : Process oriented programming POP
    • It's a process centric programming idea , Analyze the steps needed to solve the problem , Then use functions to implement these steps step by step , When using, call one by one until the problem is solved
    • advantage
      • Break the problem down into steps 、 Like an assembly line , Step by step , Process and simplify complex problems . Higher performance than object-oriented programming , In object-oriented programming, the class calling process needs to load 、 Instantiation , Greater resource consumption ; High performance requirements, such as single chip microcomputer 、 Embedded development 、 Linux/Unix Process oriented development is generally adopted for
    • shortcoming
      • No object-oriented programming, easy to maintain 、 Easy to reuse 、 Easy to expand
    • Applicable scenario
      • In the development of systems with high performance requirements
  • Pre knowledge : object-oriented programming OOP
    • Object oriented programming is an object-oriented programming idea , By analyzing the problem , Decompose objects one by one , Object contains specific properties and behaviors , Then through the calls between different objects to solve the problem .
    • advantage
      • 1) Easy to understand : Adopt the object-oriented idea to design and develop , More in line with people's way of thinking , High readability .
      • 2) Easy maintenance : Object oriented has encapsulation 、 Inherit 、 Characteristics of polymorphism , Even if the demand changes , More local modules need to be maintained , Easier and cheaper to maintain .
      • 3) Easy to expand : Object oriented has encapsulation 、 Inherit 、 Characteristics of polymorphism , High cohesion can be designed in the system design stage 、 Low coupling system structure , Make the system more flexible 、 It's easier to expand .
      • 4) Easy to reuse : It's the same thing , Because of the three characteristics of object-oriented , So that the general code can be reused more simply , Improve development efficiency .
    • shortcoming
      • Performance will be lower than process oriented , Programming complexity is high
    • Applicable scenario
      • User needs change all the time , Internet applications , game , Enterprise internal application
  • What is interface oriented programming
    • Interface oriented programming (Interface Oriented Programming:OIP) It's a kind of programming idea , Interface is a form of entity abstraction , It is used to separate the internal implementation for external communication , Ultimately, internal changes are implemented without affecting external interaction with other implementations


 

原网站

版权声明
本文为[Mori Ohashi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071427006642.html