java接口,private,final,static组合(课堂)

时间:2022-09-20 08:56:25


package com.qianfeng.day10.demo5;

 

//private final static组合修饰接口中的属性

 

//运用场景:工具类 

public class SingleInstance {

      

      //3、一个可以缓存唯一实例的对象

      privatefinal static SingleInstance singleInstance = new SingleInstance();

      

      //1、私有的构造方法

      privateSingleInstance(){

      }

      

      //2、提供一个静态获取实例对象的方法

      publicstatic SingleInstance getInStance(){

            return singleInstance;

      }

      

}

 

 

 

package com.qianfeng.day10.demo5;

 

public class Test {

      publicstatic void main(String[] args) {

            

            SingleInstance singleInstance2 =SingleInstance.getInStance();

            SingleInstance singleInstance1 =SingleInstance.getInStance();

            System.out.println(singleInstance1 ==singleInstance2);

            

      }

}

 

 

 

 

package com.qianfeng.day10.demo6;

// shift + alt + a

/*接口知识点:

*属性:

*1接口的属性是使用public final  static 修饰的,如果不显示写,依然是这三修饰

*  也即:接口中的属性默认是使用 public final static 修饰的,且是固定

*接口

*1、跟抽象类类似,使用 implements实现接口

*2、有抽象方法,实现的接口类必须重写接口中的所有方法

*3、一个类可以实现多个接口 implements

*4、接口可以多继承

*5接口不能new对象

*

*方法:

*

*

*

*/

public interface Fly {

      //Illegalmodifier for the interface field Fly.in;

      //onlypublic, static & final are permitted

      intin = 1;

      

      //抽象 

       void fly();

 

      

}

 

 

 

package com.qianfeng.day10.demo6;

 

public interface Run {

      voidrun();

}

 

 

 

package com.qianfeng.day10.demo6;

 

public interface FlyAndRun extends Fly,Run{

      voidfly();

      voidrun();

}

 

 

 

 

 

package com.qianfeng.day10.demo6;

 

//The type Bird must implement theinherited abstract method Fly.fly()

public class Bird implements FlyAndRun{

 

      @Override

      publicvoid fly() {

            System.out.println("鸟飞....");

      }

 

 

      @Override

      publicvoid run() {

            

      }

 

}

 

 

package com.qianfeng.day10.demo6;

 

public class Test {

      publicstatic void main(String[] args) {

            System.out.println(Fly.in);

            

            //The final field Fly.in cannot beassigned

            //Fly.in = 1111;

            

            //Fly fly = new Fly();

            

      }

}