Common methods of classes and objects and array utility classes

1. Classes and Objects

1. When no one references the object, it will be automatically recycled.
2. The object must be on the heap, and the reference variable is not necessarily on the stack
. this.data accesses the properties of the current object, and this.func() accesses the method of the current object
4. The difference between initializing object member variables and assigning values ​​​​to object member variables is to use the constructor (this method has no return value type, and the method name is the same as the class name) to initialize object member variables. Use this() to call other construction methods in the current class. There may be parameters in (), see which construction method matches the parameters in (), and call which construction method, it can only be used inside the current construction method, and can only be placed on the first line. Use the idea to generate the construction method, right-click to find Generate, and then click Constructor
5. If there is no package statement in the class, the class will be placed in a default package, which is src. default (default package access rights), if there is no access modifier qualifier before the member variable, different classes under the same package can access this member variable (this is the maximum access right of default) 6. Static member variables and static member methods (static modification, also called class variables and class methods, static cannot modify local variables), through class calls (class. ×××), do not depend on objects
. A static method cannot directly call a non-static member method (the call requires a new object, and then call it through the reference of the object) or directly call a non-static member variable. This cannot be used in a static modified method, because static member variables and static member methods do not depend on the object, and this is a reference to the current object. Non-static methods can directly call static member methods. Non-static methods can be called directly.
7. Member variables (properties, fields), member methods (behaviors).
8. Static member variables are generally not initialized in the construction method, and the instance attributes related to the object are initialized in the construction method. Use static code blocks (static plus curly braces) to initialize static member variables. Static code blocks are inside the class and outside the method. Execute the static code block first, then execute the construction code block (instance code block), and then execute the construction method. The static code block is only executed once. The static code block will be executed as long as the class is loaded, and the construction code block (instance code block) will be executed when the object is instantiated.

class Student{
    
    
    private String name;
    private String gender;
    private static String classRoom;
    //实例代码块
    {
    
    
        this.name = "ZhangSan";
        this.gender = "man";
        System.out.println("instance");
    }
    //静态代码块
    static {
    
    
        classRoom = "4";
        System.out.println("static");
    }
    public Student(){
    
    
        System.out.println("Student init");
    }
    public static void main(String[] args) {
    
    
        Student s1 = new Student();
        Student s2 = new Student();
    }
}

insert image description here
9. The package statement is used to explain which package this file is in.
10. When instantiating the object. The parent class static code block is executed before the subclass static code block, and the static code block is executed first and only once; then the parent class construction code block (instance code block) and parent class construction method are executed; finally, the subclass construction code block (instance code block) and subclass construction method are executed. First execute the static parent class and subclass, then execute the instance of the parent class and the construction of the parent class, and finally execute the instance of the subclass and the construction of the subclass 11. After final modification of the variable, the variable becomes a constant and the constant cannot be modified. 3. Combination: such as using other classes as type definition variables in a class (using other types of objects as members in a
class
)
. Combination can be used as much as possible
14. When instantiating an object (new Base()), it is equivalent to calling a constructor
15. new Rect() is an anonymous object. The disadvantage of anonymous objects is that they must be re-instantiated every time they are used

Rect rect = new Rect();
rect.draw();
rect.draw();
new Rect().draw();
new Rect().draw();//每次使用都要重新实例化

16. A class generates a bytecode file, and each internal class has its own bytecode file

class OuterClass{
    
    
        class InnerClass{
    
    //实例内部类
        }
        static class InnerClass2{
    
    //静态内部类
        }
    }
    interface A{
    
    
        void testA();
    }
    public class Test{
    
    
        public static void main(String[] args){
    
    
            A a=new A(){
    
    
                @Override
                public void testA(){
    
    
                    System.out.println("hello");
                }
            };//匿名内部类,即没有名字的内部类,实现了A接口并重写了A接口中的testA方法
            a.testA();
        }
    }

Local inner classes can only be defined inside the method, and cannot be modified by access qualifiers such as public static. They can only be used inside the method, and local inner classes are almost never used
.

Outclass outClass = new Outclass();
Outclass.InnerClass innerClass1 = outClass.new InnerClass();//方法1
Outclass.InnerClass innerClass2 = new Outclass().new InnerClass();//方法2

(2) In the internal class of the instance, to define a static member variable, you must add final, because the variable modified by static does not depend on the object, but when obtaining the internal class object of the instance in other classes that are not external classes, you must rely on the external class object, causing contradictions, (the internal class is equivalent to the ordinary member variable of the external class, and the ordinary member variable will not be loaded when the class is loaded. There is static in the instance internal class, and static is created when the class is loaded, causing contradictions) After adding final, the final modification is a constant, and this constant does not need a class Loading, you can know that the value of data is 6 at compile time

public static final int data = 6;

(3) When the member variables of the inner class of the instance and the member variable of the outer class have the same name, access its own member variables and member variables of the outer class in the member methods of the inner class of the instance (the inner class of the instance contains the this of the outer class)

class Outclass {
    
    
    public int data1 = 1;
    class InnerClass {
    
    
        public int data1 = 11;
        public void test() {
    
    
            System.out.println(this.data1);
            System.out.println(Outclass.this.data1);
        }
    }
}
class Test1 {
    
    
    public static void main(String[] args) {
    
    
        Outclass.InnerClass innerClass2 = new Outclass().new InnerClass();
        innerClass2.test();
    }
}

The printed result is:
insert image description here
(4) The members of the external class can be directly accessed in the method of the instance internal class (when the names of the external class members and the instance internal class members are different) (5) In the external class, the members of the instance internal class cannot be directly accessed. If you want to access, you must obtain the object of the instance internal class (note the difference from (1) Obtaining the instance internal class object) The following is a member method of the external
class
:

public void test(){
    
    
    InnerClass innerClass = new InnerClass();
    System.out.println(innerClass.data);
}

18. Static inner class
(1) When obtaining static inner class objects in other classes that are not outer classes, there is no need to rely on outer class objects

OuterClass.InnerClass innerClass = new OuterClass.InnerClass();

(2) To access the non-static member variables of the external class in the member methods of the static internal class, a new external class object is required, and the non-static member variables of the external class are accessed through this external class object (that is, the non-static member variables of the external class are accessed by the external class object). In the member method of the static inner class, you can directly access the static member variable of the outer class.
In the member method of the static inner class, you can access the non-static member variable of the outer class:

public void test(){
    
    
    OuterClass outerClass = new OuterClass();
    System.out.println(outerClass.data);
}

19. Anonymous inner class
The data that can be accessed in the anonymous inner class is the data that has not been modified (capture of variables), and the final modified constants that can be accessed in the anonymous inner class by default

int val = 10;
    //val = 100;加上这句之后匿名内部类使用val会报错
    A a = new A(){
    
    //在匿名内部类中重写A类中的test方法
        @Override
        public void test(){
    
    
            System.out.println(val);
        }
    };
    //val = 100;加上这句之后匿名内部类使用val也会报错,因为是全局编译,这句放在这后面也不行

2. Common methods of array tool classes

Arrays.sort(arrays);//数组排序,arrays是数组名,即数组的引用

System.out.println(Arrays.toString(arrays));//将数组转换为字符串输出

int[] copy = Arrays.copyOf(array,array.length*2);//拷贝array数组,拷贝的长度为array.length*2,如果拷贝的长度太长则扩容
int[] copy = Arrays.copyOfRange(array,13);//拷贝array数组中1到3下标的内容,但因为Java[1,3),所以拷贝到的只是1下标和2下标的内容,如果拷贝的长度太长则扩容

System.out.println(Arrays.binarySearch(array,15));//Arrays工具类实现的二分查找,在数组中找15
Arrays.binarySearch(int[] a, int fromIndex, int toIndex, int key)//在数组指定范围内进行二分查找

boolean flg = Arrays.equals(array1,array2);//比较两个数组对应下标的数据是否一样

int[] arr = new int[]{
    
    1, 2, 3};
Arrays.fill(arr, -1);//帮数组填充数据,这里将数组内容全部填充(这里是修改)为-1
fill(int[] a, int fromIndex, int toIndex, int val)//在数组指定范围内进行填充,指定的范围是[)

Guess you like

Origin blog.csdn.net/zhanlongsiqu/article/details/131055585