keep on:接口

接口

  1. 接口中第一的属性必须是public static final 的,而且接口中定义的方法则必须是public abstract的,因此这些修饰符可以部分或全部省略。

  2. 接口中定义的属性的值在实现类中不能被更改。

  3. 一个类可以实现某个接口,但不可继承某个接口

  4. 但接口可以继承接口

  5. 如果一个类实现了一个接口的部分方法,则该类必须声明为抽象类

  6. 一个类在继承一个父类的同时实现一个或多个接口,但extends关键字必须在implements 之前

  7. 不可以new接口对象,但可以定义一个接口引用类型的变量并将其指向实现接口的对象,达到多态的目的!

package hbbitter01jk;
interface It{
    int i = 20;
    public void f();
}
 abstract class B{
     public void f(){
         
     }
 }
​
public class Hbbitter01jk {
​
    public static void main(String[] args) {
      
    }
    
}
interface It{
    public static final int i = 20;
    public abstract void f();
}
 interface It2{
    int i = 20;
    void f();
}//完全相等,只不过是把它省略了
package hbbitter01jk;
interface It{
    public static final int i = 20;
    public abstract void f();
}
 interface It2{
    int i = 20;
    void f();
}
 class A implements It2{//implements 不能改为extends,因为类可以继承类,但不可以继承接口。逻辑意义不同。
     //类可以实现接口
public void f(){
    System.out.printf("%d",i);
}
   
    
}
public class Hbbitter01jk {
​
    public static void main(String[] args) {
      A aa = new A();
      aa.f();
    }
    
}/*
run:
20成功构建 (总时间: 0 秒)
*/
​
package hbbitter01jk;
class A{
    
}
interface It1{
   
}
 interface It2{
  
}
interface It3 extends It1,It2
{
    
}
interface It4{
     int i = 4;
}
class T extends A implements It4,It3{
   
}
​
public class Hbbitter01jk {
​
    public static void main(String[] args) {
    
    }
    
}
/*
run:
成功构建 (总时间: 0 秒)
*/
interface It1{
   void f();
}
class A implements It1{
   public  void f(){
        System.out.printf("AAAA");
    }
}

common question:

class A{
     int i;
     public void show(){
         System.out.printf("%d",i);//这里的i是属性i,系统会自动进行赋值
         
     }
     public void f(){
         int i;//这里的i和属性i没有冲突
         //System.out.printf("%d",i);//因为i是局部变量,Java在使用局部变量时,必须得初始化
     }
     public void g(int i){
   System.out.printf("%d",i);
     }
 }
​
package hbbitter01jk;
​
interface It1{
   void f();
}
class A implements It1{
   public  void f(){
        System.out.printf("AAAA");
    }
​
}
​
​
public class Hbbitter01jk {
​
    public static void main(String[] args) {
       // int i;
        It1 it;
        it = new A();
        it.f();
        
    //Tt it = new It();
    }
    
}
​
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值