Java advanced anonymous inner class, access modifier, package


1. Formal parameters and return values

  • (1) Formal parameter:
    class name: the object of this class is needed.
    Abstract class name: the subclass object of this class is needed.
    Interface name: the implementation class object of this interface is needed.

  • (2) Return value type:
    Class name: What is returned is the object of this class.
    Abstract class name: What is returned is the subclass object of this class.
    Interface name: What is returned is the object of the implementation class of this interface.

  • (3) Chain programming
    object. Method 1(). Method 2()... Method n();

    This usage: In fact, after the method 1() is called, there should be an object; after the
    method 2() is called, an object should be returned.
    After the method n() is called, it may or may not be an object.

Code demo

class Link{
    
    
    public String Test(){
    
    
        return "helloworld";
    }
}
public class LinkCodeTest {
    
    
    public static void main(String[] args) {
    
    
        Link link=new Link();
        System.out.println(link.Test().length());
      
    }
}

Insert picture description here

Two, package

1 package

  • (1) Actually it is a folder

  • (2) Function:

    • A: distinguish the classes with the same name
    • B: Classification management of classes
      • a: According to function
      • b: According to the module
  • (3) Package definition
    package package name;

      多级包用.分开。
    
  • (4) Matters needing attention:

    • A: The package statement must be the first valid statement in the file
    • B: There can only be one package in a java file
    • C: If there is no package, the default is no package name
  • (5) Compile and run with package
    A: manual type
    B: automatic type
    javac -d. HelloWorld.java

2. Guide package

  • (1) We have used a packaged class many times, which is very troublesome. At this time, Java provides a keyword import.
  • (2) Format:
    import package name...class name;
    another:
    import package name...*; (not recommended)
  • (3) The order of package, import, class
    package> import> class

The following two ways of writing are OK, the first one is recommended

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.*;

Three, permission modifier

1. Permission modifier:

Insert picture description here
Note: Only one of these four permission modifiers can appear at any time.

2. Common permission modifiers

  • (1) Classification:

    • Permission modifier: private, default, protected, public
    • Status modifier: static, final
    • Abstract modifier: abstract
  • (2) Modification of common classes and their composition

    • Class:
      default, public, final, abstract

        常用的:public
      
    • Member variables:
      private, default, protected, public, static, final

        常用的:private
      
    • Construction method:
      private, default, protected, public

        常用的:public
      
    • Member methods:
      private, default, protected, public, static, final, abstract

        常用的:public
      
  • (3) The other more common ones:

    public static final int X = 10;
    public static void show() {}
    public final void show() {}
    public abstract void show();

  • Note: The default modifier of the method in the interface is public abstract, and the constant is public static final.
    Only constants can be defined, but variables cannot be defined. Assign values ​​when defining.

interface Inter<T>{
    
    
   public static final int a=0;
    void sum(T a);
}

Four, internal category

  • (1) When a class is defined inside another class, the class is called an inner class.

       举例:把类B定义在类A中,类B就被称为内部类。
    
  • (2) Access rules for internal classes

      A:可以直接访问外部类的成员,包括私有
      B:外部类要想访问内部类成员,必须创建对象
    
  • (3) Classification of internal classes

      A:成员内部类
      B:局部内部类
    
  • (4) Member inner class

      A:private 为了数据的安全性
      B:static 为了访问的方便性
      
      成员内部类不是静态的:
      	外部类名.内部类名 对象名 = new 外部类名.new 内部类名();
      成员内部类是静态的:
      	外部类名.内部类名 对象名 = new 外部类名.内部类名();
    
  • (5)
    Interview questions for members' internal classes require code output of 30, 20, 10

class Outer {
    
    
			public int num = 10;
			
			class Inner {
    
    
				public int num = 20;
				
				public viod show() {
    
    
					int num  = 30;
					
					System.out.println(num);
					System.out.println(this.num);
					System.out.println(Outer.this.num);
				}
			}
		}
  • (6) Local inner class

    • A: Local internal classes must add final modification to access local variables.
      B: Why?
      Because the local variables disappear after they are used, but the data in the heap memory does not disappear immediately.
      Therefore, the variable is still used in the heap memory, and the amount of change is gone.
      In order to make the value still exist, add final modification.
      Through the decompilation tool, we have seen that after adding final, the heap memory directly stores the value instead of the variable name.
  • (7) Anonymous inner class

    • A: It is a simplified form of the local inner class
    • B: Premise
      There is a class or interface
    • C: Format:
      new class name or interface name () { rewrite method; }

    • D: Essence:
      In fact, it is an anonymous object of a subclass that inherits the class or implements the interface
  • (8) The use of anonymous inner classes in development. When
    we develop, we will see abstract classes or interfaces as parameters.
    At this time, we know that what we actually need is a subclass object.
    If the method is called only once, we can simplify the format of anonymous inner classes.

      interface Person {
      	public abstract void study();
      }
      
      class PersonDemo {
      	public void method(Person p) {
      		p.study();
      	}
      }
      
      class PersonTest {
      	public static void main(String[] args) {
      		PersonDemo pd = new PersonDemo();
      		pd.method(new Person() {
      			public void study() {
      				System.out.println("好好学习,天天向上");
      			}
      		});
      	}
      }
    
  • (9) Anonymous internal interview questions

interface Inter {
    
    
			void show();
		}
		
		class Outer {
    
    
			//补齐代码
			public static Inter method() {
    
    
				return new Inter() {
    
    
					public void show() {
    
    
						System.out.println("HelloWorld");
					}	
				};
			}
		}
		
		class OuterDemo {
    
    
			public static void main(String[] args) {
    
    
				Outer.method().show(); //"HelloWorld"
			}
		}

Java introductory basic learning (1)
Java introductory basic learning (2)
Java introductory basic learning (3)
Java introductory basic learning (4)
Java introductory basic learning (5)
Java introductory basic learning (6)
Java introductory basic learning (7)
java Introductory Basic Learning (8)
Common Objects of Advanced Java (1)
Common Objects of Advanced Java (2)
Bubble Sorting of
Java Advanced Java Selection and Sorting of
Advanced Java Advanced Object-Oriented (Package) of
Java Advanced Object-oriented (code block, inheritance)
Java advanced object-oriented (polymorphism, abstraction, interface)

Guess you like

Origin blog.csdn.net/qq_45798550/article/details/108179468