Process oriented (pop) vs object oriented (oop)
Process oriented is a kind of functional behavior. It takes function as the minimum unit and considers how to do it
Object oriented emphasizes the objects with functions, taking class / object as the minimum unit and considering who will do it
Two basic elements of Java object-oriented programming: Class - > object
Use and instantiation of classes
-
Class: the description of a class of things, which is an abstract and conceptual definition.
-
Object: it refers to each individual, concrete and instance of such things that actually exist.
-
Class members: attributes (member variables), methods ()
Steps for using classes and objects:
- Create class, and design class members (key)
- Create an object of a class (instantiation of a class)
- Call the structure [object variable name. Attribute or object variable name. Method] in the class through the object of the class
Class member 1: use of attributes:
Attribute (member variable) VS local variable
-
Attribute is declared in {} of class
Local variables are variables declared in methods, method parameters, constructors, constructor parameters, and code blocks
-
Attributes can be modified with permission modifiers; Local variables are not allowed
Common permission modifiers: private\public\ default \protected
-
Attribute has default initialization value; No local variables
Default initialization value for property:
Integer variable (int\short\byte\long): 0
Floating point variable (float\double): 0.0
Boolean variables: false
Application type variable (array, class, interface): null
-
Attributes are stored in memory in the heap
Local variables are stored on the stack in memory
Class member 2: Method usage:
-
Method declaration:
Permission modifier return value type method name (formal parameter list){
Method body;
}
-
Return value types can be divided into two types: with return value and without return value
① With return value: specify the return data type when declaring; Use the return keyword in the method body to return a variable or constant of the specified type
② No return value: declared with void; Normally, the return keyword is not used, but if it is used, only return can be used—— Indicates the end of the method
-
Format of formal parameter list: data type 1 variable name 1, data type 2 variable name 2······
-
-
Object array:
Array elements are classes
Exercise: define class Student, which contains three attributes: Student number(int), grade state(int), and score(int). Create 20 Student objects with Student numbers from 1 to 20. Grades and grades are determined by random numbers. Question 1: print out the Student information of grade 3 (the state value is 3). Question 2: use bubble sorting to sort students' grades and traverse all Student information
package com.oop.java; public class Exer1 { public static void main(String[] args) { Student[] array = new Student[20]; for(int i=0;i<array.length;i++) { //Assign values to array elements array[i] = new Student(); //Assign values to each attribute of an element array[i].number = i+1; array[i].state = (int)(Math.random()*9)+1; array[i].score =(int)(Math.random()*101)+0; } Exer1 ss = new Exer1(); ss.searchState(array, 3); System.out.println(); ss.sortState(array); ss.print(array); } //Traversal array public void print(Student[] array1) { for(int i=0;i<array1.length;i++) { System.out.println(array1[i].info()); } } /** * Find the student information of the specified grade in the Sudent array * @param array1 Specify array * @param state Grade to find */ public void searchState(Student[] array1,int state) { for(int i=0;i<array1.length;i++) { if(array1[i].state==state) { //System.out.println(array[i]);// Address value System.out.println(array1[i].info()); } } } /** * Bubble sort the Student array * @param array1 */ public void sortState(Student[] array1) { for(int i=0;i<array1.length-1;i++) { for(int j=0;j<array1.length-1-i;j++) { if(array1[j].score>array1[j+1].score) { //int temp = array[j].score; //array[j].score = array[j+1].score; //array[j+1].score = temp; //Important: the array element student is exchanged in order, not the student's grade Student temp = array1[j]; array1[j] = array1[j+1]; array1[j+1] = temp; } } } } } class Student{ int number;//Student ID int state;//grade int score;//achievement //Output student information, you can write a method public String info() { return "Student No.:"+number+", Age:"+state+", Results:"+score ; } }
-
Method overloads: overloads are formed between several methods of the same class, method name and parameter list
Note: ① formal parameter list: number of parameters and data type (order) of parameters ② it is independent of permission modifier and return value type
-
Variable number of formal parameters
Format: data type Variable name
You can pass in multiple formal parameters of the same data type (similar to an array)
The definition of a variable parameter can only be declared at the end and has only one
-
Value Passing Mechanism of method parameters
(focus on understanding the structure of local variables - stack, attributes and new - heap by drawing memory analysis diagram)
Value Passing Mechanism: ① if the parameter is a basic data type, the actual parameter is assigned to the formal parameter with the actual stored data value of the actual parameter
② If the parameter is a reference data type, the address value of the stored data of the parameter is assigned to the formal parameter by the parameter
-
Call of recursive method
Calling its own method in a method is called recursion
package com.oop.java; /* * Use of recursive methods: * Example: a sequence of numbers is known: f(0)=1;f(1)=4,f(n+2)=2*f(n+1)+f(n) * Find the value of f (10) */ public class Exer3 { public static void main(String[] args) { Exer3 exe = new Exer3(); int f = exe.f(10); System.out.println(f); } public int f(int n) { if(n==0) { return 1; }else if(n==1) { return 4; }else { return 2*f(n-1)+f(n-2); } } }
Class member 3: constructor
-
Functions: ① create objects; ② Information about the initialization object
-
Format: permission modifier class name (formal parameter list){
}
-
explain:
- By default, the system provides a null parameter constructor with the same permissions as the class; Once the constructor is explicitly defined, the system no longer provides an empty parameter constructor
- Overloads also exist between constructors (see the formal parameter list: number of parameters and parameter types)
- At least one constructor in a class
-
Assignment process of attributes (sequence)
Default initialization value - explicit assignment - assignment in constructor - assignment by "object. Attribute" or "object. Method"
Class member 4 -- code block
-
Code block (initialization block): function: initialize classes and objects
-
Format: {}
-
You can only use static modification, which can be divided into static code blocks and non static code blocks according to whether static modification is used
——Static code block: static {}
① Execute as the class is loaded and only once
② Class information can be initialized in it (only static properties or methods can be called)
——Non static code block: {}
① It is executed as the object is created. Each time an object is created, it will be executed once
② Initializing an object's properties or methods
- Method and sequence of attribute assignment: default initialization value - explicit initialization / code block initialization (who declares first, who first) - constructor assignment - object Properties / methods
Class member 5: inner class
-
Declare another class B inside a Class A (a -- external class, B -- internal class)
-
Classification: member inner class: static member inner class (static decoration), non static member inner class [similar to method, parallel structure]
Local inner classes: within methods, code blocks, and constructors
————Member internal class: its use is viewed from two perspectives - as a member of an external class [can call the structure of an external class; modified by static and permission modifiers] and as a class [can define attributes, methods, constructors, etc.; modified by final and abstract]
① How to instantiate an internal member class:
② How to call the structure of an external class: pay special attention to the use of duplicate names
————Use of local internal classes in development: used more in methods
//Returns an object of a class that implements the Interface1 interface public Interface1 getInterfaca(){ //Create a class that implements Interface1 interface: local inner class //Method 1: class MyInterface1 implements Interface1{ } return new MyInterface1(); //Mode 2: //return new MyInterface1(){} }