JAVA learning -5 inner class species

The role of internal class: can indirectly achieve multiple inheritance
inner classes in Java is divided into four categories:

  • Static inner classes static inner class (also called nested class)
  • Members of the inner class member inner class
  • Local internal local inner class class
  • Anonymous inner classes anonymous inner class

1, static inner classes

The simplest form of inner classes.

When the class definition plus the static keyword .

And external classes can not have the same name.

Are compiled into a completely separate .class file name is in the form of OuterClass $ InnerClass.class.

You can access only static members and static methods of the outer class , including static members and methods private.

Generating a static inner class object way:

OuterClass.InnerClass inner = new OuterClass.InnerClass();

Static inner classes using the code:

package com.learnjava.innerclass;

class StaticInner
{
    private static int a = 4;

    // 静态内部类
    public static class Inner
    {
        public void test()
        {
            // 静态内部类可以访问外部类的静态成员
            // 并且它只能访问静态的
            System.out.println(a);
        }

    }
}

public class StaticInnerClassTest
{

    public static void main(String[] args)
    {
        StaticInner.Inner inner = new StaticInner.Inner();
        inner.test();
    }
}

2, members of the inner class

Members of the inner class is defined in another class, but do not modify the definition of static.

Members of the inner class and static inner classes can be compared for non-static member variables and static member variables.

Members of the inner class as an instance variable.

It has access to all its member variables and methods of the outer class, whether it is static or non-static can be.

Create an instance of class members inside of the outer class inside:

this.new Innerclass();

Create an instance of the inner class outside the outer class:

(new Outerclass()).new Innerclass();

Access external members of the class inside the class:

Outerclass.this.member

For details, see code examples:

class MemberInner
{
    private int d = 1;
    private int a = 2;

    // 定义一个成员内部类
    public class Inner2
    {
        private int a = 8;

        public void doSomething()
        {
            // 直接访问外部类对象
            System.out.println(d);
            System.out.println(a);// 直接访问a,则访问的是内部类里的a

            // 如何访问到外部类里的a呢?
            System.out.println(MemberInner.this.a);
        }

    }

}

public class MemberInnerClassTest
{

    public static void main(String[] args)
    {

        // 创建成员内部类的对象
        // 需要先创建外部类的实例
        MemberInner.Inner2 inner = new MemberInner().new Inner2();

        inner.doSomething();
    }
}

3, a partial inner class

Internal local class definition in the method, is also smaller than the scope of the method. One type is the least internal class used.

Like local variables can not be modified public, protected, private, and static.

Local variables can only be the final type of access methods defined.

Internal local class definition in the method, it can only be used in the method, i.e., it generates only a partial inner class instance in which the method and call its methods.

class LocalInner
{
    int a = 1;

    public void doSomething()
    {
        int b = 2;
        final int c = 3;
        // 定义一个局部内部类
        class Inner3
        {
            public void test()
            {
                System.out.println("Hello World");
                System.out.println(a);

                // 不可以访问非final的局部变量
                // error: Cannot refer to a non-final variable b inside an inner
                // class defined in a different method
                // System.out.println(b);

                // 可以访问final变量
                System.out.println(c);
            }
        }

        // 创建局部内部类的实例并调用方法
        new Inner3().test();
    }
}

public class LocalInnerClassTest
{
    public static void main(String[] args)
    {
        // 创建外部类对象
        LocalInner inner = new LocalInner();
        // 调用外部类的方法
        inner.doSomething();
    }

}

4, an anonymous inner classes
Anonymous inner class is not the name of the local inner classes, do not use the keyword class, extends, implements, no constructor.

Anonymous inner classes implicitly inherit a parent class or implement an interface.

Anonymous inner classes used most often, usually as a method parameter.

import java.util.Date;

public class AnonymouseInnerClass
{

    @SuppressWarnings("deprecation")
    public String getDate(Date date)
    {
        return date.toLocaleString();

    }

    public static void main(String[] args)
    {
        AnonymouseInnerClass test = new AnonymouseInnerClass();

        // 打印日期:
        String str = test.getDate(new Date());
        System.out.println(str);
        System.out.println("----------------");

        // 使用匿名内部类
        String str2 = test.getDate(new Date()
        {
        });// 使用了花括号,但是不填入内容,执行结果和上面的完全一致
            // 生成了一个继承了Date类的子类的对象
        System.out.println(str2);
        System.out.println("----------------");

        // 使用匿名内部类,并且重写父类中的方法
        String str3 = test.getDate(new Date()
        {

            // 重写父类中的方法
            @Override
            @Deprecated
            public String toLocaleString()
            {
                return "Hello: " + super.toLocaleString();
            }

        });

        System.out.println(str3);
    }
}
Published 57 original articles · won praise 3 · Views 6194

Guess you like

Origin blog.csdn.net/qq_39830579/article/details/102594255