-------Android培训、java培训、期待与您交流! ----------
/*
* 接口
*
* 格式:interface{}
* 接口中的成员修饰符是固定的
* 成员变量:public static final
* 成员函数:public abstract
*
* 接口的出现,将"多继承"通过另一种形式体现出来,即“多实现”
*
* 接口:初期理解,可以认为是一个特殊的抽象类。当抽象类中的方法都是抽象的,那么该类可以通过接口的形式来
* 表示interface{}
*
* class用于定义类,interface用于定义接口
*
* 接口定义时,结构特点:
* 1、接口中常见定义:常量,抽象方法
* 2、接口中的成员都有固定修饰符
* 常量:public static final
* 方法:public abstract
*
* 记住:接口中的成员都是public的
* 接口是不可以创建对象的,因为有抽象方法。需要被子类实现,子类对接口中的抽象方法全都实现后,子类才可以
* 实例化。否则子类是一个抽象类
*/
interface Inter
{
public static final int NUM=3;//如果少写,写成int NUM=3;也没关系,
//只要是接口,public static final会自动给加上。但最好写全
public abstract void show();//如果少写,写成void show();也没关系,
//只要是接口,public abstract会自动给加上。但最好写全
}
class Test implements Inter
{
public void show(){}
}
class TestDemo
{
public static void main(String[] args)
{
Test t=new Test();
System.out.println(t.NUM);
System.out.println(Test.NUM);
System.out.println(Inter.NUM);
}
}
/*
* 运行结果为:
* 3
* 3
* 3
*/
//接口可以被类多实现,也是对多继承不支持的转换形式。java支持多实现
interface InterA
{
public static final int NUM=3;
public abstract void show();
}
interface InterB
{
public abstract void method();
}
class Test1 implements InterA,InterB
{
//要想Test1能创建对象,就要覆盖show(){}和method(){}两个抽象方法
public void show(){}
public void method(){};
}
class InterfaceDemo1
{
public static void main(String[] args)
{
Test t=new Test();
System.out.println(t.NUM);
System.out.println(Test.NUM);
System.out.println(Inter.NUM);
}
}
/*
* 运行结果为:
* 3
* 3
* 3
*/
/*
* 为什么继承不可以多继承,而接口却可以实现多个接口?
* 因为如果继承的多个接口中都有相同的方法,但方法主体不同,那么子类就不知该继承哪一个
* 而可以多实现,是因为多个接口中可能也有相同的方法,但是接口中的方法是抽象方法没有方法主体,所以多实现就不会
* 遇到多继承出现的问题。
*
* 但如果多实现时多个接口出现了相同的方法,但相同方法的返回值类型不同时,也不能多实现
*
* 如以下程序:
* InterC和InterD中都有show()方法,但方法都没有不同的方法主体,所以可以被Test2多次实现接口,被覆盖
*/
interface InterC
{
public static final int NUM=3;
public abstract void show();
}
interface InterD
{
public abstract void show();
}
class Test2 implements InterC,InterD
{
public void show(){}
}
class InterfaceDemo2
{
public static void main(String[] args)
{
Test t=new Test();
System.out.println(t.NUM);
System.out.println(Test.NUM);
System.out.println(Inter.NUM);
}
}
/*
* 运行结果为:
* 3
* 3
* 3
*/
//一个类既可以继承一个类,又可以同时实现多个接口
interface InterE
{
public static final int NUM=3;
public abstract void show();
}
interface InterF
{
public abstract void show();
}
class Demo8
{
public void function(){}
}
class Test3 extends Demo8 implements InterE,InterF
{
public void show(){}
}
class InterfaceDemo3
{
public static void main(String[] args)
{
Test t=new Test();
System.out.println(t.NUM);
System.out.println(Test.NUM);
System.out.println(Inter.NUM);
}
}
/*
* 运行结果为:
* 3
* 3
* 3
*/
/*
* 类与类之间是继承关系;
* 类与接口之间是实现关系;
* 接口与接口之间是继承关系。
*/
interface A
{
void methodA();
}
interface B extends A
{
void methodB();
}
interface C extends B
{
void methodC();
}
class D implements C
{
public void methodA(){}
public void methodB(){}
public void methodC(){}
}
//一个接口可以继承多个接口,没有方法体就不会出现冲突
interface A1
{
void methodA();
}
interface B1
{
void methodB();
}
interface C1 extends A1,B1
{
void methodC();
}
//但以下情况不允许出现,A2和B2中出现了不同类型的show方法,所以C2无法同时继承B2和A2
/*
interface A2
{
void methodA();
int show();
}
interface B2
{
void methodB();
boolean show();
}
interface C2 extends B2,A2
{
void methodC();
}
class D2 implements C2
{
public void methodA(){}
public void methodB(){}
public void methodC(){}
}
*/
/*
* 接口的特点:
* 接口是对外暴露的规则
* 接口是程序的功能扩展
* 接口可以用来多实现
* 类与接口之间是实现关系,而且类可以继承一个类的同时实现多个接口
* 接口与接口之间可以有继承关系
*/
abstract class Student3
{
abstract void study();
void sleep()
{
System.out.println("sleep");
}
}
interface Smoking
{
public abstract void smoke();
}
class ZhangSan extends Student3 implements Smoking
{
void study(){}
public void smoke(){}
}
class LiSi extends Student3
{
void study(){}
}