Internal Java ultra detailed summary (including code examples)

What is the inner class

What is the inner class?
As the name suggests, it is to define a class placed inside another class .
Clearly the concept, the feeling is very simple, in fact the position of the key is placed inside the class, a class may be in the range of the scope or range of a scope of the method a code block.
So I understand the concept of just the first step to completely master the details of the features of Java classes to get inside.
Look at an example, this is the most common internal class:

public class Product1 {
    class Design{
        private String name = "P30 pro";
        public String showName() {
            return name;
        }
    }

    class Content{
        private int i;
        Content(int value){
            i = value;
        }
        int value() {return i;}
    }
    public void show(int value) {
        Content c = new Content(value);
        Design d = new Design();
        System.out.println(d.showName());
        System.out.println(c.value());
    }
    public static void main(String[] args) {
        Product1 p = new Product1();
        p.show(6000);
    }
}

Note:
The above example shows the use of the most basic internal class, it is to define one or more classes on the inside of the inner periphery. It can be seen in use show () method and the general category, as there is no difference.

Further, any position outside the periphery of the non-static methods of the class, the class object can not be created directly inside the interior new class manner. Compilation errors will be reported.
Like this:
Here Insert Picture Description
there is this:
Here Insert Picture Description
If you want to create an internal class object outside the enclosing class non-static methods. How to do it?

Correct posture is this:
the increase in the peripheral two public methods class, returns the inner classes.

    public Design design() {
        return new Design();
    }
    public Content content(int value) {
        return new Content(value);
    }

Internal class object then acquires external object by the call method.

    public static void main(String[] args) {
        Product2 p = new Product2();
        p.show(6000);
        Product2.Content c1 = p.content(100);
        Product2.Design d1 = p.design();
    }

It is noteworthy that, other classes, they can not access directly outside to the inside of the outer class class object of the outer class.
Compilation errors will be reported. This is also in line with the definition of the inner class, that is, for the enclosing class services Well!

Features within the class

1 can be linked to an external class

When generating an internal class object, there is a link between this object and the periphery of the object made it, so all members can access the peripheral object, without any special conditions.

The following example is an example of "Java programming ideas" in the
first define a Selector Interface

public interface Selector {
    boolean end();
    Object current();
    void next();
}

Sequence then define a class, a class which defines an internal private.

public class Sequence {

    private Object[] items;
    private int next = 0;
    public Sequence(int size) {items = new Object[size];}
    public void add(Object x) {
        if(next < items.length) {
            items[next++] = x;
        }
    }
    private class SequenceSelector implements Selector{
        private int i = 0;
        public boolean end() {
            return i == items.length;
        }
        public Object current() {
            return items[i];
        }
        public void next() {
            if(i < items.length) {i++;}
        }
    }
    public Selector selector() {
        return new SequenceSelector();
    }
    public static void main(String[] args) {
        Sequence sequence = new Sequence(10);
        for(int i = 0; i < 10; i++) {
            sequence.add(Integer.toString(i));
        }
        Selector selector = sequence.selector();
        while(!selector.end()) {
            System.out.print(selector.current() + " ");
            selector.next();
        }
    }
}

Description:
see SequenceSelector an internal class, which end (), current () and next () uses both the private items peripheral class field.

2. Use .this and .new

.this Usage
If you need to generate a reference to an external class object, you can use the name behind the outer class followed by the dot and this.
As follows:

public class DotThis {
    void name() {System.out.println("name");}
    public class Inner{
        public DotThis outer() {
            return DotThis.this;
        }
    }
    public Inner inner() {return new Inner();}
    public static void main(String[] args) {
        DotThis dt = new DotThis();
        DotThis.Inner inner = dt.inner();
        inner.outer().name();
    }
}

Note DotThis.this just had the correct external class references. It does not create an external class object.

.new usage
can create an internal class object through the grammar, but note that you want to use to create an object outside the class.

public class DotNew {
    public class Inner{}
    public static void main(String[] args) {
        DotNew dn = new DotNew();
        DotNew.Inner dnInner = dn.new Inner();
    }
}

Internal class classification

1, a partial inner class

Definition of a class in a scope of the method or process. Referred to as inner classes locally.

public class Product3 {
    public Section section(String inputName) {
        class ASection implements Section{
            private String name;
            private ASection(String name) {
                this.name = name;
            }
            @Override
            public String hello() {
                return name + " say hello";
            }
        }
        return new ASection(inputName);
    }
    public static void main(String[] args) {
        Product3 p = new Product3();
        Section section = p.section("aaaaa");
        System.out.println(section.hello());
    }
}

ASection interface is to achieve a Section, Section interface code as follows:

public interface Section {
    String hello();
}

Description:

  • This inner class section () method, this method can not be accessed outside ASection class;
  • Methods inner classes are not allowed access modifier (public, private, protected);
  • Note that method returns a reference to Section, that there is upward transition.

Also can be defined on an inner class of one block to process, as the if statement block.

public class Product4 {

    public String check(boolean flag) {
        String checkId = null;
        if(flag) {
            class DetailCheck{
                private String id;
                private DetailCheck(String id) {
                    this.id = id;
                }
                String getId() {
                    return id;
                }
            }
            DetailCheck dc = new DetailCheck("1111");
            checkId = dc.getId();
        }
        return checkId;
    }
    public static void main(String[] args) {
        Product4 p = new Product4();
        System.out.println(p.check(true));
    }
}

Description:
DetailCheck internal class if block, so if its scope also within the scope of the statement block. Outside this range it is not available. For example, this will compile error:
Here Insert Picture Description

2, anonymous inner classes

Anonymous inner classes is actually a special method for an internal class (partial inner class). It is a special class that defines the interior thereof and incorporated in the one object creation. Internal class name does not appear statement by the class keyword, so that the "anonymous."
Look at the code sample:

public class Product5 {
    public Section section() {
        return new Section() {
            private String name = "hayli";
            @Override
            public String hello() {
                // TODO Auto-generated method stub
                return name + " haha";
            }
        };
    }
    public static void main(String[] args) {
        Product5 p = new Product5();
        p.section();
    }
}

Description:
Here Section may be an interface, but also the base class.

3, nested class (static inner classes)

Use the static keyword modified internal class called static inner classes, also known as nested classes.
The biggest difference between the class and the common internal nested classes are:

  • General internal class object implicitly holds a reference to an external class object that created it. The nested class to create objects and does not require an external class object.
  • You can not access the non-static nested external object from the object class.
  • Common internal classes can not have static data and static fields, and can not contain nested classes, nested classes but may comprise all of these things.
public class Product6 {
    private static int id = 100;
    private static class BSection implements Section{
        private String name = "bbbb";
        @Override
        public String hello() {
            return name + " hello";
        }
        // 只能访问外部类的静态数据或字段
        public int getId() {return id;}
        
        // 可以包含静态数据或方法
        static int x = 200;
        public static void test1() {}
        
        // 可以再嵌套一层
        static class BInner{
            private String name;
            static void test1() {System.out.println("inner ===");}
        }
    }
    public static void main(String[] args) {
        Section section = new BSection();
        section.hello();
    }

to sum up

This introduction of what is inside the class, several specific types Detailed most common definition of internal methods and inner classes of Java. Although the use of work opportunities within the class will not, but understand the most basic knowledge, inner classes really hit the wording in the project, but also to understand how it happens.

Scan code concerned about micro-channel public number: Two Notes battalion commander. Reply to "two battalion commander", can receive Java-related technical information.
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/happyone/p/11306419.html