1 Interface
1.1 concept:
- Because Java does not allow multiple inheritance inside, so if you want to realize the function of more than one class, it can be achieved by implementing multiple interfaces.
- Abstract type is Java Java abstract classes and interfaces represented, is a concrete manifestation of abstraction we need to be raised.
- OOP object-oriented programming, if to increase the reuse rate program, the program increases maintainability, extensibility, the interface must be oriented programming, abstract-oriented programming.
- Proper use of interfaces, abstract classes are too useful as an abstract type on top of java structural level.
- interface interface name codes {...}
1.2 Features:
- Interfaces are abstract
- Create the interface by interface keyword
- By implements let subclasses to achieve
- As will be appreciated, the interface is an abstract class special
- Interface broke through the limitations of single inheritance java
- Can achieve more than between interfaces and classes, can multiple inheritance between interfaces and interfaces
- The interface is exposed outside of the rules, a set of development standards
- Interface improve the function expansion program, reducing the coupling! ! Coupling inherited strong! !
public class Test1_Interface {
public static void main(String[] args) {
//TODO 创建多态对象测试
}
}
//接口可以看做是程序的一套规范规则
//通过interface关键字定义接口
interface Inter{
//接口里可以有普通方法吗??--- 不可以,都是抽象方法
abstract public void save() ;
abstract public void delete() ;
}
//接口的实现类 和 接口之间是 implements实现的关系
//实现类实现了接口后,有两条路可以选:是一个抽象的实现类 + 重写接口中的所有抽象方法
//abstract class InterImpl implements Inter {
class InterImpl implements Inter {
@Override
public void save() {
System.out.println("正在保存您的数据...");
}
@Override
public void delete() {
System.out.println("正在删除您的数据...");
}
}
Usage 2 interface
2.1 constructor:
- There is no interface to the constructor.
- The default when you create an object implementation class super (), is the default constructor without parameters Object of the call.
//创建接口 interface Inter2{ //1、接口里可以有构造方法吗? --- 不能!! // public Inter2() {} }
2.2 member variables:
- There is no interface to member variables are constant. So, when you define a variable did not write modifiers, the default will add: public static final
2.3 member method:
- Interface methods in the default we are all abstract, if you are not stated in the abstract, it will be automatically filled.
2.4 complex Usage:
- The limitations of single inheritance in Java can be resolved through the interface.
- Interface can achieve multiple inheritance can also be more, even more than at the same time can be inherited achieved.
public class Test4_ComInter {
public static void main(String[] args) {
Interface1 in = new Interface1Impl();
in.save();
in.update();
}
}
//创建接口1
interface Interface1{
void save();
void update();
}
//创建接口2
interface Interface2{
void get();
void delete();
}
//打破了java单继承的局限性,因为接口之间可以多继承,多个接口之间逗号隔开
interface Interface3 extends Interface1,Interface2{
void add();
}
//接口还可以多实现吗??---可以多实现,只不过接口之间逗号隔开
class ManyImpl implements Interface1,Interface2{
public void save() {}
public void update() {}
public void get() {}
public void delete() {}
}
//接口可以继承的同时,多实现?? --
class MoreImple extends ManyImpl implements Interface1,Interface2 {
}
//创建实现类,使用3号接口的功能,需要重写几个方法呢??---同时重写1号和2号和3号接口里的所有功能
class Interface3Impl implements Interface3{
@Override
public void save() { }
@Override
public void update() { }
@Override
public void get() { }
@Override
public void delete() { }
@Override
public void add() { }
}
//创建实现类
class Interface1Impl implements Interface1{
@Override
public void save() {
System.out.println("save()...");
}
@Override
public void update() {
System.out.println("update()...");
}
}