接口的语法基础:
1.接口的数据类型
接口也是一种引用数据类型,其编译后生成的也是class文件。
2.接口是完全抽象的,是特殊的抽象类
抽象类是半抽象的,抽象类中还可以有具体的实例方法。但是,接口中只存在抽象的方法!
3.抽象类的定义方式:
public class Test01{
public static void main(String[] args){
}
}
interface A{
}
4.接口支持多继承
一个接口可以继承多个接口!
public class Test02{
public static void main(String[] args){
}
}
interface A{
}
interface B{
}
interface C extends A,B{//接口的多继承
}
5.接口中所有元素都是public修饰的!
public
**6. 接口中只有两部分内容!
1.常量
public static final double PI = 3.14;
2.抽象方法
public abstract void doSome();
因为接口中只有常量和抽象方法,所以二者的修饰符列表可以省略不写:
public class Test03{
public static void main(String[] args){
}
}
interface A{
public static final double PI = 3.14;
public abstract void f();
}
interface B{//这是一个接口
double PI = 3.14;//省略public static final
void f();//省略public
}
7. 接口的实现问题:
接口是一种完全抽象类,所以接口和抽象类一样不能被实例化。接口只能由实现类实现!实现类通过implements关键字修饰:
但是,接口可以看作特殊的抽象类,实现可以看作特殊的继承!
所以实现接口的实现类中必须重写/覆盖/实现接口中的抽象方法(抽象方法只能出现在抽象类中)
- 错误
public class Test04{
public static void main(String[] args){
}
}
interface A{//接口A
double PI = 3.14;
int sum(int a,int b);
int sub(int a,int b);
}
class B implements A{//实现类B实现接口A
// 错误: B不是抽象的, 并且未覆盖A中的抽象方法sub(int,int)
}
- 正确
public class Test04{
public static void main(String[] args){
}
}
interface A{//接口A
double PI = 3.14;
int sum(int a,int b);
int sub(int a,int b);
}
class B implements A{//实现类B实现接口A
//实现类必须实现接口中的抽象方法
public int sum(int a,int b){
return a+b;
}
public int sub(int a,int b){
return a-b;
}
}
注意:实现类指的是非抽象类,一般抽象类实现接口可以不重写接口中的抽象方法,因为抽象方法只能出现在抽象类中:所以,抽象类也可以实现接口
public class Test04{
public static void main(String[] args){
}
}
interface A{//接口A
double PI = 3.14;
int sum(int a,int b);
int sub(int a,int b);
}
//编译通过,没有问题!
abstract class B implements A{
}
8.接口中的多态
接口本身是一种特殊的抽象类,不能实例化。实现类可以看作是接口的继承。所以同样可以使用多态机制。
public class Test05{
public static void main(String[] args){
//接口方面的多态
//父类的引用指向子类
//面向接口编程,去实现接口,达到扩展程序的目的
Animal a = new Cat();
a.eat();
}
}
interface Animal{
void eat();
}
//实现类
class Cat implements Animal{
public void eat(){
System.out.println("猫吃猫粮");
}
}
9.接口可以多继承,那实现类可以同时实现多个接口吗?
可以,这种机制弥补了JAVA中只能单继承的缺陷
public class Test06{
public static void main(String[] args){
}
}
interface A{
void m1();
}
interface B{
void m2();
}
interface C{
void m3();
}
//实现类实现多个接口
class Test implements A,B,C{
public void m1(){
}
public void m2(){
}
public void m3(){
}
}
接口中没有继承关系的两个接口可以相互转换,编译会通过。这和类的转换不同,两个没有继承关系的类进行向上转型或者向下转型编译会报错。
接口A和接口B虽然没有继承关系,但是写代码的时候,可以互转。
编译器没意见。但是运行时可能出现:ClassCastException
public class Test07{
public static void main(String[] args){
A a = new Test();
B b = new Test();
C c = new Test();
//a.m2();报错,找不到符号
B b1 = (B)a;//实际上B 和 A接口没继承关系,但是向下转型编译通过了
b1.m2();//运行时,底层对象都是Test();
/*编译没问题,但是运行时把dog类型的对象转换成cat类型,类型转换异常
Animal a = new Dog();
Cat c =(Cat)a;
c.move();
*/
/*向下转型调用子类独有的方法
原本a.shout()编译不通过,因为Aniaml中没有shout方法
Aanimal a = new Dog();
if(a instanceof Dog){
Dog d = (Dog)a;
d.shout();
*/
}
}
}
interface A{
void m1();
}
interface B{
void m2();
}
interface C{
void m3();
}
//实现类
class Test implements A,B,C{
public void m1(){
}
public void m2(){
}
public void m3(){
}
}
10.继承和实现可以同时出现
- extends 在前 , implements 在后。
- 使用接口,可以使用多态机制。
public class Test08{
public static void main(String[] args){
//多态
//父类型接口指向子类型对象
//使用Flyable而不是Animal是问了调用fly()通过编译
Flyable c = new Cat();
c.fly();
}
}
class Animal{
}
interface Flyable{
void fly();
}
class Cat extends Animal implements Flyable{
public void fly(){
System.out.println("Cat fly!");
}
}