java 接口

接口是什么?

abstract代表抽象的意思。只有声明而没有具体实现的方法叫抽象方法。无法被初始化的类叫抽象类。抽象类用abstract修饰,类里可以有抽象方法,也可以有成员变量和方法。而接口是完全抽象的类,不提供任何的具体实现。

实例如下:Test0521.java

enum Note{MIDDLE_C,C_SHARP,B_FLAT;}//创建正常类实现方法

class Instrument{
	void play(Note n){System.out.println("Instrument.play() "+n);}
	String what(){return "Instrument";}
	void adjust(){System.out.println("Adjusting Instrument");}
}
class Wind extends Instrument{
	void play(Note n){System.out.println("Wind.play() "+n);}
	String what(){return "Wind";}
	void adjust(){System.out.println("Adjusting Wind");}
}
class Percussion extends Instrument{
	void play(Note n){System.out.println("Percussion.play() "+n);}
	String what(){return "Percussion";}
	void adjust(){System.out.println("Adjusting Percussion");}
}
class Brass extends Wind{
	void play(Note n){System.out.println("Brass.play() "+n);}
	void adjust(){System.out.println("Adjusting Brass");}
}
class WoodWind extends Wind{
	void play(Note n){System.out.println("WoodWind.play() "+n);}
	String what(){return "WoodWind";}
}
class Test0521{
	static void tune(Instrument i){
		i.play(Note.MIDDLE_C);
	}
	static void tuneAll(Instrument[] e){
		for(Instrument i:e) tune(i);
	}
	public static void main(String[] args){
		Instrument[] e={new Wind(),new Percussion(),new Brass(),new WoodWind()};
		tuneAll(e);
	}
}
enum Note{MIDDLE_C,C_SHARP,B_FLAT;}//创建抽象类和抽象方法实现方法
/*抽象类和抽象方法的特点:
1.抽象类不能被实例化。
2.抽象类不一定有抽象方法,但有抽象方法的类必定是抽象类。
3.构造器和静态方法不能被声明为抽象方法。
4.抽象类的导出类必定要实现基类的所有抽象方法,不然必须被定义成抽象类。
*/
abstract class Instrument{//抽象类:用abstract修饰的类。语法:abstract class 类名
	abstract void play(Note n);//抽象方法:仅有声明没有方法体(具体实现)的方法。
	String what(){return "Instrument";}
	abstract void adjust();//语法:abstract void 方法名();
}
class Wind extends Instrument{
	void play(Note n){System.out.println("Wind.play() "+n);}
	String what(){return "Wind";}
	void adjust(){System.out.println("Adjusting Wind");}
}
class Percussion extends Instrument{
	void play(Note n){System.out.println("Percussion.play() "+n);}
	String what(){return "Percussion";}
	void adjust(){System.out.println("Adjusting Percussion");}
}
class Brass extends Wind{
	void play(Note n){System.out.println("Brass.play() "+n);}
	void adjust(){System.out.println("Adjusting Brass");}
}
class WoodWind extends Wind{
	void play(Note n){System.out.println("WoodWind.play() "+n);}
	String what(){return "WoodWind";}
}
class Test0521{
	static void tune(Instrument i){
		i.play(Note.MIDDLE_C);
	}
	static void tuneAll(Instrument[] e){
		for(Instrument i:e) tune(i);
	}
	public static void main(String[] args){
		Instrument[] e={new Wind(),new Percussion(),new Brass(),new WoodWind()};
		tuneAll(e);
	}
}
------------------------------------------------------------------------------------------------------------------
E:\java>java Test0521
Wind.play() MIDDLE_C
Percussion.play() MIDDLE_C
Brass.play() MIDDLE_C
WoodWind.play() MIDDLE_C
enum Note{MIDDLE_C,C_SHARP,B_FLAT;}//创建接口实现方法

interface Instrument{//接口:完全抽象的类。只提供形式,而不提供任何具体实现。
	int VALUE=5;//1.接口中,域隐式地是static和final的。
	void play(Note n);//2.接口中,所有方法都自动是public的。
	String what();//3.接口没有构造器、静态块和静态方法。
	void adjust();//4.一个类只能继承一个类,但可以实现多个接口。
}//5.接口可以继承,而且可以实现多继承。语法为:extends 接口名[,接口名,接口名...]
//6.接口可以嵌套在类或者其他接口中。
class Wind implements Instrument{//接口实现语法:implements 接口名[,接口名,接口名...]
	public void play(Note n){System.out.println("Wind.play() "+n);}//实现接口方法必须用public修饰。
	public String what(){return "Wind";}
	public void adjust(){System.out.println("Adjusting Wind");}
}
class Percussion implements Instrument{
	public void play(Note n){System.out.println("Percussion.play() "+n);}
	public String what(){return "Percussion";}
	public void adjust(){System.out.println("Adjusting Percussion");}
}
class Brass extends Wind{
	public void play(Note n){System.out.println("Brass.play() "+n);}
	public void adjust(){System.out.println("Adjusting Brass");}
}
class WoodWind extends Wind{
	public void play(Note n){System.out.println("WoodWind.play() "+n);}
	public String what(){return "Wind";}
}
public class Test0521{
	static void tune(Instrument i){
		i.play(Note.MIDDLE_C);
	}
	static void tuneAll(Instrument[] e){
		for(Instrument i:e) tune(i);
	}
	public static void main(String[] args){
		Instrument[] e={new Wind(),new Percussion(),new Brass(),new WoodWind()};
		tuneAll(e);
	}
}
------------------------------------------------------------------------------------
E:\java>java Test0521
Wind.play() MIDDLE_C
Percussion.play() MIDDLE_C
Brass.play() MIDDLE_C
WoodWind.play() MIDDLE_C
//使用接口的原因:
interface CanFight{void fight();}//1.使用接口能够向上转型为多个基类型

interface CanSwim{void swim();}//2.使用接口能够防止客户端程序员创建该类的对象(抽象类同理)

interface CanFly{void fly();}

class ActionCharacter{public void fight(){System.out.println("ActionCharacter.fight()");}}

class Hero extends ActionCharacter implements CanFight,CanSwim,CanFly{
	public void swim(){System.out.println("Hero.swim()");}
	public void fly(){System.out.println("Hero.fly()");}
}
public class Test0521{
	static void a(CanFight x){x.fight();}
	static void b(CanSwim x){x.swim();}
	static void c(CanFly x){x.fly();}
	static void d(ActionCharacter x){x.fight();}
	public static void main(String[] args){
		Hero h=new Hero();
		a(h);b(h);c(h);d(h);
	}
}
-----------------------------------------------------------
E:\java>java Test0521
ActionCharacter.fight()
Hero.swim()
Hero.fly()
ActionCharacter.fight()
interface Monster{void menace();}//通过继承来扩展接口
interface DangerousMonster extends Monster{//继承接口
	void destroy();
}
interface Lethal{void kill();}
class DragonZilla implements DangerousMonster{
	public void menace(){}
	public void destroy(){}
}
interface Vampire extends DangerousMonster,Lethal{//继承多个接口
	void drinkBlood();
}
class VeryBadVampire implements Vampire{
	public void menace(){}
	public void destroy(){}
	public void kill(){}
	public void drinkBlood(){}
}
public class Test0521{
	static void f(Monster m){m.menace();}
	static void g(DangerousMonster d){
		d.menace();d.destroy();
	}
	static void h(Lethal l){l.kill();}
	public static void main(String[] args){
		DangerousMonster d=new DragonZilla();
		f(d);g(d);
		Vampire v=new VeryBadVampire();
		f(v);g(v);h(v);
	}
}
interface I1{void f();}//组合接口时的重名冲突:需要组合的接口之间要避免出现重名方法
interface I2{int f(int i);}
interface I3{int f();}
class C{public int f(){return 1;}}

class C2 implements I1,I2{
	public void f(){}
	public int f(int i){return 1;}
}

class C3 extends C implements I2{
	public int f(int i){return 1;}
}

class C4 extends C implements I3{
	public int f(int i){return 1;}
}

class C5 extends C implements I1{//重载方法只通过返回类型是区分不开的
	public void f(){}
}

interface I4 extends I1,I3{}//需要组合的两个接口中,返回类型不一样的同名方法是不能兼容的
-----------------------------------------
E:\java>javac Test0521.java
Test0521.java:229: C5 中的 f() 无法覆盖 C 中的 f();正在尝试使用不兼容的返回类型

找到: void
需要: int
        public void f(){}
                    ^
Test0521.java:232: 类型 I3 和 I1 不兼容;两者都定义了 f(),但却带有不相关的返回
类型
interface I4 extends I1,I3{}
^
2 错误
//接口中的域:自动是static和final。
interface Month{//Java SE5之前都是用接口来群组常量(生成枚举类型),Java SE5之后,由enum(枚举类型)关键字来实现这一功能。
	int JANUARY=1,FEBRUARY=2,MARCH=3,APRIL=4,MAY=5,JUNE=6,JULY=7,AUGUST=8,SEPTEMBER=9,OCTOBER=10,NOVEMBER=11,DECEMBER=12;
	int RANDOM_MONTH=(int)(Math.random()*12)+1;//不能是“空final”,但是可以被非常量表达式初始化。
}
//嵌套接口:接口可以嵌套在类或者其他接口中
class A{//嵌套在类中

	interface B{void f();}//包访问接口
	public class BImp implements B{
		public void f(){}
	}
	private class BImp2 implements B{
		public void f(){}
	}

	public interface C{void f();}//public接口
	public class CImp implements C{
		public void f(){}
	}
	private class CImp2 implements C{
		public void f(){}
	}

	private interface D{void f();}//private接口:不能在定义它的类之外被实现
	private class DImp implements D{
		public void f(){}
	}
	public class DImp2 implements D{//这种实现方式不允许向上转型
		public void f(){}
	}

	public D getD(){return new DImp2();}
	private D dRef;
	public void receiveD(D d){
		dRef=d;
		dRef.f();
	}
}

interface E{//嵌套在接口中:所有的接口元素都必须是public的

	interface G{void f();}//包访问接口

	public interface H{void f();}//public接口
	
	//private interface I{}//private接口出错:非法的修饰组合:public和private

	void g();
}
public class Test0521{
	public class BImp implements A.B{
		public void f(){}
	}
	class CImp implements A.C{
		public void f(){}
	}
	
	//class DImp implements A.D{}//出错:A.D可以在A中访问private

	class EImp implements E{//实现接口时,并不需要实现嵌套在其内部的任何接口
		public void g(){}
	}
	class EGImp implements E.G{
		public void f(){}
	}
	class EImp2 implements E{
		public void g(){}
		class EG implements E.G{
			public void f(){}
		}
	}
	public static void main(String[] args){
		A a=new A();

		//不能直接使用getD()的返回值,只能将返回值交给有权使用它的对象。
		//A.D ad=a.getD();//出错:A.D可以在A中访问private
		//A.DImp2 d2=a.getD();//出错:不兼容的类型
		//a.getD().f();//出错:A.D中的f()是在不可访问的类或接口中定义的

		A a2=new A();
		a2.receiveD(a.getD());
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值