Home  >  Article  >  Java  >  Object-oriented (inner class, static, package, access modifier, final)

Object-oriented (inner class, static, package, access modifier, final)

巴扎黑
巴扎黑Original
2017-06-26 11:15:101243browse

final
final is the final modifier, which can modify classes, member methods, and variables.
Final modified classes cannot be inherited.
Final modified methods cannot be overridden.
Final modified variables cannot be assigned again and become constants.

The reference data type variable modified by final can modify the attribute content in the object, but the address value cannot be changed
The member variable modified by final cannot use the default value, which is meaningless. The assignment must be completed before creating the object. .
Naming rules for constants All letters are capitalized. Multiple words are connected with _

static
Static modifier, the content modified by static belongs to the class and does not belong to an object , multiple objects share this member
Members modified with static can be directly accessed using the class name. It is recommended to use it like this:
Class name. Static method name (parameter);
Class name. Static attribute name ;
Static modified members can only directly access statically modified members, this and super cannot appear, because classes are superior to objects.
Static Notes
Static modified members are loaded with the class Loading takes precedence over object existence.
Static can only access static, and cannot use this/super

Static constant:static final

Local code block: limit the scope of the variable
Member code block: and Called construction code block, it will be executed as long as the object is created
Static code block: loaded as the class is loaded, the class is only loaded once and the code is loaded once
Loading of class: Create object, access static member variables, access static Method
Static code block>Member code block>Constructor method

The concept of internal class
A class defined inside a class is called an internal class. It is divided into: Member inner class and local inner class

Anonymous inner class
is a kind of local inner class
Anonymous inner class accomplishes two things:
1: Defined Anonymous subclass of a type
2: After defining the class, the object of the class is created immediately

Purpose: To create a subclass object of a certain class
Format: new parent class/interface () {
Overriding method
};

Package
The basic way for software to organize files, used to organize files Classes with the same functions are placed under the same package to facilitate management
Use package at the front of the class to define the package where the class is located. Note: the declared package must be consistent with the folder where the file is located
Package access: (The prerequisite class is modified with public)
Under the same package, you can access it at will
Under different packages:
You can use the full name directly
For convenience, you can choose to import the package and then use the class name directly itself, without adding the package name. After package, use import before class to import the class
If it is a class under the lang package, you can directly use

Access permissions
All four permissions in the same class can be accessed
Irrelevant classes under the same package can only be accessed by private
Irrelevant classes under different packages can only be accessed by public
Subclasses under different packages cannot be accessed by default and private access

The above is the detailed content of Object-oriented (inner class, static, package, access modifier, final). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn