day10 object oriented programming

1. Array Select sort Train of thought analysi...
1. Array

Select sort

Train of thought analysis

Disadvantages: frequent exchange and waste of time

Improved version selection sorting

Improvement: add a subscript of the minimum value of mindex record

//Improved version selection sorting for (int i = 0; i <arr.length - 1; i++) { //i is set as the reference position, and the goal is to put the minimum value of all subsequent numbers, including the reference position, into the reference position int mindex = i;//It is assumed that the reference position is the minimum value for (int j = i + 1; j < arr.length; j++) {//Find the minimum subscript if (arr[mindex] > arr[j]){//If the value of j position is smaller, record the smaller subscript mindex = j; } } //What is saved in minIndex is the subscript of the minimum value int tmp = arr[i]; arr[i] = arr[mindex];//Minimum homing arr[mindex] = tmp; } for (int a : arr) { System.out.print(a+" "); }

Quick sort

Key idea: partition + recursion

He traded the stack for time

Train of thought analysis

code implementation

public class QuickSort { public static void quick(int[] arr,int begin,int end){ if(begin == end){//An end condition must be given here, otherwise an array overflow will occur return; } //Set a key value key, with this keyword as the midpoint, small on the left and large on the right int key = arr[begin]; //Set the index of keyIndex to store the key int keyIndex = begin; //for loop traversal array for (int i = begin; i < end; i++) { //Compare the numbers in the array with the key respectively, and there is no need to move the values larger than the key //If it is smaller than the key value if(arr[i] < key){ //For keyIndex + +, the purpose is to locate the position of the final key value keyIndex++; //The smaller number should be placed on the left, so before the exchange, the keyIndex must refer to a number larger than the key value, but after the exchange, it becomes a number smaller than the key value int tmp = arr[i]; arr[i] = arr[keyIndex]; arr[keyIndex] = tmp; } } //After traversal, the key value is still at the position with subscript 0. At this time, we need to put the key value at the position indicated by keyIndex. After exchange, the array is basically in order arr[begin] = arr[keyIndex]; arr[keyIndex] = key; //Sort the first half and the second half recursively quick(arr,begin,keyIndex); quick(arr,keyIndex+1,end); } public static void main(String[] args) { int[] arr = new int[8]; for (int i = 0; i < arr.length; i++) { arr[i] = (int)(Math.random()*30); } for (int t : arr) { System.out.print(t+" "); } System.out.println(); quick(arr,0,arr.length); } }

Inside JDK, there is fast platoon: Double partition fast platoon

Calling function: array. Sort();

2. Multidimensional array

Two dimensional array: an array of arrays whose elements are one-dimensional subarrays

initialization

1) Dynamic initialization

Format 1: int[][] arr = new int[3][2];

            There are three one-dimensional arrays in the two-dimensional array   There are 2 elements in each one-dimensional array

Format 2: int[][] arr = new int[3] [];

            There are three one-dimensional arrays in the two-dimensional array.   Each one-dimensional array is the default initialization value null  

2) Static initialization:

Format 1: int[][] arr = new int[][]{,,;

Format 2: int[][] arr = new int{,,;

Format 2 is inconvenient. Declaration initialization must be on the same line!

3) Special writing: int[] x,y[]; x is a one-dimensional array and Y is a two-dimensional array.

Multi dimensional arrays in Java do not have to be in the form of regular matrix

The array is created in memory

Take the maximum and minimum values

public static void main1(String[] args) { int[][] arrarr = new int[6][];//Indicates that an array with only 6 subarrays is created, and no real subarrays are generated for (int i = 0; i < arrarr.length ; i++) { //Really create a subarray int n = (int)(Math.random()*4+6); arrarr[i] = new int[n]; //Assign a value to a subarray for (int j = 0; j < arrarr[i].length; j++) { arrarr[i][j] = (int)(Math.random()*200); } } for (int i = 0; i < arrarr.length; i++) { for (int j = 0; j < arrarr[i].length; j++) { System.out.print(arrarr[i][j]+" "); } System.out.println(); } //Find the maximum and minimum values int max = arrarr[0][0]; int min = arrarr[0][0]; for (int i = 0; i < arrarr.length; i++) { for (int j = 0; j < arrarr[i].length; j++) { if (arrarr[i][j] > max){ max = arrarr[i][j]; } if (arrarr[i][j] < min){ min = arrarr[i][j]; } } } System.out.println("max="+max); System.out.println("min="+min); }
3. Variable parameter method

1) Variable parameter, immutable type

2) Fully compatible with int arrays

         int... values is essentially an array. Compilation helps us convert the scattered numbers we pass in

3) The args command line parameter in the main method is also a variable parameter in nature

4) Variable parameter is a new technology in JDK version

         String [] is universal, but using string... Should be used in JDK 1.5 or above

5) Only one variable parameter is allowed in a method and must be placed last

//Example: find the maximum value public class MaxArgs { public static int max(int... args) { //Find maximum value int max = args[0]; for (int i = 0; i < args.length; i++) { if (args[i] > max){ max = args[i]; } } return max; } public static void main(String[] args) { System.out.println(max(2,4,7,9,5,45,6,4)); System.out.println(max(28,400,70,9,53,45,66,41)); } }
4. Inherit

What is inheritance?

Inheritance express ownership   ( Ownership: no direct use right, indirect use right)

Inheritance: it is to create subclasses from existing classes, which are called parent classes, base classes (subclasses are extended based on parent classes), and superclasses (subclasses refer specifically to parent class members through super qualification)

What are subclasses?

1) Subclass is an extension of the parent class. Subclass > = parent class

2) Subclasses inherit all members of the parent class, except constructors

3) Subclasses can add new members

                 Unique members; Extended members in subclasses

Syntax format:

//PC inherits all properties and methods in Computer

public class PC extends Computer{}

Memory angle analysis subclass > = parent class

A subclass object created in memory will contain all the attributes in the parent class. There is only one pure subclass object, which can be regarded as a parent object implied in the subclass object, but the parent object is not created, but the subclass object can be used as a parent object.

What is the case with inheritance?

When multiple classes have the same properties and behaviors, in order to improve code reuse, it makes the writing of subclasses easier

Role of inheritance:

1) Improve reusability

2) The relationship between classes is the premise of polymorphism

3) Dynamics

What is the difference between multi inheritance and multi inheritance?

1) Multi level inheritance, also known as single inheritance.

         A subclass can only have one parent

                 Direct parent: close

                 Indirect parent: far away

2) Multiple inheritance: a subclass has multiple direct parent classes (not allowed), because when multiple inheritance, if multiple direct parent classes have methods with the same name, the subclass will be confused

Only single inheritance is supported in java. Multiple inheritance is not allowed!

Can private members and methods be inherited?

It can inherit, but it can't be used directly.

Because the private modified member can only be used in this class, and the subclass is not this class, it cannot be used directly in the subclass. You can add a public get/set method in the parent class, and you can use it indirectly in the subclass through the get/set method inherited by the parent class. Play a protective role!!

Example: use of internal organs and CPU

The constructor cannot inherit, but can it be called?

Yes, all subclass constructors will call the parameterless constructor in the parent class by default

super(); If not, there will be an implicit call to the parent parameterless constructor

  Note:

1) The first line in the subclass constructor must be this(...) or super(...)

this(...) is used to indirectly call the parent class construction

The function of super(...) is to call the parent class constructor directly

2) Conclusion: the subclass constructor must first call the parent constructor

If this(...) and super(...) appear at the same time, it will be initialized twice

5. Method override

One of the most important concepts is not

Definition: subclasses transform the inherited parent methods

             On the premise of inheritance

Override reason: the parent class method no longer meets the requirements

Coverage conditions:

1) The method signatures (method name, parameter list and return value type) in the subclass and parent class must be exactly the same. The consistency of parameter list is reflected in the consistency of type, parameter and order

2) The overridden method cannot have more strict access rights than the overridden method. The subclass control modifier > = parent class

3) Must be static

Note: if the method is overridden, the method in the subclass is executed when it is executed

public String getDetails(){
      return "cpu:"+getCpu() + ", memory:" + getMemory() + ", disk:" + getDisk() + ", keyboard:" + keyboard;
}

Why don't you write complete in the parent class?

Because the parent class is passive, it never knows which subclasses inherit it.

Properties cannot be overwritten. They always coexist!

If the child and parent have the same property

this.age represents the age of the current class

super.age is inherited from the parent class

Method override must be annotated!!!

6. Annotation

Special annotations do not participate in the operation of the program, but can be recognized by the compiler and JVM

The essence is a modifier, which is placed in front of the method

@override annotation:

Request the compiler to check the method coverage. If there is a problem with the method coverage, please compile an error

@Override public String getDetails(){ return super.getDetails()+",Keyboard:"+keyboard; }
7. super keyword

super refers specifically to the parent class of the current subclass

If there are two say() methods in the subclass, super.say() represents the overridden method in the parent class

For the test class, the method overridden by the subclass is the only method, and the overridden method in the parent class is never visible

Benefits: when the parent class changes, there is no need to modify the child class to realize serial calls

public String getDetails(){ return super.getDetails()+",Keyboard:"+keyboard; }

super function: it can access the properties, methods and constructors in the parent class

Note: the traceability of super is not limited to the direct parent class

The difference between this and super:

1) this represents the current object as a whole

2) super indicates that it is just a memory identifier used to qualify members inherited from the parent class

3) super is never an object!

8. Access modifier  

1 September 2021, 17:37 | Views: 7841

Add new comment

For adding a comment, please log in
or create account

0 comments