java学习 接口派生 内部类实现接口 暑假第七天

静态的内部类是不需要外部类的 实例的 非静态的内部类是不能定义一个静态的方法 静态可以有静态方法

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 } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值