Java object-oriented learning

Since the knowledge points of classes and objects are relatively difficult, and the knowledge points involved are very wide and complicated, here, they are learned as a single knowledge point. I will learn this knowledge point through different videos, different materials, and according to different characteristics.

1. Understand what are classes and objects.

A class is a template for a class of objects, which defines the properties and methods that a class of objects have.

See the figure below: Combining the knowledge points of stack and heap, user4 is stored in the stack, and new Class(~) is stored in the heap. 

Procedural and Object Oriented:

Constructor (construction method):

In general, attributes of a class are also called member variables. The scope of the attribute is the entire class body. When defining member variables, if there is no alignment initialization, Java will use the default value. For example: o, null (null is generally the default value of the object.)

And how to initialize the properties in the class? Use constructors!

concept:

 Some notes on creating constructors:

For example: look at the second picture, double x, y; it is equivalent to creating two spaces. And named x, y.

Compare Getter and Setter methods with constructor methods:

JVM memory analysis:

The memory of the Java virtual machine can be simply divided into three parts:

Virtual machine stack (JVM Stack), heap, and method area.

Their introduction is shown in the figure below:

Process memory analysis of program execution (emphasis):

The knowledge points in this section summarize the knowledge points learned earlier. Through the following example, it is a good explanation of the memory changes of various parts during program execution. !

Summary: (Need to remember)

Each object of the same class has different member variables to store space.

Every object of the same class shares the methods of that class.

Attributes in the class (only consider instance variables) assignment process:

This is actually the difference between initialization and instantiation. Attributes in a class are called instance variables, also called member variables.

Parameter passing mechanism:

Divided into two pass values. However, what they have in common is that all passing by value is "passing by value".

different places:

Moreover, if the reference type parameter is passed, the original parameter object will point to a null space. This involves the Gc mechanism. (That is, the garbage collection mechanism.)

Garbage collection mechanism:

Java's memory management, to a large extent, is actually the management of object instances in the heap. This includes the allocation and deallocation of object space. (Attention! It must be the allocation of object space! It is not initialization or creation. Creation means, for example, a Person class. Then creating an object is: Person user. Person itself has created a space, and user is the name of this space. In the previous understanding of variables, the example of the parking space explained the name very well.)

Interview question: What is the difference between initialization and instantiation?

memory leak:

The following three situations are most likely to cause memory leaks:

 In fact, the latter type, or including the second type, may cause memory leaks if the life cycle is too long, especially for processes with unclosed connections.

static和this:

The essence of this is: the address of the created object. (In fact, it is the address of the current object.)

this cannot be used in static methods.

Static declared member variables are static members, also known as class variables. (The life cycle of class variables is the same as that of classes)

Features: It is a common variable of this class, shared by all instances.

           For all objects of this class, there is only one static member variable, which is shared by all objects of this class.

           It does not need to be instantiated, it is called directly with the class name.

Non-static properties and methods cannot be placed in static methods.

Constructors are used to initialize objects.

variable:

  Variables are generally divided into local variables (stored on the stack), member variables (also called instance variables, stored on the heap) and static variables (also called class variables, stored on the stack).

Package mechanism:

The package mechanism solves the problem of class duplication. Packages are to classes what folders are to files.

Use import to import packages. When using import com.util.* need to pay attention: the use of * will reduce the compilation speed, but will not reduce the running speed.

Question: It can also import static properties. Java.lang packages can be used directly without importing.

Two, the three major characteristics of object-oriented programming 

Object-oriented programming has three characteristics: inheritance, encapsulation, and polymorphism.

Inheritance extends:

     There is no multiple inheritance for classes in Java, but multiple inheritance for interfaces.

     The subclass inherits the parent class, and can get all the properties and methods of the parent class (private ones are not allowed)

     If the defined class does not call extends, its parent class is: java.lang.Object.

instanceof operator:

   It is a binary operator for Boolean types with objects on the left and classes on the right. Return True when the object on the left is an object created by the class on the right.

Method override:

   Just three points. Can't remember to look it up myself. This has been said over and over again.

final keyword:

Inheritance and Composition:

The code of the defined class can be easily reused through inheritance. Of course, the code can also be reused in the way of combination.

Composition, in fact, is to create an object instantiation of another class in one class, and add this object in the test class. As shown below:

 Summary: Combination is more flexible, while inheritance has only one parent class, but combination can have multiple attributes, and each has its own advantages. OB

Encapsulation:

Why do you need to learn encapsulation?

 What is encapsulation:

 

How to achieve encapsulation:

In fact, it is defined using permission modifiers.

Example using encapsulation:

 

Combination also has an application scenario, which is used in the relationship of composition, for example, creating customer classes and account classes. These two classes are independent. How to make customers and account classes related to each other. Knowledge points from design to combination:

Permission modifiers:

  When setting the properties of the class as private, the properties of the class can only be accessed through the setter method in the test class.

  Priority: private> default (no modifier by default) > protected > public

  Class: only public and default decorations can be used.

  Internal members of the class: 4 permissions can be used for modification.

In JSP, it reflects encapsulation:

For example, to connect to the database, the POJO class is called JavaBean.

Object class:

It is the root base class for all Java classes. All properties and methods except constructors are inherited. However, not all classes can be used.

toString():

Defined in the Object class, its return type is String type. The returned content is the class name and its reference address. However, in practical applications, we often override the toString method.

equals and ==:

In the object, == means to judge whether it is the same object. equals determines whether two objects have the same logical value.

Difference: "=" is used for basic data types, and equals is used for reference data types. For example: String is a reference data type. To compare strings for equality, use equals.

 Manual project training: (test learning strength)

Reference blog:

(47 messages) Java—Shang Silicon Valley Customer Information Management System [Project 2] (familiar with the framework, familiar with the design structure!!!)_Shang Silicon Valley Customer Relationship Management Department_丿今朝的博客-CSDN博客

Reference video:

92-Object-Oriented (Advanced)-Project 2: Demonstration and Code Implementation of Pinyin E-commerce Customer Management System_哔哩哔哩_bilibili

mission target:

  1. Customer is an entity object used to encapsulate customer information
  2. CustomerList is the management module of Customer objects, internally uses arrays to manage a group of Customer objects, and provides corresponding methods of adding, modifying, deleting and traversing for CustomerView to call
  3. CustomerView is the main module, responsible for menu display and user operation processing

 The task UML class diagram is as follows:

 

 The specific role of these methods:

 I won’t talk about the latter three, it’s very simple, it’s the return value matched with our attributes.

After these methods are written, we can perform unit testing .

From the third step to realize the view view page, you can not write it temporarily. Mainly realize the first two. The first two methods have been mentioned. After writing the first two pages, we can test our effect. 

This is the main interface view to enter: the knowledge points designed by this view are very interesting. You can take it out and try it out.

A switch statement is involved. The use of the select method! Great idea!

Trouble encountered:

inside the delete method. Although I thought of adding a judgment statement, when there is only one element, only assign it to null. But in the end something went wrong. The best way is that the following for loop can also be used as a judgment condition.

public boolean delete(int index){
   if(index<0 || index>total){
       return false;
   }
//涉及的思想就是,当只有一个元素时,这个for循环就不运行。(这个用法请牢记!)
//这样,直接运行下面,把这个元素赋值为null。
//删除的操作,有两步:第一步,找到删除的元素。将其指向下一位元素。(如果只有一个元素,这一步就跳)
                   第二步,将下一位元素指向null。并把元素总数减一。
   for(int i=index;i<total;i++){
       customers[i]=customers[i+1];
   }
   customers[total]=null;
   total--;
   return true;
}

Guess you like

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