java foundation notes - object oriented 1

Tetrahedral object

1. Basis of class

Structure of class 1.1

class Person{
	Member properties
    Member method
}
Person p = new Person();

1.2 anonymous objects

new Student().toString();

2. Method of class

2.1 local variables of the method

​ The local variable of the member method has no default initialization assignment and must be explicitly assigned

​ Formal parameters are also local variables. However, initialization and assignment are not required!!! Assign value when calling

2.2 memory loading location of member methods

​ Class, loaded in heap (except static)

​ However, the local variables of the method are loaded in stack space

2.3 method overload

​ Multiple functions with the same name are allowed in the same class, as long as the parameter list is different (independent of the return value!)

2.4 variable number parameters

​ 0 or more will do

​ However, it must be declared at the end of the formal parameter list

public void show(String ... str){
}
public void show(String str){ //Can coexist. If only one parameter is passed, the compiler will choose this method
}
public void show(String[] str){//No, the compiler will think the same, and this is also the way jdk handled variable number parameters before 1.5
}

3. Packaging

​ High cohesion, low coupling

3.1 authority

​ Permissions for class members

​ private, default, protected, public

Modifier Class interior Same package Subclasses of different packages Different packages
private Y
default Y Y
protected Y Y Y
public Y Y Y Y

​ class has only public and default permissions

​ public classes can be accessed anywhere

​ The default class can only be accessed by classes under the same package

4. Constructor, construction method, constructor

​ Construction method, not method...

​ After a parameterized constructor is provided, the default nonparametric constructor is invalidated unless another nonparametric (overloaded) constructor is defined

class Person{

	public Person(){
		sout;
	}
}

4.2 this call constructor

1. If a class has n constructors, at most n-1 constructors can use this();

​ Prevent circular calls

2. Put at most one this() in each construction method;

class Person{
	private String str;
    private int num;
	public Person(){
		sout;
	}
    public Person(String str){
        this();
    }
        public Person(String str,ing num){
        this(str);// The second constructor was called
        this.num=num;
            
    }
}

5. Succession

5.1 override

​ The return value of the method overridden by the parent class is void or basic type, and the overridden method of the child class cannot be changed

​ The return value of the overridden method of the parent class is the reference type, and the return value of the overridden method of the child class can only be the reference type or its subclass

​ The static method of the parent class cannot be inherited unless the child class is also static (it is not overridden)

​ The permission of overriding a method can only be increased, and the private method of the parent class cannot be overridden

5.2 super

​ Note: the id with the same name is the same as the nearest one

​ super() must be on the first line of a subclass constructor

​ There can only be one super() and this(), but not both. When they are not written, the null parameter construction method of the parent class will be called by default

class Fu{
    private int a;
    private int id;
    public Fu(int a,int id){
        this.a = a;
        this.id = id;
    }
}
class Zi extends Fu{
    private int b;
    private int id;
    public Zi(int a , int b,int id1,int id2){
        super(a,id1);
        this.b=b;
        this.id=id2;
    }
}

5.3 memory inclusion

​ When a subclass is instantiated, all available properties of the parent class will be loaded in the pair space

6. Polymorphic Polymorphism

6.1 Foundation

​ Polymorphism is a runtime behavior

​ Fu fu =new Zi()

6.2 virtual method call

Polymorphism: calling the method overridden by the child and parent class is actually executing the method of the child class and becomes a virtual method call (when you left click ctrl in the IDE to view the method declaration, the method of the parent class will be opened)

Compile to the left and run to the right (compile to see the variable types on the left and run to execute the methods of the classes on the right)

public class Person{
    int age;
    String name;
    public void eat(){
        sout("eat");
    }
}
public class Man extends Person{
    boolean isSmoking;
    public void smoke(){
        sout("smoke");
    }
    public void eat(){
        sout("Man eat");
    }
}
public class Woman extends Person{
    int height;
    public void eat(){
        sout("Woman eat");
    }
}

public static void main(String[] args){
    //Polymorphism,
   	Person p= new Man(); 
    p.eat()
    p.smoke()//An error is reported because p has no smoke method
}

6.3 why use polymorphism

​ Few people write Fu fu =new Zi() directly

​ In fact, it is used in the formal parameter passing of the method (declared is the parent class, and passed in is the object of the child class)

​ The same is true for the return value. The method declares that the return value is the parent class, but a new subclass object is returned in the code

​ Example 1:

​ equals(Object obj);

​ Example 2:

When java connects to the database, the interface methods provided by java are unified, but the specific execution methods of different database manufacturers are different, so different manufacturers are required to rewrite the parent class methods provided by java

import java.sql.Connection;
public void do(Connection conn){
  	pass;
}
psvm(){
    Connection conn1 = new MySQlConnection();//Different databases
    do(conn1);
    Connection conn2 = new OracleConnection();//Different databases
    do(conn2);
}

6.4 downward transformation instanceof

​ Polymorphic objects cannot call subclass specific methods and properties, such as the smoke method above

​ Note that the subclass's methods have been loaded into memory, but the declared types can't be called

​ So it needs transformation

Fu fu = new Zi();
if(fu instanceof Zi ){ //It is not judged that an exception of ClassCastException may be encountered
    Zi zi = (Zi)fu;
}

a instanceof A: judge whether object a is class A

7. Object and packaging

java.lang.Object

7.1 equals (object obj) and==

Equals (object obj) compares the addresses of two objects. The bottom layer uses==

However, equals is rewritten in the String class. The comparison is the value of the String. Date and File, as well as various wrapper classes, are rewritten

class Objet{
    public boolean equals(Object obj){
        return (this == obj)
    }
}

==Reference types compare addresses, and basic data types compare values

be careful:

int  i = 10;
double j = 10.0;
sout(i == j)//true, because the automatic type is promoted

7.2 toString( )

When you output a reference to an object, you actually call the tostring() method of the object

public String toString() {
	return getClass().getName() + "@" + Integer.toHexString(hashCode());//Calculate the hash value in the space, find the position, turn to hexadecimal, and then turn to Int
}

String, Data, File and wrapper classes also override the toString method

7.3 packaging Wrapper

Byte,Short,Integer,Long,Float,Double,Boolean,Character

transformation:

Basic data type -- packing -- -- > Packing class: Integer i= 9; Just (auto boxing), new Integer(9) is out of date

Packing class ----------- unpacking ------------- basic data type: int i = just assign an Integer directly (automatic unpacking)

Packing class ------ > string: integer in = 9; in.toString()

String --------- > Packing class: Integer.valueOf("21");

String ------ > basic data type: Intger i = Integer.parseInteger("123");

Basic data type -------- > string: String STR = string.valueof (3.4f);

7.4 automatic packing and unpacking

Integer i =new Integer(9);
Integer j =new Integer(9);
sout(i == j); //false


Integer n =1;
Integer m =1;
sout(m == n); //true
//Automatic packing,

Integer x = 128
Integer y = 128;
sout(x == y); //false
//There is a static inner class IntegerCache of Integer in Integer
// There is an Integer[] -128 to 127 in it
//If you use the automatic packing method, the numbers assigned to Integer in this range will directly use the (static) in this array, and there is no need to go to new! 

Keywords: Java

Added by susi on Sun, 28 Nov 2021 12:17:31 +0200