07-Java package, construction method, static keyword

1. Package

Encapsulation is the core idea of ​​object-oriented programming. Encapsulate the properties and behavior of the object, and its carrier is the class. The class usually hides its implementation details from the client. This is the idea of ​​encapsulation.

Benefits of encapsulation:

  • Hidden implementation details, convenient for users
  • Safe, you can control the visible range

Encapsulation is achieved through permission modifiers:

Modifier This category This package Subclasses of other packages Anywhere
private × × ×
default × ×
protected ×
public

The permission modifier can be modified:

  • Class (external class): public and default
  • Attributes: 4 types
  • Method: 4 kinds
  • Construction method: 4 kinds

Encapsulation of attributes:

(1) Attribute privatization (private)

(2) Provide public (public) getter/setter methods

2. Construction method

The constructor, also called the constructor, is one of the members of the class.

format:

【修饰符】 class 类名{
    
    
    【修饰符】 类名(){
    
    //无参构造
        
    }
    【修饰符】 类名(形参列表){
    
    //有参构造
        
    }
}

例:
 class Test{
    
    
     int a;
     public Test(){
    
    
         
     }
     public Test(int a){
    
    
         this.a=a;
     }
 }

The role of the construction method:

  • Use with new to create objects
  • Initialize object properties

Characteristics of the construction method

  • All classes have constructors
  • The name of the constructor must be the same as the class name
  • If a class does not show a construction method, then the system will default to a no-parameter construction
  • If a class explicitly/explicitly declares the constructor, then the system will no longer automatically add the default no-parameter construction, if necessary, then you need to add it manually
  • The construction method can be overloaded
  • The constructor does not have a return value type (Nor write void, you can write return;)

3.static keyword

​ When we write a class, we are actually describing the properties and behaviors of its objects, and no substantial objects are produced. Only through the new keyword can objects be produced, and then the system will allocate memory space to the objects. The method can be called externally. We sometimes hope that regardless of whether objects are generated or how many objects are generated, there is only one copy of certain specific data in the memory space. For example, all Chinese people have a country name, and every Chinese person shares this Country name, it is not necessary to assign a variable to represent the country name in each instance of Chinese. So you need to use static.

static: static, can modify methods, attributes, code blocks, internal classes.

Static modified member variables: generally called static variables or class variables

  • The value of a static variable is shared by all objects of the class
  • Static variables are stored in the method area
  • The get/set methods corresponding to static variables are also static
  • Static variables are initialized with the initialization of the class, which takes precedence over the creation of instance objects
  • When the access permission allows, you can directly call the class name and variable without creating the object

Static modified member method: class method or static method

  • Static modified methods, when the access permission allows, can be accessed through "class name. method"
  • Static modified methods cannot be overridden, because static methods are class methods, belong to the class, and have nothing to do with the object
  • There cannot be this and super inside a static method.
    • If there are local variables and class variables in the static method with the same name, you can use "this class name.xxx"
    • If you want to access the static members of the parent class in the static method, you can use "parent class name.xxx"
  • Non-static properties and methods cannot be accessed inside static methods.

static modified code block: static code block

3.1 Constructing code blocks

Non-static code block

Syntax format:

【修饰符】 class 类名{
    
    
    {
    
    
        构造代码块
    }
}

Construct a code block:

  • In the process of creation, assign values ​​to object properties to assist in the process of instance initialization
  • Will be executed every time an object is created
  • Prior to execution of the constructor
  • When creating a subclass object, you need to initialize the properties inherited from the parent class first, so the building block and constructor of the parent class will be executed first

3.2 Static code block

Syntax format:

【修饰符】 class 类名{
    
    
    static{
    
    
        静态代码块;
    }
}

Static code block:

  • Assist in class initialization and assign values ​​to class variables.
  • Initialized with the initialization of the class, only executed once
  • If the subclass is initialized, its parent class is not initialized, the parent class will be initialized first
  • Non-static members (attributes, methods, inner classes) cannot be accessed in static code blocks
  • This and super keywords cannot be used in static code blocks
    • If you have duplicate names and need to access static members of this class or parent class, you can use "this class name.xx" and "parent class name.xx".

3.3 Static and non-static access principles

(1) In the same class:

  • Static members can not access non-static member
  • Static members can be accessed in non- static members

(2) Among different categories:

  • Access to non-static members of other classes must use the "object. non-static member" format
  • To access the static members of other classes, it is recommended to use the format of "class name . static member" **, you can also use "object. static member"

3.4 Initialization sequence

  • The compiler will merge the contents of "explicit initialization of class variables" and "static code block" into one class initialization method in order. So the initialization process of the class is the execution method, that is, the explicit initialization of the class variable and the static code block must be executed together, and because the class initialization only needs to be done once, there is only one, and it will only be executed once.
  • When executing the method of the subclass, if the parent class is not initialized, the method of the parent class will be executed first.
  • The compiler will merge the contents of "display initialization of non-static variables" and "non-static code blocks" into each constructor in order to form an instance initialization method. There are several constructors, and there are several methods. . And the contents of "Non-static variable display initialization" and "Non-static code block" are above, and the original code in the constructor is below. Therefore, the initialization of the object, that is, the method of executing it, that is, the display initialization of non-static variables, the non-static code block and the code in the constructor are executed together, and the display initialization of non-static variables and the content of the non-static code block precede The code execution of the constructor.
  • When the method of the subclass is executed, the method of the parent class must be called first, because the properties inherited from the parent class must be initialized first. (Super([list of actual parameters]))
  • Both class variables and instance variables have default values, and they have default values ​​before they are used or initialized.

Sample code:

class Ba{
    
    
	private String str = assign();
	{
    
    
		System.out.println("(1)父类的非静态代码块");
	}
	public Ba(){
    
    
		System.out.println("(2)父类的无参构造");
	}
	public String assign(){
    
    
		System.out.println("(3)父类的assign()");
		return "ba";
	}
}
class Er extends Ba{
    
    
	private String str = assign();
	{
    
    
		System.out.println("(4)子类的非静态代码块");
	}
	public Er(){
    
    
		//super()  ==>调用父类的实例初始化方法,而且它在子类实例初始化方法的首行
		System.out.println("(5)子类的无参构造");
	}
	
	public String assign(){
    
    
		System.out.println("(6)子类的assign()");
		return "er";
	}
}
class Test{
    
    
    public static void main(String[] args){
    
    
        new Er();//612645
    }
}

Illustration:
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46988935/article/details/110198297