Java-接口

1)接口可以理解为一种特殊的抽象类,是一种单独引用类型
2)接口中的所有属性:全是公开,静态的常量
接口中所有的方法:全是公开,抽象方法
3)接口中的关键字:interface
4)接口可以让一个类去实现(继承),该类继承接口中属性,并实现所有的抽象方法

public class Test1 {
    public static void main(String[] args) {
        Aoo aoo=new Aoo();
        aoo.print();
        aoo.sub(23,12);
        //父类声明指向子类对象
        IAoo aoo2=new Aoo();
        aoo2.print();
        aoo2.sub(25, 23);
    }
}
interface IAoo{
    public static final double PI=3.14;
    int MIN=1;
    public abstract void sub(int num1,int num2);
    void print();
}
class Aoo implements IAoo{
    public void sub(int num1, int num2) {
        int result=num1-num2;
        System.out.println(result);
    }
    public void print() {
        System.out.println(PI);        
    }    
}

5)接口依然支持父类声明指向子类对象
格式:接口名 对象名=new 实现类构造方法
6)接口中没有构造方法,接口不能直接实例化,但其实现类可以直接实例化
7)接口的目的:加强java的功能,一个子类只能有一个直接父类,但可以多个接口实现
java单继承(父类),多实现(接口)

public class Test1 {
    //抽象类名(父亲) 对象名=new 子类的构造方法
    public static void main(String[] args) {
        Emp1 emp=new Emp1();
        emp.f1();
        emp.f2();
    //父类声明指向子类对象
    //如果声明(定义)对象时,用接口名来定义,该对象只能调用定义名字接口中的方法
        Moo moo=new Emp1();
        moo.f1();
//        moo.f2();---报错
    //用Moo声明的,Emp就相当于Moo的子类对象,f2方法在Moo并没有,不能调用
    }
}
interface Moo{
    void f1();
}
interface Noo{
    void f2();
}
class Emp1 implements Moo,Noo{
    public void f2() {
        System.out.println("Noo接口的方法");    
    }
    public void f1() {
        System.out.println("Moo接口的方法");
    }    
}

8)接口可以多继承:一个接口可以继承多个接口
格式:接口 extends 接口1,接口2,接口3…

public class Test1 {
    public static void main(String[] args) {
        FALL foo=new Foo2();
        foo.f1();
        foo.f2();
        foo.f3();
        foo.f4();
    }
}
interface F1{
    void f1();
}
interface F2{
    void f2();
}
interface F3{
    void f3();
}
interface FALL extends F1,F2,F3{
    void f4();
}
//接口的实现类必须要实现接口中所有的抽象方法
class Foo2 implements FALL{
    public void f1() {
        System.out.println("f1");        
    }
    public void f2() {
        System.out.println("f2");        
    }
    public void f3() {
        System.out.println("f3");        
    }
    public void f4() {
        System.out.println("f4");
    }    
} 

总结:接口跟抽象类之间的区别
1.定义的方式不同
接口是用interface,抽象类是abstract class
2.内涵:
接口强调要实现的方法,全部是抽象方法
抽象类强调是继承关系,抽象类本质就是一个类,只不过包含抽象方法(没有方法体的方法)
3.构造方法:
接口没有任何构造方法
抽象类可以有构造方法
4.继承:
接口可以多继承
抽象类只能单继承
5.访问修饰符:
接口中的方法和属性:全部是public
抽象类中的方法和属性:访问修饰符任意
6.接口用implements表示实现类实现该接口,一个接口继承多个接口,可以extends
抽象类只能用extends来表示继承

public class Test1 {
    public static void main(String[] args) {
        Taxi taxi=new Taxi();
        taxi.name="出租车";
        taxi.color="绿色";
        taxi.speed=0;
        taxi.start();
        taxi.run();
        taxi.stop();
        taxi.getMoney();
        taxi.controlDegree();
        Bmw bmw=new Bmw();
        bmw.name="宝马";
        bmw.color="白色";
        bmw.speed=0;
        bmw.start();
        bmw.run();
        bmw.stop();
        bmw.controlDegree();
    }
}
//抽象类,Car,属性:名字,颜色,速度
//                  方法:启动,行驶,刹车(抽象方法)
//接口1,收费:getMoney
//接口2,控制温度:controlDegree
//Taxi:属性:名字,颜色,速度,价格
//      方法:启动,行驶,刹车,收费,控制温度
//Bmw:属性:名字,颜色,速度
//     方法:启动,行驶,刹车,控制温度      
abstract class Car{
    String name;
    String color;
    int speed;
    Car(){}
    Car(String name,String color,int speed){
        this.name=name;
        this.color=color;
        this.speed=speed;
    }
    public abstract void start();
    public abstract void run();
    public abstract void stop();
}
interface Fee{
    int Fee=100;
    void getMoney();
}
interface Degree{
    void controlDegree();
}
class Taxi extends Car implements Fee,Degree{
    double price;
    public void controlDegree() {
        System.out.println("控制温度");
    }
    public void getMoney() {
        System.out.println("收取"+Fee);
    }
    public void start() {
        speed=speed+100;
        System.out.println(name+"启动");
    }
    public void run() {
        System.out.println(name+"在行驶,速度"+speed);    
    }
    public void stop() {
        speed=0;
        System.out.println(name+"刹车了,速度"+speed);
    }    
}
class Bmw extends Car implements Degree{
    public void start() {
        speed=speed+120;
        System.out.println(name+"启动");
    }
    public void run() {
        System.out.println(name+"在行驶,速度"+speed);    
    }
    public void stop() {
        speed=0;
        System.out.println(name+"刹车了,速度"+speed);    
    }
    public void controlDegree() {
        System.out.println("控制温度");
    }
}

接口,工厂模式,父类声明指向子类的综合
在sun公司,接口用来指定规范,比如很多种不同数据库,通过java来操作数据库的代码,都不太一样,sun不编写操作数据库具体代码,指定一个接口,再指定一个方法,由其他的数据库公司来实现该接口,编写操作自己的公司数据库的实现类

public class Test1 {
    //设计一个接口,java连接数据库
    //sun:经常使用接口,作为规范,比如设计一个连接数据库接口,由其他数据库软件公司来编写该接口实现类
    public static void main(String[] args) {
        Driver db2=DriverFactory.getInstance(1);
        db2.connect();
        Driver oracle=DriverFactory.getInstance(2);
        oracle.connect();
        Driver sqlServer=DriverFactory.getInstance(3);
        sqlServer.connect();
    }
}
//一个接口Driver有3个不同的实现类,用来连接不同的数据库
//工厂类:根据用户参数的不同来提供不同的数据库连接
class DriverFactory{
    public static Driver getInstance(int type){
        Driver driver=null;
        if(type==1){
            driver=new DB2Driver();
        }else if(type==2){
            driver=new OracleDriver();
        }else if(type==3){
            driver=new SQLServerDriver();
        }
        return driver;
    }
}
interface Driver{
    void connect();//java连接数据库
}
class DB2Driver implements Driver{
    public void connect() {
        System.out.println("java连接DB2...");    
    }    
}
class OracleDriver implements Driver{
    public void connect() {
        System.out.println("java连接Oracle...");    
    }    
}
class SQLServerDriver implements Driver{
    public void connect() {
        System.out.println("java连接SQLServer...");        
    }    
}
原文地址:https://www.cnblogs.com/lilyo/p/12792801.html