- 关键词:interface
- 接口声明(抽象化)
public interface 接口名{ }
- 接口实现(实例化)
public class 实例接口名 implements 接口名{ }
- 接口继承
public interface 子接口名 extends 父接口名{ }要点
- 接口可有多个实例
- 子接口可声明于父接口名字一样的方法
- 接口类声明的属性也可以被继承
public interface lockinterface { int x = 1; void open(); void close(); } public interface lockinterface2 extends lockinterface{ int y = 2; void open(); void close(); } public class locktype implements lockinterface { @Override public void open() { System.out.println("locktype开门"); } @Override public void close() { System.out.println("locktype关门"); } } public class locktype2 implements lockinterface2{ @Override public void open() { System.out.println("locktype2开门"); } @Override public void close() { System.out.println("locktype2关门"); } } public class inter_manage { private lockinterface lock1; private lockinterface2 lock2; public void configlock1(lockinterface lock){ this.lock1 = lock; } public void configlock2(lockinterface2 lock){ this.lock2 = lock; } public void useLock1open(){ lock1.open (); } public void userLock1Close(){ lock1.close (); } public void useLock2open(){ lock2.open (); } public void userLock2Close(){ lock2.close (); } public static void main(String[] args) { inter_manage door = new inter_manage(); locktype lock1 = new locktype(); locktype2 lock2 = new locktype2(); door.configlock1(lock1); door.useLock1open(); door.userLock1Close(); door.configlock2(lock2); door.useLock2open(); door.userLock2Close(); System.out.println(lock2.x); System.out.println(lock2.y); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)