您的位置:首页 > 编程语言 > Java开发

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 } }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: