Java object-oriented learning part 3

1. Static modifier

static means static, the basic concept is as follows:

Static category:

Generally, our classification is based on whether to use static modification. Divided into static variables (class variables), instance variables.

 

Comparison of static variables and instance variables: 

For comparison, as long as you remember something else, it will be easy to understand. Static variables have only one copy in the memory space.

 

scenes to be used:

 

Applications:

Note: static methods can only access static instances. Instance variables cannot be accessed.

12. Singleton mode

 

 Hungry Chinese model:

 Lazy model:

 Application scenario:

 

 

 main() method explanation:

 After adding static, it can be regarded as the entry point of the program.

13. Code blocks

Code blocks are information used to initialize classes and objects. According to the content of initialization, it is divided into static code block and non-static code block.

basic concept:

 

Use of code blocks: 

 

14. The order of attribute assignment in the class:

(Written test questions are often tested)

 

Second, the final modifier:

concept:

The final modifier means final and is used in classes, methods, and variables. In the method, the representative address cannot be changed. On a variable, it means that the variable content cannot be changed. In a class, it cannot be inherited.

And the static modifier learned earlier, it is more used in code blocks, and it is used more when assigning values ​​​​to attributes.

Classification of final modified variables: 

3. Abstract classes and methods

Abstraction means that there are only method signatures and no method bodies. Divided into abstract classes and abstract methods. Its concrete implementation mainly lies in overriding abstract methods. (And rewriting the attribute of the abstract class is called implement, not ovriding)

basic concept:

 Only used to decorate classes and methods. Cannot be used to modify attributes!

Specific use:

 

 Scope of action: (often tested)

32. Template method design pattern:

(TemplateMethod)

Looking at this template design pattern, it is actually a feature of abstract classes, which shows that abstract methods and non-abstract methods can be written in abstract classes. In the inherited subclass, only the abstract method needs to be rewritten.

Confuse::

 As mentioned earlier, for the instantiation of an abstract class, the method of the parent class must be completely rewritten by the subclass to realize the instantiation of the class. Then this template design pattern should not rewrite non-abstract methods of abstract classes. Will the direct instantiation report an error?

Subclasses can be instantiated directly! That is to say, not all the methods of the parent class are rewritten, only the abstract methods of the parent class are rewritten. Subclasses are runnable and are not considered abstract.

33. Interface (emphasis)

Interfaces are actually the same characteristics as abstract classes, but unlike abstract classes, they are not inheritance relationships, but implementation relationships! It is not the relationship between the subclass and the parent class, but the relationship between the interface (equivalent to the parent class) and the implementation class. Interfaces are also polymorphic. The difference between it and abstraction is that it has no constructor. The method of super and this cannot be used.

concept:

It means "can you", which is equivalent to the realization of a certain function!

 Sample model:

When declaring attributes, don’t forget to add static final; when declaring methods, don’t forget to add abstract.

 

 

Features of the interface:

Two ways to add anonymous objects: 

 

Interview question: Distinguish between abstract classes and interfaces

 

Combined use of inheritance and interfaces:

Three, four, JDK8 and JDK9 interface new features

Before JDK8, the modification of the method must be added with public abstract.

After JDK8, you can call the default method, static method (static).

Knowledge point 1: The static method declared by the interface can only be called by the interface. Cannot be called by its implementing class.

Knowledge point 2: The default method declared by the interface can be inherited by the implementation class. Equivalent to implementing the default method of the inherited interface of the class. There are overridden features.

Knowledge point 3: A class implements two interfaces, and these two interfaces have default methods with the same name and parameters. If the implementation class does not override these two default methods, an error will be reported. (interface's default method conflicts)

 Knowledge point 4: The subclass inherits the parent class and then implements the interface. But the parent class and interface have methods with the same name and parameters. By default, the method of the parent class is called. Class priority principle.

Knowledge point 5: Call the overridden method in the interface in the implementation class. (interface.super.method())

Fourth, the inner class

An inner class is actually another class created in a class. Its relationship with the outer class is this relationship, not inheritance or implementation. The role of the inner class is to realize the development principle of "high cohesion and low coupling".

basic concept:

Classification:

Need to master:

Create an instance of the inner class and call the structure of the outer class in the inner class.

Understanding of member inner classes:

 

Create an instance of an inner class (exam): 

There are two types of creation methods, which are divided into creating instances of static inner classes (new class. class) and creating instances of non-static inner classes (class object. new. class).

Three anonymous methods of interface inner classes:

This Comparable is a class that comes with jdk.

 

 

Internal class passing value (common test)

Five, enumeration class

The enumeration class is also a kind of class. The difference between it and the class modified by the class is that the enumeration cannot create its objects at will in the test. Enumeration is equivalent to creating an instance in this class.

concept:

 

Implementation of the enumeration class:

Before JDK5, the implementation could not create its objects outside the class (that is, the way to implement enumeration):

Step 1: Declare instance variables (declare attributes) of objects of the current class. (not visible outside)

The second step: privatize the constructor of the class. Objects cannot be created externally.

Step 3: Create an instance of the class. public static final. Declare a constant object. (visible outside)

Use the enumeration class directly after jdk5:

How to use the enumeration class:

Mainly in the usual examples, how to use it after we have created the enumeration class. Several ways are introduced below, the main thing to remember is the enumeration type [] values(). It returns an array of that object. 

 Test instance:

The enum class implements the interface:

Realization corresponds to the inheritance relationship

Enumerated type:

It is divided into enumerations without parameters. In this case, there is no constructor, and the tostring method is not overridden.

An enumeration with parameters, in this case, has a constructor, and the tostring method needs to be rewritten as needed.

 

6. Annotation

 Annotations start from jdk5.0 and exist as @ annotation names. What it does is simplicity. Can be used instead of some configuration files. It is also possible to specify the scope of the classes used.

basic concept:

 

 Basic annotations:

Commonly used annotations are the first two.

Meta annotations:

In fact, it is annotating the existing annotations. Used to specify the scope and format of this annotation.

6.1. Unit testing (important)

Unit testing is essentially white box testing. Like before we used CRUD, each class must write a test class. This is more troublesome, and it is not necessary to write a test for each class through unit testing. You can put the classes and methods that need to be tested together. Each can be tested individually. And it can also test different methods in a class separately. 

basic concept:

The basic steps:

Example: @Test

Simplified operations: 

7. Understanding of packaging

The wrapper class is swapper. By learning wrapper classes, we can convert primitive data types to reference data types when using them. Because some basic data types cannot use some object-oriented methods. It is very inconvenient. For example below:

Necessary to exist:

 

Core requirements:

Conversion between primitive data and wrapper classes.

Case operation:

 

 After jdk5:

Versions after jdk5 start using autoboxing and autounboxing. It is faster than the method used before, but the essence is to use the basic method. 

7.1, String data conversion:

basic concept:

 

Convert each other:

For the String type, the wrapper class, and the conversion of basic data types are shown in the figure below. In fact, after jdk5, it is equivalent to only the conversion between the String class and (String class and packaging class).

 

Autoboxing note:

The way autoboxing should only work with wrapper classes and primitive datatype classes. For the String class, its essence is an alternative wrapper class. It is converted by String.valueOf( i ). And the basic wrapper class learned before is also Interger.valueOf( i ).

The difference is that String is converted to the basic class. The way to use is parseInt( String ). The wrapper class is converted to the base class using intValue( Integer ).

 

Case operation:

I learned to use arrays to store data earlier, but this time I use vectors to store them. Their pros and cons:

 Steps:

 

Differences between using autoboxing and boxing: 

 v.addElement(Object obj); Equivalent to Object obj = intScore. (autoboxed). This method is used the most.

Pen questions:

 

8. IDEA shortcut keys and breakpoints.

 

Guess you like

Origin blog.csdn.net/qq_55928086/article/details/132125657