- package innertest;
- interface product{
- void price();
- }
- public class test1 {
- private int i=1;
- private String ts="test1";
- private class shoe implements product{
- public void price(){
- System.out.println("i am excuted at "+ts+" innerclass!");
- test();//内部类可以访问类的成员和方法
- }
- }
- public shoe getshoe(){
- return new shoe();
- }
- private void test(){
- System.out.println(i);
- }
- public static void main(String[] args){
- test1 t=new test1();
- product p=t.getshoe();
- test1.shoe s=t.new shoe();
- s.price();
- p.price();
- }
- }
- 使用内部类,可以完全隐藏实现细节。某个类的内部类和其成员,方法具有同等的权利
java内部类的试验
最新推荐文章于 2023-03-25 20:36:11 发布