当前位置:网站首页>2021-05-12 interface definition and Implementation

2021-05-12 interface definition and Implementation

2022-06-23 10:07:00 Deer like deer

Interface

General class : Only concrete implementation
abstract class : Specific implementation and specification ( Abstract method ) There are
Interface : Only norms , I can't write by myself - Professional constraints . Constraint and implementation separation : Interface oriented programming

Interface is specification , It defines a set of rules

The essence of an interface is a contract , Like the law , Everyone should abide by

OO The essence of , It's the abstraction of objects , The best way to do this is through interfaces

The keyword of the declaration class is class, Statement The keyword of the interface is interface

package oop.demo11;

//interface Is a defined keyword , Interfaces need to have implementation classes 
public interface UserService {
    
    // All definitions in an interface are actually abstract public public  It's ok if you don't write 
    public abstract void run();
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);
    
    // The attributes defined in the interface are constants 
    int age1 = 99;
    public static final int age = 99;   // be equal to  int age1 = 99
    
}

package oop.demo11;

public interface TimeService {
    
    void timer();
}

// Class passing implements Keyword implementation interface 
// The class that implements the interface , Need to rewrite the methods in the interface 
// Multiple inheritance , Using interfaces to implement multiple inheritance 
public class UserServiceImpi implements UserService,TimeService{
    // Multiple inheritance , Inherited UserService and TimeService Two interfaces 
    @Override
    public void run() {
    

    }

    @Override
    public void add(String name) {
    

    }

    @Override
    public void delete(String name) {
    

    }

    @Override
    public void update(String name) {
    

    }

    @Override
    public void query(String name) {
    

    }

    @Override
    public void timer() {
    

    }
}

effect :

  1. constraint ( standard )
  2. Define some methods , Let different people realize
  3. public abstract
  4. public static final
  5. Interface cannot be instantiated , There is no constructor in the interface
  6. implements Multiple interfaces can be implemented
  7. You have to override the methods in the interface
原网站

版权声明
本文为[Deer like deer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230954195345.html