Java basic notes-object-oriented 2, java object-oriented
Constructor: (when no constructor is defined in the class, the system will add a constructor with null parameters to the class by default)
Format:
Class Person
{
Person () {}// null Parameter
}
1. The function name is the same as the class name,
2. the return value type is not defined, that is, the return statement cannot be written.
Purpose: Initialize the object.
Differences from common functions:
1. the constructor is executed only once. Other functions can be executed multiple times.
Construct a code block:
Construct a code block to initialize the object. The object runs as soon as it is created and takes precedence over the constructor.
Difference from constructor:
The same point is that the object is initialized and used.
The constructor code block initializes all objects, and the constructor initializes the corresponding objects.
The constructed code block defines the initialization content that is common to different objects.
Format:
For example:
{
System. out. println ("construct a code block! ");
}
This Keyword:
Role: used to distinguish member variables from local variablesSame name.
For example:
Class Person {
String name; // ---> member variable name.
Person (String name) // ---> local variable ame.
{
This. name = name; // this. name => indicates the name of the member variable.
Name = name; // if defined in this way, both names are local variable names.
}
}
This indicates the reference of the object to which the class belongs,
In general:Which function calls the function where this is located, and this indicates the object.
Code demonstration of this keyword, constructor, and constructor code block.
1 class Person 2 {3 private int age; 4 private String name; 5/* 6 construct a code block to initialize the object. The object runs as soon as it is created and takes precedence over the constructor. 7. Differences from constructor: the constructor code block initializes all objects, while the constructor initializes the corresponding objects. 8. The constructed code block defines the initialization content that is common to different objects. 9 */10 {11 System. out. println ("construct a code block! "); 12 cry (); 13} 14 // constructor 15 Person () 16 {17 System. out. println ("A: name =" + name + "age =" + age); 18 // cry (); 19} 20/* this: keyword 21 function: used to distinguish a member variable from a local variable with the same name. 22 this indicates the reference of the object to which the class belongs. this indicates the object to which the function calls the function of this. 23 */24 Person (int age) 25 {26 this. age = age; 27 System. out. println ("B: name =" + name + "age =" + age); 28 // cry (); 29} 30 Person (int age, String name) 31 {32 this. age = age; 33 this. name = name; 34 System. out. Println ("C: name =" + this. name + "age =" + this. age); 35 // cry (); 36} 37 38/* 39 this application: defines a function that compares two people of the same age. 40 */41 public boolean compare (Person p) 42 {43 return this. age = p. age; // this. this in age indicates the object that calls this function. the object p4 in row 77 calls the compare function. this is the representative p4.44} 45 46 // The function 47 public void setName (String name) with the name set) 48 {49 this. name = name; 50} 51 // function 52 public String getName () 53 {54 return name; 55} 56 57 pub Lic void cry () 58 {59 System. out. println ("Cry --------! "); 60} 61} 62 63 class PersonDemo264 {65 public static void main (String args []) 66 {67 Person p1 = new Person (); 68 Person p2 = new Person (10); 69 Person p3 = new Person (5, "Tom"); 70 System. out. println (p3.getName (); 71 72 p3.setName ("Paul"); 73 System. out. println (p3.getName (); 74 75 Person p4 = new Person (25); 76 Person p5 = new Person (20); 77 boolean B = p4.compare (p5); 78 System. out. println (B); 79 80} 81}
Static keywords:
Used to modify member variables (global variables) and functions.
Static modified variables are also called class variables and static variables.
Static member variables are modified only once to prevent being referenced in other file units.
Differences between static local variables and common local variables:
1. Changed the local variable to a static variable.Storage MethodThat is, it changes its survival.
2. After the member variable is changed to a static variable, itsScope, Limits its scope of use.
Static local variables are initialized only once, and the next time is based on the previous result value;
<When to define a static function: When the function does not access static data (Object-specific data), this function can be defined as a static function.>???
Because static functions only have one instance in the memory, they do not need to be instantiated any more during use. These classes are generally stateless, and all functions are called the same.
Therefore, they apply to tool classes.
Code exercises for tool applications:
1/*** 2 This is a tool class that can operate on arrays. It can obtain the most value, sort, print, and other functions. 3 @ author JerryHo 4 @ version V1.0 5 */6 // to generate a help document, this class must be public. 7 public class ArrayTool 8 {9 // After the constructor is privatized, you can force this class to create a constructor with null parameters of object 10/** 11. 12 */13 private ArrayTool () 14 {15 // The system has a default constructor 16} 17/** 18 to obtain the maximum value in the integer array. 19 @ param arr receives an array of the int type. 20 @ return returns a maximum value in the array. 21 */22 public static int getMax (int arr []) 23 {24 int max = 0; 25 for (int x = 0; x <Rr. length; x ++) 26 if (arr [max] <arr [x]) 27 {28 max = x; 29} 30 return arr [max]; 31} 32/** 33 get the minimum value in the integer array. 34 @ param arr receives an array of the int type. 35 @ return returns a minimum value in the array. 36 */37 public static int getMin (int arr []) 38 {39 int min = 0; 40 for (int x = 0; x <arr. length; x ++) 41 {42 if (arr [min]> arr [x]) 43 {44 min = x; 45} 46} 47 return arr [min]; 48} 49/** 50: select and sort int-type arrays. 51 @ param arr receives an int array. 52 */53 Public static void selectSort (int arr []) 54 {55 for (int x = 0; x <arr. length-1; x ++) 56 {57 for (int y = x + 1; y <arr. length; y ++) 58 {59 if (arr [x]> arr [y]) 60 {61 // int temp = arr [x]; 62 // arr [x] = arr [y]; 63 // arr [y] = temp; 64 swap (arr, x, y ); 65} 66} 67} 68} 69/** 70 bubble sort the array. 71 @ param arr receives an int array. 72 */73 public static void bubbleSort (int arr []) 74 {75 for (int x = 0; x <arr. length-1; x ++) 76 {77 fo R (int y = 0; y <arr. length-1-x; y ++) 78 if (arr [y]> arr [y + 1]) 79 {80 // int temp = arr [y]; 81 // arr [y] = arr [y + 1]; 82 // arr [y + 1] = temp; 83 swap (arr, y, y + 1 ); 84} 85} 86} 87/** 88 swap the values of two variables in the integer array. 89 @ param arr receives an array of the int type. 90 @ param a: the position to be replaced. 91 @ param B the position to be replaced. 92 */93 private static void swap (int [] arr, int x, int y) 94 {95 int temp = arr [x]; 96 arr [x] = arr [y]; 97 arr [y] = temp; 98} 99 100/** 101 print the elements in the integer array. 102 @ param arr receives an int array. 103 print format: [element1, element2,...] 104 */105 public static void printArray (int arr []) 106 {107 System. out. print ("["); 108 for (int x = 0; x <arr. length; x ++) 109 {110 if (x! = Arr. length-1) 111 {112 System. out. print (arr [x] + ","); 113} 114 else115 {116 System. out. println (arr [x] + "]"); 117} 118} 119} 120}
1 class ArrayToolDemo 2 {3 public static void main (String args []) 4 {5/* 6 int [] arr = new int, 12}; 7 int [] arr1 = new int [] {9, 3, 6, 18, 13, 4, 23}; 8 ArrayTool at = new ArrayTool (); 9 10 int max =. getMax (arr); 11 System. out. println ("max =" + max); 12 13 int min =. getMin (arr); 14 System. out. println ("min =" + min); 15 16. printArray (arr); 17. selectSort (arr); 18. printArray (arr); 19 20. printArray (arr1); 21. bubbleSort (arr1); 22. printArray (arr1); 23 */24 25 // call the related functions directly with the class name. 26 int [] arr = new int [] {, 12}; 27 int [] arr1 = new int [] {, 23 }; 28 29 int max = ArrayTool. getMax (arr); 30 System. out. println ("max =" + max); 31 int min = ArrayTool. getMin (arr); 32 System. out. println ("min =" + min); 33 34 int Max = ArrayTool. getMax (arr1); 35 System. out. println ("max =" + Max); 36 int Min = ArrayTool. getMin (arr1); 37 System. out. println ("min =" + Min); 38 39 ArrayTool. printArray (arr); 40 ArrayTool. selectSort (arr); 41 ArrayTool. printArray (arr); 42 43 ArrayTool. printArray (arr1); 44 ArrayTool. selectSort (arr1); 45 ArrayTool. printArray (arr1); 46 47 48} 49}
I stopped electricity for almost one day yesterday. I got up late and started watching it in the morning. I had to think about it myself after reading the tutorial. I don't know much about it in many places, but I still want to take it easy, I felt that my head was stiff. After watching the video tutorial, I had to take notes again. I had to think about what I learned in a day, then I typed the code and found that there were always many problems after compilation, I changed my mind to see where the problem occurred. I had to write a blog record at, and I felt full and tiring all day long, there is a feeling of high school. You have to take a bath and go to bed. You have to continue tomorrow .. hold on ...!