java学习 接口派生 内部类实现接口 暑假第七天
2011-07-24 00:49
344 查看
静态的内部类是不需要外部类的 实例的 非静态的内部类是不能定义一个静态的方法 静态可以有静态方法
1. 当一个类从另一个类的内部类派生的时候 那么我们在构造对象的同时 要建立一个内部类 与外部类的 映射关系 下面是例子 /// 当我们实现一个类从内部类派生出来的时候 我们需要建立一个从内部类到外部类的引用关系///car.super()//
class Car { class wheel { } }class Text extends Car.wheel{ Text(Car car)
{ car.super(); //通过这么一个特殊的调用建立内外 类之间的联系 } public static void main(String []args) { Car one=new Car(); //car作参数传递 Text planewheel=new Text(one); }}2. 内部类实现接口 接口也可以作为返回值 interface Animal //定义一个接口 { void eat(); void sleep();}class zoo //外部类 { class Tiger implements Animal //实现Animal接口 内部类 { public void sleep() { System.out.println("Tiger is sleeping !"); } public void eat() { System.out.println("Tiger is eating !"); } } Animal GetAnimal() //返回一个Animal对象 { return new Tiger(); } public static void main(String []args) { zoo p=new zoo(); //产生一个新的 zoo对象 Animal an=p.GetAnimal(); //通过 zoo的GetAnimal方法返回一个 Tiger对象 也就是 Animal接口 就跟父类一样 接口对象也可以赋予实现类对象 an.eat(); //通过接口调用方法 an.sleep();//call the method through the object of interface } }
1. 当一个类从另一个类的内部类派生的时候 那么我们在构造对象的同时 要建立一个内部类 与外部类的 映射关系 下面是例子 /// 当我们实现一个类从内部类派生出来的时候 我们需要建立一个从内部类到外部类的引用关系///car.super()//
class Car { class wheel { } }class Text extends Car.wheel{ Text(Car car)
{ car.super(); //通过这么一个特殊的调用建立内外 类之间的联系 } public static void main(String []args) { Car one=new Car(); //car作参数传递 Text planewheel=new Text(one); }}2. 内部类实现接口 接口也可以作为返回值 interface Animal //定义一个接口 { void eat(); void sleep();}class zoo //外部类 { class Tiger implements Animal //实现Animal接口 内部类 { public void sleep() { System.out.println("Tiger is sleeping !"); } public void eat() { System.out.println("Tiger is eating !"); } } Animal GetAnimal() //返回一个Animal对象 { return new Tiger(); } public static void main(String []args) { zoo p=new zoo(); //产生一个新的 zoo对象 Animal an=p.GetAnimal(); //通过 zoo的GetAnimal方法返回一个 Tiger对象 也就是 Animal接口 就跟父类一样 接口对象也可以赋予实现类对象 an.eat(); //通过接口调用方法 an.sleep();//call the method through the object of interface } }
相关文章推荐
- java学习 接口派生 内部类实现接口 暑假第七天
- java学习 返回接口匿名实例 暑假第七天
- java学习 返回接口匿名实例 暑假第七天
- Java 学习笔记 (3) -抽象类 abstract 接口 interface 内部类 inner class 继承 extends 实现 implements 包 package
- JAVA学习4——内部类实现接口 未完成
- JAVA学习4——内部类实现接口 未完成
- Java集合源码学习(10)_Set接口的实现HashSet
- Java学习笔记_22_Set接口的实现类
- Java匿名内部类继承类和实现接口
- java学习干货之接口与实现
- Java学习笔记_22_Set接口的实现类
- Java学习笔记(接口与内部类)
- Java集合源码学习(6)_List接口的实现_ArrayList_Vector
- Java基础学习笔记(七)Set接口及其实现子类
- java代理的学习,通过类实现接口来实现代理。proxy来创建动态类,和InvocationHandler接口的实现,和工作原理。
- java 内部类与接口结合实现隐藏具体的方法的实现
- 黑马程序员java学习第七天,继承及接口
- Java学习笔记四——接口与内部类
- 黑马程序员-Java学习04-接口,多态,内部类,匿名内部类,Object类
- Java学习笔记——利用接口和observer实现对象监视