Inner class and outer class

The first blog I opened last time said that I should insist on a daily update, but I didn't do it because of my own reasons. But starting from today, we must persist, no matter how late, no matter what we find, we must be a little better today than yesterday.

Little Bai, if there is any mistake, please point it out to help improve it.

Inner class

In short, I read a lot of blogs and said that after having internal classes, Java single inheritance can be solved. I don’t understand this very much now. I will continue to modify the ones I understand more

Member internal class (class file generated after compilation, name: external class name $ internal class name.class)

1 I think it can be regarded as a member variable, which can be modified with public, protected, private "none", but because it is called a member inner class, not a static inner class, so I cannot use static modification. It can also be modified with final and abstract, just like member variables.

 2 You can directly call the methods and variables of the external class that contains it (including private members and static members), and there can be variables and methods with the same name as the external class.

3 If you access a member of the same name of an external class, use external class.this. member variable/method

4 To create a member inner class, you must first create an outer class.

5 Variables in internal classes cannot be modified by static, and everything else can

6 In the inner class to access the instance variable of the outer class with the same name as the inner class, use the outer class name.this.variable name

Code can be found in every feature inside, see code

See the code: https://github.com/dht12/CSDN/blob/master/InnerClass%201

Note : Add one to account for extra things, I also found out that it took so long to learn

1 You can no longer use public, private, "none" and modification in the method. You can't use static, abstract modification, but you can use final modification.

I think it may be because the variables in the method can only be used when the method itself is called. They are local variables and can only be used in the method itself, so there is no need for those modifiers.

2 In the class, "private String name" can not define this kind of global variable, use it in the method, but can not give it a second assignment in the class, because it is in the class. It can be assigned in the method, but the initial value can be assigned                                                           

public String name="dht";//Compile without error

public String name;

name="dht";//Compile will report an error

3abstrack can not modify variables, because there is no meaning! ! ! ! ! ! ! ! ! ! (I must be an idiot) 

4Access permission modifiers: public, protected, "none", private,

Local inner class

Locals are like the characteristics of local variables. It is an internal class defined in a method/block. Access modifiers and static are not allowed, but fianl abstrack can be used

1 You can access the local variables of the external class (that is, the variables in the method), but the variables must be final

I didn’t write the code, and I felt troublesome and useless

 Anonymous inner class

Anonymity can be regarded as a kind of local inner class. (It has all the characteristics of a local inner class)

Thread, using anonymous inner classes

new Thread(new Runnable() {
    @Override
    public void run() {
        for(int i=10;i<20;i++){
            System.out.println("a"+i);
        }
    }
}).start();

1 As the name implies, the anonymous inner class has no name, so it cannot define a constructor

2 There can be no static modified variables and methods.

3 Must inherit/implement a class/interface, but cannot have both, it must be behind new

4 It cannot be abstract (this is different from a local inner class), because it must implement all abstract methods of the inherited class/implemented interface

The code will be added later

Static inner class

It is decorated with static and does not depend on external classes when created

supplement

j constructor ava modified with the modifier can only access (public, protected, private), can not be used fianl, synchronized, static, native, abstract modification

My understanding: The construction method is generally called when the class is instantiated, and the class is used to modify various variables, so it cannot be final

                   Synchronized is generally locked on the thread, the object is not meaningful

                   Static is called when the class is loaded. If the class is not instantiated, but it is called when it is loaded, the memory consumption will be large.

                  Native is a native method, abstract is an abstract class, abstract class cannot be instantiated

I feel that I wrote a very good article about this,

https://blog.csdn.net/sinat_34344123/article/details/81942427

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_40728070/article/details/90757376