1. Parameter transfer
1.1 class name as formal parameter and return value (application)
-
1. The class name is used as the formal parameter of the method
The formal parameter of the method is the class name. In fact, what is needed is the object of the class
The address value of the object is actually passed
-
2. The class name is used as the return value of the method
The return value of the method is the class name. In fact, it returns the object of the class
The address value actually passed is also the address value of the object
-
Example code:
class Cat { public void eat() { System.out.println("Cats eat fish"); } } class CatOperator { public void useCat(Cat c) { //Cat c = new Cat(); c.eat(); } public Cat getCat() { Cat c = new Cat(); return c; } } public class CatDemo { public static void main(String[] args) { //Create an operation class object and call a method CatOperator co = new CatOperator(); Cat c = new Cat(); co.useCat(c); Cat c2 = co.getCat(); //new Cat() c2.eat(); } }
1.2 abstract classes as formal parameters and return values (understanding)
-
Abstract class as parameter and return value
- The formal parameter of the method is the abstract class name. In fact, what is needed is the subclass object of the abstract class
- The return value of the method is the abstract class name. In fact, it returns the subclass object of the abstract class
-
Example code:
abstract class Animal { public abstract void eat(); } class Cat extends Animal { @Override public void eat() { System.out.println("Cats eat fish"); } } class AnimalOperator { public void useAnimal(Animal a) { //Animal a = new Cat(); a.eat(); } public Animal getAnimal() { Animal a = new Cat(); return a; } } public class AnimalDemo { public static void main(String[] args) { //Create an operation class object and call a method AnimalOperator ao = new AnimalOperator(); Animal a = new Cat(); ao.useAnimal(a); Animal a2 = ao.getAnimal(); //new Cat() a2.eat(); } }
1.3 interface name as formal parameter and return value (understood)
-
Interface as parameter and return value
- The formal parameter of the method is the interface name. In fact, what is needed is the implementation class object of the interface
- The return value of the method is the interface name. In fact, it returns the implementation class object of the interface
-
Example code:
interface Jumpping { void jump(); } class JumppingOperator { public void useJumpping(Jumpping j) { //Jumpping j = new Cat(); j.jump(); } public Jumpping getJumpping() { Jumpping j = new Cat(); return j; } } class Cat implements Jumpping { @Override public void jump() { System.out.println("The cat can jump high"); } } public class JumppingDemo { public static void main(String[] args) { //Create an operation class object and call a method JumppingOperator jo = new JumppingOperator(); Jumpping j = new Cat(); jo.useJumpping(j); Jumpping j2 = jo.getJumpping(); //new Cat() j2.jump(); } }
2. Internal class
2.1 basic use of internal classes (understanding)
-
Inner class concept
- Define a class in a class. For example, if a class B is defined inside a Class A, class B is called an inner class
-
Internal class definition format
-
Format & ex amp le:
/* Format: class External class name{ Modifier class internal class name{ } } */ class Outer { public class Inner { } }
-
-
Access characteristics of internal classes
- Internal classes can directly access members of external classes, including private classes
- An object must be created for an external class to access members of an internal class
-
Example code:
/* Internal class access features: Internal classes can directly access members of external classes, including private classes An object must be created for an external class to access members of an internal class */ public class Outer { private int num = 10; public class Inner { public void show() { System.out.println(num); } } public void method() { Inner i = new Inner(); i.show(); } }
2.2 member internal class (understanding)
-
The definition location of the class inside the member
- In a class, a method is in the same place as a member variable
-
External member internal class format
- Format: external class name. Internal class name object name = external class object. Internal class object;
- Example: Outer.Inner oi = new Outer().new Inner();
-
Recommended usage scheme of inner class of member
- For the purpose of designing a class as an internal class, most of them do not want to be accessed by the outside world, so the definition of the internal class should be privatized. After privatization, a method that can be called by the outside world is provided. The internal class object is created and called inside the method.
-
Example code:
class Outer { private int num = 10; private class Inner { public void show() { System.out.println(num); } } public void method() { Inner i = new Inner(); i.show(); } } public class InnerDemo { public static void main(String[] args) { //Outer.Inner oi = new Outer().new Inner(); //oi.show(); Outer o = new Outer(); o.method(); } }
2.3 local internal classes (understanding)
-
Local internal class definition location
- A local inner class is a class defined in a method
-
Local internal class mode
- Local internal classes cannot be used directly by the outside world. You need to create objects inside the method and use them
- This class can directly access members of external classes or local variables in methods
-
Sample code
class Outer { private int num = 10; public void method() { int num2 = 20; class Inner { public void show() { System.out.println(num); System.out.println(num2); } } Inner i = new Inner(); i.show(); } } public class OuterDemo { public static void main(String[] args) { Outer o = new Outer(); o.method(); } }
2.4 anonymous inner class (application)
-
Premise of anonymous inner class
- There is a class or interface, where the class can be concrete or abstract
-
Format of anonymous inner class
-
Format: new class name () {rewrite method} new interface name () {rewrite method}
-
give an example:
new Inter(){ @Override public void method(){} }
-
-
The nature of anonymous inner classes
- Essence: it is an anonymous object that inherits the class or implements the subclass of the interface
-
Details of anonymous inner classes
-
Anonymous inner classes can be accepted in the form of polymorphism
Inter i = new Inter(){ @Override public void method(){ } }
-
-
Anonymous inner classes call methods directly
interface Inter{ void method(); } class Test{ public static void main(String[] args){ new Inter(){ @Override public void method(){ System.out.println("I am an anonymous inner class"); } }.method(); // Direct call method } }
2.4 use of anonymous inner classes in development (application)
-
Use of anonymous inner classes in development
- When a method needs a subclass object of an interface or abstract class, we can pass an anonymous inner class to simplify the traditional code
-
Example code:
interface Jumpping { void jump(); } class Cat implements Jumpping { @Override public void jump() { System.out.println("The cat can jump high"); } } class Dog implements Jumpping { @Override public void jump() { System.out.println("The dog can jump high"); } } class JumppingOperator { public void method(Jumpping j) { //new Cat(); new Dog(); j.jump(); } } class JumppingDemo { public static void main(String[] args) { //Requirements: create the object of the interface operation class and call the method method method JumppingOperator jo = new JumppingOperator(); Jumpping j = new Cat(); jo.method(j); Jumpping j2 = new Dog(); jo.method(j2); System.out.println("--------"); // Simplification of anonymous inner classes jo.method(new Jumpping() { @Override public void jump() { System.out.println("The cat can jump high"); } }); // Simplification of anonymous inner classes jo.method(new Jumpping() { @Override public void jump() { System.out.println("The dog can jump high"); } }); } }
3. Common API
3.1 Math (application)
-
1. Math class overview
- Math contains methods for performing basic numeric operations
-
2. Method calling method in Math
- If there is no constructor in the Math class, but the internal methods are static, they can be called through the class name
-
3. Common methods of Math class
Method name method name explain public static int abs(int a) Returns the absolute value of the parameter public static double ceil(double a) Returns the minimum double value greater than or equal to the parameter, which is equal to an integer public static double floor(double a) Returns the maximum double value less than or equal to the parameter, which is equal to an integer public static int round(float a) Returns the int closest to the parameter by rounding public static int max(int a,int b) Returns the larger of the two int values public static int min(int a,int b) Returns the smaller of the two int values public static double pow (double a,double b) Returns the value of a to the power of b public static double random() The return value is a positive value of double, [0.0,1.0)
3.2 System (application)
- Common methods of System class
Method name | explain |
---|---|
public static void exit(int status) | Terminate the currently running Java virtual machine. Non zero indicates abnormal termination |
public static long currentTimeMillis() | Returns the current time in milliseconds |
-
Sample code
- Requirement: output 1-10000 on the console and calculate how many milliseconds this code has been executed
public class SystemDemo { public static void main(String[] args) { // Get start time node long start = System.currentTimeMillis(); for (int i = 1; i <= 10000; i++) { System.out.println(i); } // Get the time node after the code runs long end = System.currentTimeMillis(); System.out.println("Total time:" + (end - start) + "millisecond"); } }
3.3 toString method of object class (application)
-
Object class overview
- Object is the root of the class hierarchy. Each class can take object as a superclass. All classes directly or indirectly inherit from this class. In other words, all classes will have a copy of the methods of this class
-
How to view the method source code
- Select the method and press Ctrl + B
-
How to override the toString method
-
- Alt + Insert select toString
-
- In the blank area of the class, right-click - > generate - > select toString
-
-
toString method:
-
It is more convenient to display the attribute values in the object in a good format
-
Example code:
class Student extends Object { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } } public class ObjectDemo { public static void main(String[] args) { Student s = new Student(); s.setName("Lin Qingxia"); s.setAge(30); System.out.println(s); System.out.println(s.toString()); } }
-
Operation results:
Student{name='Lin Qingxia', age=30} Student{name='Lin Qingxia', age=30}
3.4 equals method of object class (application)
-
Function of equals method
- It is used for comparison between objects and returns true and false results
- For example: s1.equals(s2); s1 and s2 are two objects
-
Scenario of overriding the equals method
- When you don't want to compare the address value of the object and want to compare it with the object attribute.
-
How to override the equals method
-
- alt + insert select equals() and hashCode(), IntelliJ Default, next and finish all the way
-
- In the blank area of the class, right-click - > generate - > select equals() and hashCode(), and the following is the same as above.
-
-
Example code:
class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object o) { //this -- s1 //o -- s2 if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; //student -- s2 if (age != student.age) return false; return name != null ? name.equals(student.name) : student.name == null; } } public class ObjectDemo { public static void main(String[] args) { Student s1 = new Student(); s1.setName("Lin Qingxia"); s1.setAge(30); Student s2 = new Student(); s2.setName("Lin Qingxia"); s2.setAge(30); //Requirement: compare whether the contents of two objects are the same System.out.println(s1.equals(s2)); } }
3.5 bubble sorting principle (understanding)
- Bubble sorting overview
- A sort method, which compares the adjacent data in the data to be sorted, puts the larger data behind, and operates all the data in turn until all the data are sorted as required
- If there are n data to sort, a total of n-1 comparisons are required
- After each comparison, one less data will be involved in the next comparison
3.6 implementation of bubble sorting code (understanding)
- code implementation
/* Bubble sort: A sort method, which compares the adjacent data in the data to be sorted, and puts the larger data behind, Operate all data in turn until all data are sorted as required */ public class ArrayDemo { public static void main(String[] args) { //Define an array int[] arr = {24, 69, 80, 57, 13}; System.out.println("Before sorting:" + arrayToString(arr)); // Minus 1 here controls the number of comparisons per round for (int x = 0; x < arr.length - 1; x++) { // -1 is to avoid index out of bounds, - x is to improve comparison efficiency for (int i = 0; i < arr.length - 1 - x; i++) { if (arr[i] > arr[i + 1]) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } } System.out.println("After sorting:" + arrayToString(arr)); } //The elements in the array form a string according to the specified rules: [element 1, element 2,...] public static String arrayToString(int[] arr) { StringBuilder sb = new StringBuilder(); sb.append("["); for (int i = 0; i < arr.length; i++) { if (i == arr.length - 1) { sb.append(arr[i]); } else { sb.append(arr[i]).append(", "); } } sb.append("]"); String s = sb.toString(); return s; } }
3.7 Arrays (application)
-
Common methods of Arrays
Method name explain public static String toString(int[] a) Returns a string representation of the contents of the specified array public static void sort(int[] a) Arranges the specified array in numerical order -
Tool design idea
1. The construction method is decorated with private
2. Members are decorated with public static