JAVA construction method, class inheritance, at the end of Chapter 4 and Chapter 5

Posted by Lefu on Mon, 10 Jan 2022 14:13:54 +0100

Face to face questions

What are the similarities and differences between abstract classes and interfaces?

If two objects are different, their hash code s must be different?

Small error, same probability

If the hash code s of two objects are different, the objects must be different

What should I pay attention to if I rewrite equals?

If you override equals, you must override the hash code method

Why rewrite hash code?

Because the use of hash map (a data structure) requires the cooperation of equals and hash code

How to work together in hash map?

equals() is required to judge the equality of key values

hashcode() is required for hashmap storage

Objects like key will be allocated to the same bucket and array unit according to hashcode

Packing and unpacking?

The basic type is transferred to packing type, and the transfer of packing type to basic type is called unpacking

What are 128 traps?

The output is true or false between - 128 -- 127

Integer, float, long, double, short, byte, character, void, boolean

In order to save the existence of this range, a constant pool of its own will point to an address, so true will go out of the range and become false

Use intValue() for comparison and unpacking comparison

public class Test8{

public static void main(String[] aaa){

Integer a = 5211314;

Integer b = 5211314;

System.out.println(a.intValue()==b.intValue());

}

}

The main reason for using packages is to ensure the uniqueness of class names. Suppose two programmers create the Employee class at the same time.

Java will have duplicate name classes, which are distinguished by packages

Package introduction

  • The first way is to add the complete package name before each class name. For example:
     java.tiie.LocalDate today = java.tine.Local Date.now();

  • A simpler and more common way is to use the import statement. The import statement is a concise description that refers to the classes contained in the package. Once the import statement is used, it is not necessary to write out the full name of the package when using the class. You can use the import statement to import a specific class or an entire package. The import statement should be at the top of the source file (but after the package statement). For example, you can use the following statement to import Java All classes in the util package.
    import java.util .*;

Static package exists after import

The Person class is called the superclass and the base class

Son1 is called subclass derived class

When a subclass is executed, the parent class must give priority to the execution of the subclass, and the subclass must inherit all of the parent class

When the subclass defines the same thing as the parent class, the parent class will disappear when the subclass is executed, and the parent class will be overwritten

public class Person{

public Person(){

System.out.println("I am the construction method");

}

public void m1(){

System.out.println("I'm a parent Person");

}

public int age = 9;

}

public class Son1 extends Person {

public void m1(){

System.out.println("I'm a subclass Son1");

}

}
public class Test8{

public static void main(String[] aaa){

Son1 xx = new Son1();

System.out.println(xx.age);

xx.m1();

}

}

The subclass calls the method of the parent class that repeats itself, using super m1(); That is, the subclass constructor calls the parent class plus super()

polymorphic

The reference of the parent class can point to the address of the value of the child class

 

There is a simple rule to judge whether it should be designed as an inheritance relationship. This is the "is-a" rule, which indicates that each object of a subclass is also an object of a superclass.

A class can only have one parent class

A parent class has many derived classes

object is the parent of all derived classes. Except for other derived classes, there is only one parent

 

  • equals()

Compare whether the addresses of two values are the same address

Compare whether the values of two strings are equal

equals is overridden in the String class

==Basic type comparison value

Reference type comparison address

  • getClass()

Get class information

  • notify()

Wake up a thread

  • notifyAll()

Wake up all processes

  • wait()

Let the current thread enter the waiting queue and enter from the ready queue

Final 

Cast type

Address satisfying the heap value of ancestor class reference = descendant class

Judge whether it is an object of the current class or a descendant class

 

abstract class

An abstract class defines itself as a parent class and has subclasses to implement it

Each animal will move, and each animal will move in a different way to achieve it by itself

The derived class of an abstract class wants to inherit. The derived class must have every function defined by the abstract class

public class PersonTest{
   public static void main(String[] args){
          Person[] people = new Person[2];

          people[0] = new Employee("Harry Hacker",20000,1919,10,1);
          people[1] = new Student("Harry Hacker","computer science");

          for(Person p:people){
          System.out.println(p.getName()+","+p.getDescription());
        }
    }
}

 

public abstract class Person{
   
  public abstract String getDescription();
                private String name;
             
              public Person(String name){
                 this.name = name;
              }
             public String getName(){

                return name;
 }
}
public class Student extends Person
{
  private String major;
public Student(String name , String major)
{
   super(name);

  this.major = major;
}
  public String getDescription()
{
return "a student majoring in "+major;
}
}
import java.time.*;
public class Employee extends Person{
           private double salary;
           private LocalDate hireDay;
public Employee(String name , double salary , int year, int month, int day){
           super(name);
           this.salary=salary;
           hireDay = LocalDate.of(year,month,day);
         }

     public double getSalary(){
       return salary;
}
     public LocalDate getHireDay(){
       return hireDay;
}
    public String getDescription(){
         return String.format("an employee with a salary of $%.2f", salary);
  }
public void raiseSalary(double byPercent)
 {
   double raise = salary * byPercent/100;
   salary+=raise;
}
}

Hash code

Is an integer value derived from the object. Hash codes are irregular. If x and y are two different objects, x.hashCode() and y.hashCode() will not be basically the same.

 

Because the equals value rewrites the hash code, it is compared with the String value, so the s and t hash codes are equal

 toString

 

generic paradigm

Broad representation of all types

public class Arr{

public E a;

}

public class Test8{

public static void main(String[] aaa){

Person x=new Person();

x.age = 20;

Arra1 = new Arr<>();

a1.a = x;

System.out.println(a1.a);

Arra2 = new Arr<>();

a2.a = "Oedo ";

System.out.println(a2.a);

Arr a3 = new Arr<>();

a3.a = new int[]{1,2,3,4};

System.out.println(a3.a[0]);

}

}

Variable number of parameters

public class Test8{

public static void main(String[] aaa){

m1("Song Yu");

m1("212121");

m1("123456");

m1("11111");

}

public static void m1(String ... a){

for(int i=0;i

System.out.println(a[i]);

}

}

}

Enumeration class specifies an instance

Topics: Java Back-end