The use of java study notes 09 interface

interface interface
1) interface api program interface app = web front-end + back-end (program database) api

2) Java programming interface is a programming specification object interface. Due to the habit of standardizing team members.

3) Declaring an interface is a programming specification that restricts programmers to write the name of a class.
A class can inherit multiple interfaces, but a class can have only one parent class.
Prior to java 8, interfaces could only include: global static constants and abstract methods.

The java 8 start interface includes the following contents:

global static constant int i = 3;

Also public final static int i =3;

abstract method void conn();

是 public abstract void conn();

Static implementation method static

public static void conn(){
}

When using it, you can directly pass the interface Db.conn();

The default implementation method default

When public default int pf(int i){ reutrn i*i; } is used, it is called and executed through the object instance of the implementation class of the interface.


Functional interface (interface with and only one abstract method in the interface)

When used, it is used through lambda expressions

Let's write a simple example to implement the interface. This is just a test interface, and no specific method is written, just a shelf.
First define the interface

public interface DbDao {
    
    
    boolean connect();
    int save();
    int delById();
    int update();
    void query();

    
}

Then define two class inheritance interfaces. After inheriting the interfaces, most of the code can be generated by advanced tools.

public class DbDaoMysqlImpl implements DbDao{
    
    
    /**
     * @return
     */
    @Override
    public boolean connect() {
    
    
        System.out.println("Mysql connect success...");
        return true;
    }

    /**
     * @return
     */
    @Override
    public int save() {
    
    
        System.out.println("mysql 数据保存成功");
        return 0;
    }

    /**
     * @return
     */
    @Override
    public int delById() {
    
    
        System.out.println("mysql删除完成");
        return 0;
    }

    /**
     * @return
     */
    @Override
    public int update() {
    
    
        System.out.println("mysql修改完成");
        return 0;
    }

    /**
     *
     */
    @Override
    public void query() {
    
    
        System.out.println("mysql 查询所有");
    }
}
public class DbDaoPgsqlImpl implements DbDao{
    
    

    /**
     * @return
     */
    @Override
    public boolean connect() {
    
    
        System.out.println("PostgreSql Connection success...");
        return true;
    }

    /**
     * @return
     */
    @Override
    public int save() {
    
    
        System.out.println("pgsql 数据保存成功");
        return 0;
    }

    /**
     * @return
     */
    @Override
    public int delById() {
    
    
        System.out.println("pgsql 删除完成");
        return 0;
    }

    /**
     * @return
     */
    @Override
    public int update() {
    
    
        System.out.println("pgsql 修改完成");
        return 0;
    }

    /**
     *
     */
    @Override
    public void query() {
    
    
        System.out.println("pgsql 查询所有");
    }
}

A simple demo verification interface can be written below.

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        DbDao my=new DbDaoMysqlImpl();
        my.connect();
        my.save();
        my.delById();
        my.update();
        my.query();
        System.out.println("----------------------------------");
        DbDao pg=new DbDaoPgsqlImpl();
        pg.connect();
        pg.save();
        pg.delById();
        pg.update();
        pg.query();
    }
}

Guess you like

Origin blog.csdn.net/xxxmou/article/details/129092663
Recommended