1. Package
1.1 what is a package
- Packages are mainly classified to manage classes, similar to folders.
- Package syntax: package company domain name Technical name, all lowercase is recommended
- The build package statement must be on the first line
1.2 Guide Package
- Classes under the same package can be accessed directly, and classes under the same package can be referenced through the import package.
- If you need to import the same class, you can only import one package path on the class. If you need to reference the same class again in the class, you need to use the full path name: package name Class name ();
- Format: import package name Class name / import package name Class name
2. Permission modifier
Permission modifiers are used to modify the scope of member access. Permission modifiers can modify member variables, constructors, methods and internal classes.
The access scope of the permission modifier is as follows:
Modifier | Same class | Other classes in the same package | Different package subclasses | Unrelated classes under different packages |
---|---|---|---|---|
private | √ | |||
default | √ | √ | ||
protected | √ | √ | √ | |
public | √ | √ | √ | √ |
3. final and constants
3.1 fianl
fianl represents the final, which can modify variables, methods and classes
- Decorated variable: this variable can only be assigned once
- Modification method: this method is the final method and cannot be overridden
- Decoration class: this class is the final class and cannot be inherited
matters needing attention:
Modifier base type: cannot be changed after the first assignment.
Modified reference type: the reference address of the object cannot be changed, but the internal information of the address object can be modified.
Example:
public class FinalTest { public static final int num = 10; public static final Dog dog = new Dog("Erha", 10); public static void main(String[] args) { System.out.println(num); System.out.println(dog.getName()); // Program error // num = 20; dog.setName("Sanha"); System.out.println(dog.getName()); } }
3.2 constants
- Constants are variables decorated with public static final. They must be initialized and cannot be modified during execution.
- Constants are often used for file matching to improve code readability and facilitate later maintenance.
- Constant naming conventions: word names are capitalized, and multiple words are connected by underscores
Execution principle:
Replacement during compilation will replace all constants with literal values.
Replace with literal to improve the efficiency of execution.
4. Enumeration, interface and abstract class
4.1 enumeration (enum)
Enumeration is a special type of java to mark and classify information
Format:
Modifier enum Enumeration type { ... }
Example:
public enum Direction { UP("Up", 1), DOWN("down", 2), LEFT("towards the left", 3), RIGHT("towards the right", 4); private String name; private int flag; private Direction(String name, int flag) { this.name = name; this.flag = flag; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getFlag() { return flag; } public void setFlag(int flag) { this.flag = flag; } }
Enumerated characteristics:
- Enumeration classes inherit Java lang.Enum
- Enumeration class is the final class and cannot be inherited
- Constructors are private and cannot be instantiated
- By default, the first line of an enumeration class is the name of the object that writes the enumeration class
4.2 interface
The interface in java is a specification, which is similar to the interface in life. For example, the Type-C charging port on the market is a standard. Everyone designs it in this way, so it can be used for general use. Constrains consistent content and opens up custom implementations.
4.2. 1 Definition Format
Modifier interface Class name { ... }
4.2. 2 features
- Before JDK8, there can only be abstract methods and constants (described below)
- Interface cannot be instantiated
- All members in the interface are public. The default is to write or not
4.2. 3. Interface implementation
Format:
Modifier class class name implements interface class name 1, interface class name 2 {
...
}
- An interface is a class used to implement an interface, which is called an implementation class. It can also be understood as a subclass, using the keyword implements
- The interface can be implemented by a single or multiple
- A class implementation interface must override all abstract methods, otherwise the implementation class is an abstract class
- Inheritance and Implementation: ① classes can only inherit single; ② classes can implement multiple; ③ interfaces can implement multiple inheritance
4.2. 4. New specification of jdk8 rear interface
When the interface is defined, many subclasses are required to implement it. During later extension maintenance, new abstract methods need to be added. At this time, all implementation classes must be implemented, otherwise the program compilation fails. This leads to a lot of unnecessary workload. A new specification is added after JDK8.
Default method
Method definition format is similar to class member method, and the modifier must be default. This method requires an interface implementation class to call.
Example:
public interface Interface1 { void show(); default int add(int x, int y) { return x + y; } default int addNum(int x, int y) { return add(x, y); } }
public class InterfaceImpl implements Interface1 { @Override public void show() { System.out.println("Realized Interface1 of show method"); int a = addNum(1, 2); System.out.println("Call the default method of the abstract class:" + a); } public static void main(String[] args) { InterfaceImpl anInterface = new InterfaceImpl(); anInterface.show(); } }
- Static method
Static methods are decorated with public (default modifier), and static needs to be added to the method. Interface static methods must be called by the interface itself.
// Interface method, written in Interface1 interface static double getPI() { return 3.14; } // Execute interface static methods Interface1.getPI();
- Private method (after jdk1.9)
Methods must be decorated with private and can only be called by default and private methods in this class
Example:
private void go(){ System.out.println("Start executing interface private methods"); }
4.2. 5 precautions
- An interface cannot instantiate an object
- A class can implement multiple interfaces, and the static methods in multiple interfaces do not conflict
- A class inherits the parent class and implements multiple interfaces at the same time. If the interface and parent class have methods with the same name, the parent class is used by default
- A single class inherits multiple implementations
4.3 abstract class
In java, abstract means Abstract. If there are uncertain methods in a class that need to be implemented by different subclasses, you can use abstract to create an abstract class. You only need to define the method name without specific implementation. Abstract keywords can modify classes and methods.
// Abstract class format Modifier abstract Class name { } // Abstract method Modifier abstract Return type method name(parameter list ...);
// Example public abstract class AbstractTest { public abstract void show(); }
Characteristics and precautions of abstract classes:
- Abstract classes cannot be instantiated
- Class members and abstract classes have
- Abstract classes do not necessarily have abstract methods, and classes with abstract methods must be abstract classes
- Subclasses that inherit abstract classes must override abstract methods, otherwise this class is also an abstract class
- abstract cannot decorate member variables, constructors, and code blocks
Usage scenario:
An abstract method can be regarded as a template when most functions are the same, but only individual functions are unique. At this time, you can use the abstract class to let the user inherit the abstract class and override the method.
Advantages of abstract classes:
- Improve code reusability
- The methods to be implemented are defined as abstract methods, so that users only need to care about the functions to be implemented
- *
At the end of this chapter, it is used for personal learning and introduction to Xiaobai. Don't spray it! I hope you can praise the collection support!
Source code [GitHub] [code cloud]