关与class和interface的几个语法问题

本文探讨了Java中非静态内部类与外部类的引用关系,并详细解析了内部类及接口的定义规则,包括不同类型的内部类中接口的使用限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,非静态内部类持有外部类引用,但是非静态内部类的内部接口的实现类对象持有最外层类的引用么?

Handler myHandler = new Handler() {  
          public void handleMessage(Message msg) {   
               switch (msg.what) {   
                    case TestHandler.GUIUPDATEIDENTIFIER:   
                         myBounceView.invalidate();  
                         break;   
               }   
               super.handleMessage(msg);   
          }   
     }; 

2,再说几个语法

 package com.example.autoinstall;

public class TestDemo317 {
    // 内部接口里面还可以写接口
    interface Intf0 {
        public interface Intf0Sub0 {
            void test();
        }
    }

    // 非静态内部类里面不可以写接口
    class Class0 {
        interface Class0InnerIntf {// The member interface Tof1 can only be
                                    // defined inside a top-level class or
                                    // interface
            void test();
        }
    }

    // 静态内部类里面可以写接口
    static class Class1 {
        public interface InnerIntf1 {
            void test();
        }
    }

    // 内部接口里面的接口中可以再嵌套一个接口
    interface Intf2 {
        public interface InnerIntf1 {
            public interface InnerIntf2 {
                void test();
            }

            void test();
        }
    }

    interface Intf3 {
        public interface InnerIntf1 {
            public interface InnerIntf1 {// public interface不能和上级同名,The nested
                                            // type InnerIntf1 cannot hide an
                                            // enclosing type
                void test();
            }

            void test();
        }
    }

    interface Intf4 {
        public interface InnerIntf1 {
            interface InnerIntf1 {// interface不能和上级同名,The nested type InnerIntf1
                                    // cannot hide an enclosing type
                void test();
            }

            void test();
        }
    }

    // 非静态内部类里面还可以写非静态内部类
    class Class3 {
        class Class4 {
        }
    }

    class Class4 {
        class Class4 {// 不能和上级同名,The nested type Class4 cannot hide an enclosing
                        // type
        }
    }

    // 静态内部类里面还可以写非静态内部类
    static class Class5 {
        class Class6 {
        }
    }

    // 静态内部类里面还可以写静态内部类
    static class Class6 {
        static class Class7 {
        }
    }

    // 非静态内部类里面不可以写静态内部类
    class Class7 {
        static class Class8 {// The member type Tof8 cannot be declared static;
                                // static types can only be declared in static
                                // or top level types
        }
    }
}

相同包访问权限
不同包访问权限

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值