0x00 overview
This article covers Java knowledge points, internal classes, and API s
0x01 parameter transfer
1.1 class name as formal parameter and return value
- 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
What is actually passed is the address value of the object
- 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
What is actually passed is also the address value of the object
Example
package com.classParams; public class Cat { public void eat() { System.err.println("Cats eat fish"); } }
package com.classParams; public class CatOperator { public void useCat(Cat c) { c.eat(); } public Cat getCat() { Cat c = new Cat(); return c; } }
package com.classParams; 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
- 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
package com.abstractClassParams; public abstract class Animal { public abstract void eat(); }
package com.abstractClassParams; public class Cat extends Animal { @Override public void eat() { System.out.println("Cats eat fish"); } }
package com.abstractClassParams; public class AnimalOperator { public void useAnimal(Animal a) { a.eat(); } public Ainmal getAnimal() { Animal a = new Cat(); return a; } }
package com.abstractClassParams; 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(); a2.eat(); } }
1.3 interface name as formal parameter and return value
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 name of the interface. In fact, what is needed is the implementation class object of the interface
Example
package com.InterfParams; public interface jumping { void jump(); }
package com.InterfParams; public class Cat implements jumping { @Override public void jump() { System.out.println("Cat high jump"); } }
package com.InterfParams; public class JumpingOperator { public void useJumpping(jumping j) { j.jump(); } public jumping getJumpping() { jumping j = new Cat(); return j; } }
package com.InterfParams; public class JumppingDemo { public static void main(String[] args) { // Create an operation class object and call a method JumpingOperator jo = new JumpingOperator(); jumping j = new Cat(); jo.useJumpping(j); jumping j2 = jo.getJumpping(); j2.jump(); } }
0x02 internal class
2.1 use of internal classes
- Inner class concept
Define a class in a class. For example, define a class B inside a Class A, and 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
package com.InnerClass1; /* Internal class access features */ 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
- 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
- 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 internal classes should be privatized,
After privatization, provide a method that can be called by the outside world, create an internal class object inside the method and call
Example
package com.InnerClass2; public class Outer { private int num = 10; private class Inner { private void show() { System.out.println(num); } } public void method() { Inner i = new Inner(); i.show(); } }
package com.InnerClass2; 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 class
- 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 in methods and use them
This class can directly access members of external classes or local variables in methods
Example
package com.IneerClass3; public 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(); } }
package com.IneerClass3; public class OuterDemo { public static void main(String[] args) { Outer o = new Outer(); o.method(); } }
2.4 anonymous inner class
- 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 felt in the form of polymorphism
Inter i = new Inter() { @override public void method() {} }
Anonymous inner classes call methods directly
package com.InnerClass4; public class Test { public static void main(String[] args) { new Inter() { @Override public void method() { System.out.println("I am an anonymous inner class"); } }.method(); } }
2.5 use of anonymous inner classes in development
- 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
package com.InnerClass5; public interface Jumpping { void jump(); }
package com.InnerClass5; public class Cat implements Jumpping { @Override public void jump() { System.out.println("The cat can jump high"); } }
package com.InnerClass5; public class Dog implements Jumpping{ @Override public void jump() { System.out.println("The dog can jump high"); } }
package com.InnerClass5; public class JumppingOperator { public void method(Jumpping j){ j.jump(); } }
package com.InnerClass5; public class JumppingDemo { public static void main(String[] args) { // Requirements: create interface operation class objects and call method method JumppingOperator jo = new JumppingOperator(); Jumpping j = new Cat(); jo.method(j); Jumpping j2 = new Dog(); jo.method(j2); System.out.println("----------"); // Anonymous inner class simplification jo.method(new Jumpping() { @Override public void jump() { System.out.println("The cat can be raised"); } }); // Anonymous inner class simplification jo.method(new Jumpping() { @Override public void jump() { System.out.println("The dog can jump high"); } }); } }
0x03 common API
3.1 Math
- Math class overview
Math contains methods for performing basic numeric operations
- Method calling method in Math
There is no constructor in Math class, but the internal methods are static and can be called through the method of class name
- Common methods of Math class
3.2 System
Common methods of System class
Example
package com.APITest; /* 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 = 0; i < 10001; 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
- 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 that the class has
- How to view the method source code
Select the method and press Ctrl+B
- How to override the toString method
Alt+String select toString
In the blank area of the class, right-click - > generate - > select toString
- Function of toString method
It is more convenient to display the attribute values in the object in a good format
Example
package com.toStringTest; public 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 + '}'; } }
package com.toStringTest; public class StudentDemo { public static void main(String[] args) { Student s1 = new Student("Alice", 13); System.out.println(s1); System.out.println(s1.toString()); } }
3.4 equals method of object class
- Function of equals method
It is used for comparison between objects and returns true or 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, you want to compare it in combination 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
package com.ObjectTest; public 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) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; if (age != student.age) return false; return name != null ? name.equals(student.name) : student.name == null; } }
package com.ObjectTest; public class ObjectDemo { public static void main(String[] args) { Student s1 = new Student("Alice", 22); Student s2 = new Student("Alice", 22); System.out.println(s1.equals(s2)); } }
3.5 bubble sorting principle
Bubble sorting overview
- A sort method that 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 times are required
- After each comparison, one less data will participate in the next comparison
3.6 implementation of bubble sorting code
package com; /* Bubble sorting */ public class BubboSort { public static void main(String[] args) { // Define an array int[] arr = {23, 68, 90, 47, 12}; 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++) { // Subtract the number of times per round for (int i = 0; i < arr.length - 1 - x; i++) { if (arr[i] > arr[i + 1]) { int tmp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = tmp; } } } System.out.println("After sorting:" + arrayToString(arr)); } 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
- Common methods of Arrays
Tool design idea
- The construction method is decorated with private
- Members are decorated with public static