JavaSE basics-(10) object-oriented permission modifiers and anonymous inner classes

table of Contents

One, the package in java

1.1 Overview and function of the package keyword

1.2 Definition of package and matters needing attention

1.3 Access between classes under different packages

Two, common modifiers in java

2.1 Modifiers

Class 2.2

2.3 Member variables

2.4 Construction method

2.5 Member method

Three, internal class

3.1 Overview of internal classes

3.2 Features of internal class access

3.3 Private inner class and static inner class

3.4 Interview questions for internal members

3.5 Definition and use of anonymous inner classes

3.6 Anonymous inner class overrides multiple method calls

3.7 Anonymous internal interview questions


 

One, the package in java

1.1 Overview and function of the package keyword

In the process of learning java, we will encounter a variety of classes. If there are too many classes, it is difficult for us to manage.

The function of the package is to classify and store the bytecode (.class), and the package is actually a folder.

 

1.2 Definition of package and matters needing attention

The package definition uses the package keyword, the format is

package 包名
多级包用.分开 例:package core.alg.de

Notes on defining packages

  • The package statement must be Chen Xu's first executable code
  • There can only be one package statement in a java file
  • If there is no package, it means no package name by default

 

1.3 Access between classes under different packages

We first create a new math tool class Mathtool under the project, and then put this class under the com/tz/ file path,

Then implement a simple function add for adding integer numbers, and define the class under the package com.tz

package com.tz;
public class Mathtool {
    public static int add(int a,int b){
        return a+b;
    }
}

Then we use the import statement in the main class to call the add function of the package,

import com.tz.Mathtool;
public class Main{
    public static void main(String args[]){
        System.out.println(Mathtool.add(1,1));
    }
}

Then we look at the output result:

 You can see that the access between classes under different packages can be implemented through import statements.

We need to pay attention to the following when using import:

import com.tz.*

When we use the "*" symbol, the program will search for all matching packages in the com/tz/ path. Although it is easy to use this symbol, if there are more classes in the package during the development process, then this method will have a search The efficiency of the problem, so generally use this method of precise import class name.

 

Two, common modifiers in java

2.1 Modifiers

  • Permission modifier: private (default), protected, public
  • Status modifier: static, final
  • Abstract modifier: abstract

Class 2.2

  • Permission modifier: default modifier, public (private classes cannot be called from outside, so private modification is generally not required)
  • Status modifier: final
  • Abstract modifier: abstract

The public modifier is frequently used in classes.

2.3 Member variables

  • Permission modifier: private (default), protected, public
  • Status modifier: static, final

Private modifiers are frequently used in member variables.

2.4 Construction method

  • Permission modifier: private (default), protected, public

The most commonly used modifier in the construction method is the public modifier.

2.5 Member method

  • Permission modifier: private (default), protected, public
  • Status modifier: static, final
  • Abstract modifier: abstract

The most commonly used modifier in member methods is the public modifier.

 

Three, internal class

3.1 Overview of internal classes

The inner class is the class defined in the class, and the relative outer class is the outer class.

 

3.2 Features of internal class access

  • The inner class can directly access the members of the outer class, including private members
  • To access the members of the inner class, an external class must create an object:
外部类名.内部类名 对象名=外部类对象.内部类对象

We use a piece of code to experience the use of internal classes:

public class InnerOuter {
    public static void main(String []args){
        OuterClass.InnerClass i=new OuterClass().new InnerClass();
        i.InnerPrint();
    }
}

class OuterClass{
    private String stirng="private string";
    class InnerClass{
        public void InnerPrint(){
            System.out.println(stirng);
        }
    }
}

See the output as follows:

 

3.3 Private inner class and static inner class

If we privatize the inner class, it cannot be accessed in other classes, and we must access the inner class through a method of the outer class to perform certain operations on the inner class

class OuterClass{
    private String stirng="private string";
    private class InnerClass{
        public void InnerPrint(){
            System.out.println(stirng);
        }
    }
    
    public void printInner(){
        InnerClass i=new InnerClass();
        i.InnerPrint();
    }
}

If this inner class is static, then we can access the non-static methods of the static inner class through the following operations

OuterClass.InnerClass oi=new OuterClass.InnerClass();
oi.InnerPrint();

If you ask about the static method of the static inner class, you can directly use the class name and method name to call

OuterClass.InnerClass.staticPrint();

 

3.4 Interview questions for internal members

public class InnerOuter {
    public static void main(String []args){
        OuterClass.InnerClass oi=new OuterClass().new InnerClass();
        oi.show();
    }
}
//使用已知的变量,在控制台输出30,20,10
class OuterClass{
    public int num=10;
    class InnerClass{
        public int num=20;
        public void show(){
            int num=30;
            System.out.println(?);
            System.out.println(?);
            System.out.println(?);
        }
    }
}

The first one is relatively simple. You can directly access the num variable in the show function and fill in num.

The second one can access the num variable of the inner class through the this pointer, fill in this.num,

The third is to access the num variable through the external class name plus this pointer, and fill in OuterClass.this.num.

Through this interview question, we know that the inner class can get the members of the outer class, which can be accessed through the outer class name.this. member name.

 

3.5 Definition and use of anonymous inner classes

An anonymous inner class is a simplified way of writing an inner class, that is, an inner class without a name.

When using anonymous inner classes, a class or interface needs to exist. This class can be a concrete class or an abstract class.

The format for defining an anonymous inner class is as follows:

new 类名or接口名(){
    重写方法;
};

Let's take an example to see how to use anonymous inner classes:

public class InnerOuter {
    public static void main(String []args){
        Outer o=new Outer();
        o.method();
    }
}

interface Inter{
    public void print();
}

class Outer{
    public void method() {
        new Inter() {
            public void print() {
                System.out.println("匿名内部类");
            }
        }.print();
    }
}

We are in the method of the class, directly use the new keyword to implement a method that needs to be rewritten in the class or interface,

And call the rewritten method after the new statement, the results of the operation are as follows:

 

3.6 Anonymous inner class overrides multiple method calls

When we need to call multiple methods of an anonymous inner class, we can use the parent class reference to point to the subclass object to achieve

public class InnerOuter {
    public static void main(String []args){
        Outer o=new Outer();
        o.method();
    }
}

interface Inter{
    public void print1();
    public void print2();
}

class Outer{
    public void method() {
        Inter i=new Inter() {
            public void print1() {
                System.out.println("匿名内部类1");
            }
            public void print2() {
                System.out.println("匿名内部类2");
            }
        };
        i.print1();
        i.print2();
    }
}

You can see the output

But when we encounter multiple functions that need to rewrite interfaces or classes, we generally don't need to use anonymous inner classes, just use inner classes directly.

Anonymous inner classes are often passed as parameters in development. Let's experience its role through an example:

public class InnerOuter {
    public static void main(String []args){
        Outer outer=new Outer();
        outer.method(new Inter(){
            public void print() {
                System.out.println("print");
            }
        });
    }
}
abstract class Inter{
    public void print(){};
}

class Outer{
    public void method(Inter i) {
        i.print();
    }
}

You can see that the anonymous inner class is treated as an object, passed in the parameters, we can save some time in development,

No longer need to inherit the abstract class and use the parent class reference to point to the subclass object, and use the anonymous inner class directly in the parameter.

 

3.7 Anonymous internal interview questions

It is required to fill in the code at the specified location and output "HelloWorld" in the console

public class InnerOuter {
    public static void main(String []args){
        Outer.method().print();
    }
}

interface Inter{
    void print();
}

class Outer{
    //请输入你的代码
}

In the main function, we can see that the method method is directly called by the class name, so method is a static method.

Then call the print method, which proves that the return value of the method method is an object, and this object can call the method print of the interface Inter.

Then the return value type is the Inter object, and then when we return, we can return an Inter object,

Next, we use the anonymous inner class to create the Inter object and implement the print method.

class Outer{
    //请输入你的代码
    public static Inter method(){
        return new Inter() {
            public void print() {
                System.out.println("HelloWorld");
            }
        };
    }
}

The output result is

In this interview question, it is more important to find that you can continue to call the method after calling the method, so the return value after calling the method must be an object.

Guess you like

Origin blog.csdn.net/weixin_39478524/article/details/112268560