美文网首页
内部类详解

内部类详解

作者: ae12 | 来源:发表于2018-10-11 13:47 被阅读23次

    Nested Classes in Java
    嵌套类:

    1.The scope of a nested class is bounded by the scope of its enclosing class. Thus in above example, class NestedClass does not exist independently of class OuterClass
    重点:嵌套类不存在外部类中

    2.A nested class has access to the members, including private members, of the class in which it is nested. However, reverse is not true i.e. the enclosing class does not have access to the members of the nested class.
    重点:嵌套类可以访问外部类的成员,包括私有成员,但是相反却不可以即:外部类不可以访问嵌套类的成员

    3.A nested class is also a member of its enclosing class.
    4.As a member of its enclosing class, a nested class can be declared private, public, protected, or package private(default).

    Nested classes are divided into two categories:
    static nested class : Nested classes that are declared static are called static nested classes.
    inner class : An inner class is a non-static nested class.
    嵌套类分为2类:
    静态嵌套类:声明为 static ,并被称为静态嵌套类
    内部类:非静态嵌套类就是内部类


    nested-classes -classify.jpeg

    static nested class :
    不能直接引用外部类变量成员而是要通过外部实例去引用。

    They are accessed using the enclosing class name.
    OuterClass.StaticNestedClass
    例如创建一个 静态嵌套类: OuterClass.StaticNestedClass staticNestedClass =new OuterClass.StaticNestedClass();

    // Java program to demonstrate accessing 
    // a static nested class 
    
    // outer class 
    class OuterClass 
    { 
        // static member 
        static int outer_x = 10; 
        
        // instance(non-static) member 
        int outer_y = 20; 
        
        // private member 
        private static int outer_private = 30; 
        
        // static nested class 
        static class StaticNestedClass 
        { 
            void display() 
            { 
                // can access static member of outer class 
                System.out.println("outer_x = " + outer_x); 
                
                // can access display private static member of outer class 
                System.out.println("outer_private = " + outer_private); 
                
                // The following statement will give compilation error 
                // as static nested class cannot directly access non-static membera 
                // System.out.println("outer_y = " + outer_y); 
            
            } 
        } 
    } 
    
    // Driver class 
    public class StaticNestedClassDemo 
    { 
        public static void main(String[] args) 
        { 
            // accessing a static nested class 
            OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); 
            
            nestedObject.display(); 
            
        } 
    } 
    

    结果输出:
    outer_x = 10
    outer_private = 30

    inner class
    To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
    要实例化内部类,首先要实例化外部类,然后用下列语法在外部对象中创建内部对象:
    OuterClass outObject =new OuterClass();
    OuterClass.InnerClass innerObject =outObject.new InnerClass();

    代码:

    // Java program to demonstrate accessing 
    // a inner class 
    
    // outer class 
    class OuterClass 
    { 
        // static member 
        static int outer_x = 10; 
        
        // instance(non-static) member 
        int outer_y = 20; 
        
        // private member 
        private int outer_private = 30; 
        
        // inner class 
        class InnerClass 
        { 
            void display() 
            { 
                // can access static member of outer class 
                System.out.println("outer_x = " + outer_x); 
                
                // can also access non-static member of outer class 
                System.out.println("outer_y = " + outer_y); 
                
                // can also access private member of outer class 
                System.out.println("outer_private = " + outer_private); 
            
            } 
        } 
    } 
    
    // Driver class 
    public class InnerClassDemo 
    { 
        public static void main(String[] args) 
        { 
            // accessing an inner class 
            OuterClass outerObject = new OuterClass(); 
            OuterClass.InnerClass innerObject = outerObject.new InnerClass(); 
            
            innerObject.display(); 
            
        } 
    } 
    

    输出:

    outer_x = 10
    outer_y = 20
    outer_private = 30
    

    Local Inner class In java
    局部内部类:
    Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. Sometimes this block can be a for loop, or an if clause.Local Inner classes are not a member of any enclosing classes. They belong to the block they are defined within, due of which local inner classes cannot have any access modifiers associated with them. However, they can be marked as final or abstract. These class have access to the fields of the class enclosing it. Local inner class must be instantiated in the block they are defined in.
    嵌套在方法体并作用于内的,可以是for 循环或是 if 子句。局部内部类不是外部类的成员,它们属于所定义在的方法体,因此不具有它们相关的访问修饰符,但是它们可以被标记为 final abstarct 。它们可以访问包含它的类的域。必须在定义它们的块内实例化局部内部类。
    Rules of Local Inner Class:

    1.The scope of local inner class is restricted to the block they are defined in.
    2.Local inner class cannot be instantiated from outside the block where it is created in.
    3.Till JDK 7,Local inner class can access only final local variable of the enclosing block. However From JDK 8, it is possible to access the non-final local variable of enclosing block in local inner class.
    4.A local class has access to the members of its enclosing class.
    Local inner classes can extend an abstract class or can also implement an interface.

    1.局部内部类范围仅限所定义的代码块中
    2.不可以在创建它的代码块外部实例化局部内部类
    3.jdk7 可以访问封闭块的final 局部变量,但是Jdk8 也可以访问非final 的局部变量
    4.可以访问封闭类的成员,局部内部可以继承一个抽象类,也可以实现一个接口

    Declaring a Local Inner class: A local inner class can be declared within a block. This block can be either a method body, initialization block, for loop or even an if statement.

    Accessing Members: A local inner class has access to fields of the class enclosing it as well as the fields of the block that it is defined within. These classes, however, can access the variables or parameters of the block that encloses it only if they are declared as final or are effectively final. A variable whose value is not changed once initialized is called as effectively final variable. A local inner class defined inside a method body, have access to it’s parameters.
    访问权限: 可以访问包含它的类的

    inner class cannot be declared static

    Demonstrating Erroneous codes for Inner class
    Inner class 的几种错误:
    情况一:

    // Java code to demonstrate that inner 
    // classes cannot be declared as static 
    public class Outer 
    { 
        private int getValue(int data) 
        { 
            static class Inner 
            { 
                private int getData() 
                { 
                    System.out.println("Inside inner class"); 
                    if(data < 10) 
                    { 
                        return 5; 
                    } 
                    else
                    { 
                        return 15; 
                    } 
                } 
            } 
              
            Inner inner = new Inner(); 
            return inner.getData(); 
        } 
          
        public static void main(String[] args) 
        { 
            Outer outer = new Outer(); 
            System.out.println(outer.getValue(10)); 
        } 
    } 
    

    Output: 编译错误。inner class 不可以static

    情况二:

    // Java code to demonstrate 
    // the scope of inner class 
    public class Outer 
    { 
        private void myMethod() 
        { 
            class Inner 
            { 
                private void innerMethod() 
                { 
                    System.out.println("Inside inner class"); 
                } 
            } 
        } 
          
        public static void main(String[] args) 
        { 
            Outer outer = new Outer(); 
            Inner inner = new Inner(); 
            System.out.println(inner.innerMethod()); 
        } 
    } 
    
    

    Output: 编译错误。Inner class 的范围必须在它定义的myMethod()方法体内使用,超出出错。
    Explanation: The above program causes compilation error because the scope of inner classes are restricted to the block they are defined in.

    相关文章

      网友评论

          本文标题:内部类详解

          本文链接:https://www.haomeiwen.com/subject/irzuaftx.html