【java】接口(行为)

接口我们可以看作是抽象类的一种特殊情况,在接口中只能定义抽象的方法和变量。

 在 java 中接口采用 interface 声明;

 接口中的方法默认都是 public abstract 的,不能更改;

 接口中的变量是 public static final 类型的,不能更改,所以必须显示的初始化;

 接口不能被实例化,接口中没有构造函数的概念;

 接口之间可以继承,但接口之间不能实现;

 接口中的方法只能通过类来实现,通过 implements 关键字;

 如果一个类实现了接口,那么接口中所有的方法必须实现;

 一类可以实现多个接口;

1、接口中的方法默认都是 public abstract 的,不能更改

public class InterfaceTest01 {    
    public static void main(String[] args) {  
    }  
} 
 
//采用 interface 定义接口 
//定义功能,没有实现 
//实现委托给类实现 
interface StudentManager {    
    //正确,默认 public abstract 等同 public abstract void addStudent(int id, String name);  
    void addStudent(int id, String name);     
    //正确  
    //public abstract void addStudent(int id, String name);    
    //正确,可以加入 public 修饰符,此种写法较多  
    public void delStudent(int id);    
    //正确,可以加入 abstract,这种写法比较少  
    public abstract void modifyStudent(int id, String name);    
    //编译错误,因为接口就是让其他人实现  
    //采用 private 就和接口原本的定义产生矛盾了  
    private String findStudentById(int id); 
 
} 

2、接口中的变量是 public static final 类型的,不能更改,所以必须显示的初始化 

public class InterfaceTest02 {    
    public static void main(String[] args) {      
        //不能修改,因为 final 的   
        //StudentManager.YES = "abc";      
        System.out.println(StudentManager.YES);  
    }  
} 
 
interface StudentManager {    
    //正确,默认加入 public static final   
    String YES = "yes";    
    //正确  
    public static final String NO = "no";     
    //错误,必须赋值,因为是 final 的  
    //int ON;    
    //错误,不能采用 private 声明  
    private static final int OFF = -1; 
}

3、接口不能被实例化,接口中没有构造函数的概念 

public class InterfaceTest03 {    
    public static void main(String[] args) {      
        //接口是抽象类的一种特例,只能定义方法和变量,没有实现   
        //所以不能实例化   
        StudentManager studentManager = new StudentManager();  
    }  
}
interface StudentManager {    
        public void addStudent(int id, String name);  
}

4、接口之间可以继承,但接口之间不能实现 

public class InterfaceTest04 {    
    public static void main(String[] args) {

      }  
} 
 
interface inter1 {  
    public void method1();     
    public void method2();  
} 
 
interface inter2 {  
    public void method3();  
} 
 
//接口可以继承
interface inter3 extends inter1 { 
 
 public void method4(); 
} 
 
//接口不能实现接口 
//接口只能被类实现 
interface inter4 implements inter2 {  
    public void method3(); 
}

5、如果一个类实现了接口,那么接口中所有的方法必须实现

public class InterfaceTest05 {    
    public static void main(String[] args) {

    //Iter1Impl 实现了 Inter1 接口   
    //所以它是 Inter1 类型的产品   
    //所以可以赋值   
    Inter1 iter1 = new Iter1Impl();   
    iter1.method1();      

    //Iter1Impl123 实现了 Inter1 接口   
    //所以它是 Inter1 类型的产品   
    //所以可以赋值   
    iter1 = new Iter1Impl123();   
    iter1.method1();      

    //可以直接采用 Iter1Impl 来声明类型   
    //这种方式存在问题   
    //不利于互换,因为面向具体编程了   
    Iter1Impl iter1Impl = new Iter1Impl();   
    iter1Impl.method1();      

    //不能直接赋值给 iter1Impl   
    //因为 Iter1Impl123 不是 Iter1Impl 类型   
    //iter1Impl = new Iter1Impl123();   
    //iter1Impl.method1();     
    }  
} 
 
//接口中的方法必须全部实现 
class Iter1Impl implements Inter1 {    
        public void method1() {   
            System.out.println("method1");   
        }    
        public void method2() {   
            System.out.println("method2");   
        }    
        public void method3() {   
            System.out.println("method3");   
        } 
} 
 
class Iter1Impl123 implements Inter1 {  
    public void method1() {   
        System.out.println("method1_123");   
        }    
    public void method2() {   
        System.out.println("method2_123");   
        }    
    public void method3() {   
        System.out.println("method3_123");   
        } 
} 
 
abstract class Iter1Impl456 implements Inter1 {    
        public void method1() {   
            System.out.println("method1_123");   
        }    
        public void method2() {   
            System.out.println("method2_123");   
        }    
        //再次声明成抽象方法  
        public abstract void method3(); 
} 
 
 
//定义接口 
interface Inter1 {    
    public void method1();    
    public void method2();    
    public void method3();  
}

6、一类可以实现多个接口

public class InterfaceTest06 {    
    public static void main(String[] args) {      
        //可以采用 Inter1 定义   
        Inter1 inter1 = new InterImpl();   
        inter1.method1();      

        //可以采用 Inter1 定义   
        Inter2 inter2 = new InterImpl();   
        inter2.method2();      

        //可以采用 Inter1 定义   
        Inter3 inter3 = new InterImpl();   
        inter3.method3();  
    }  
} 
 
//实现多个接口,采用逗号隔开 
//这样这个类就拥有了多种类型 /
/等同于现实中的多继承 
//所以采用 java 中的接口可以实现多继承 
//把接口粒度划分细了,主要使功能定义的含义更明确 
//可以采用一个大的接口定义所有功能,替代多个小的接口, 
//但这样定义功能不明确,粒度太粗了 
class InterImpl implements Inter1, Inter2, Inter3 { 
     public void method1() {   
        System.out.println("----method1-------");  
     }     
     public void method2() {   
        System.out.println("----method2-------");  
     }    
     public void method3() {   
        System.out.println("----method3-------");  
    } 
} 
interface Inter1 {    
    public void method1();  
} 
 
interface Inter2 {    
    public void method2();  
} 
 
interface Inter3 {    
    public void method3();  
} 
 
/* 
interface Inter {        
    public void method1();     
    public void method2();     
    public void method3(); } 
*/ 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值