19-23 review of knowledge points

Posted by mmponline on Sat, 15 Jan 2022 03:28:03 +0100

1, Static static

1. Characteristics

(1) Loads as the class loads

(2) Prior to the existence of the object: it cannot coexist with this (this represents the object address value reference of the current class). When the object does not have new, the current static modified member already has memory

(3) Statically decorated and can be shared by multiple objects

(4) The statically modified variables and methods are static variables and static methods;

Member variables and member methods are non static

2. Access mode

Class variable

Class Method ();

3. Precautions for use

(1) Non static methods: you can access both static and non static variables

You can call either static or non static methods

(2) Static methods: can only access static variables and call static methods

2, Code block

1. Definition

In the java file, {....} The wrapped content is called a code block

2. Classification

(1) Local code blocks: using in methods

(2) Construction code block: member variable location of class

(3) Static code block: the member position of the class, using static {...}

Static code block > construction code block > local code block

III. inheritance

1. What is inheritance

The common memory of multiple classes is extracted into an independent class, and then the multiple classes and independent classes produce an inheritance relationship

2. Benefits

(1) Improve code maintainability

(2) Improve code reusability

(3) The relationship between classes takes dynamics as the premise

3. Characteristics

Classes and classes only support single inheritance, not multiple inheritance, but multi-layer inheritance

4. Precautions for use

(1) Subclasses inherit from the parent class and can inherit non private members of the parent class. Private members can only be accessed in this class

(2) Constructors cannot be inherited, but subclasses can access parent constructors indirectly through the super keyword

5. Access to constructor in inheritance

(1) the subclass inherits from the parent class, and all construction methods of the subclass (super(), super: the spatial representation of the parent object represented by the parent object (the address value reference of the parent object) will access the parameterless method of the parent class by default; In the inheritance relationship, the parent class must be initialized and hierarchically initialized first (the parameterless construction of the parent class is executed first, and then the construction method of the child class is executed)

(2) If the parameterless construction of the parent class does not exist, the subclass will report an error (because all construction methods of the subclass default to the parameterless construction method of the parent class)

Method 1: manually give the parameterless construction method of the parent class

Method 2: in the first sentence of all the construction methods of the subclass, you can indirectly access the parametric construction of the parent class by adding super (xxx)

Method 3: as long as all construction methods of the subclass can initialize the parent class

In the parametric construction method of the subclass: add his () to access the nonparametric construction of the class, and then add super (xxx) to the nonparametric construction method to access the parametric construction of the parent class;

Example 1

class Fu{

    //Parameterless construction method of parent class
    public Fu(){
        System.out.println("Fu Nonparametric construction method of...");
    }

    public Fu(String name){
        System.out.println("Fu Parametric construction method of...");
    }
}
//Subclass
class Zi extends  Fu{
    public Zi(){
        System.out.println("Zi Nonparametric construction method of");
    }
    public Zi(String name){
        super(name);//The calling parent class has parameters
        System.out.println("Zi Parametric construction method of");
    }
}

//Test class
public class ExtendsDemo {
    public static void main(String[] args) {
        //Create subclass objects
        Zi zi = new Zi() ;
        System.out.println("-----------");
        //Creating objects with parametric constructs
        Zi zi2 = new Zi("hello") ;
    }
}

Operation results

Fu Nonparametric construction method of...

Zi Nonparametric construction method of

---------------

 Fu Parametric construction method of...

"Zi Parametric construction method of

Example 2

class Father{

    public Father(String name){
        System.out.println("Fu Parametric construction method of...");
    }
}

class Son extends  Father{
    public Son(){
        super("name") ;//Accessing the parameterized constructor of the parent class
        System.out.println("Zi Nonparametric construction method of");
    }
    public Son(String name){
        this() ;//Access the parameterless constructor of this class
        System.out.println("Zi Parametric construction method of");
    }
}

//Test class
public class ExtendsDemo2 {
    public static void main(String[] args) {

        Son s = new Son() ;
        Son s2 = new Son("123456") ;
    }
}
result

Fu Parametric construction method of...
Zi Nonparametric construction method of
------------------------
Fu Parametric construction method of...
Zi Nonparametric construction method of
Zi Parametric construction method of

6. The difference between this and super

(1) Address value reference

this represents the address value reference of the current class object

super represents the address value reference of the parent object

(2) Accessing member variables

this. Variable name; Access member variables in this class

super. Variable name; Accessing member variables in the parent class

(3) Access construction method

this(); Access the parameterless constructor of this class

super(); The parameterless constructor that accesses the parent class

this(xxx); Access the parameterized constructor of this class

super(xxx); Construct a method with arguments to the parent class

(4) Member method

this. Method name (); Access member methods of this class

super. Method name (); Accessing member methods of the parent class

7. Test site

(1) Inheritance initialization problem

Hierarchical initialization, let the parent class initialize first, and then the child class initialize

(2) Priority of code block

Static code block > construction code block > construction method (static code block is executed first and then executed once)

class Person{
    static{
        System.out.println(1");
    }
    public Person(){
        System.out.println("2");
    }
    {
        System.out.println("3");
    }
}
class Student extends  Person{
    {
        System.out.println("4");
    }
    static{
        System.out.println("5");
    }
    public Student(){
        System.out.println("6");
    }

    public Student(int i) {
        System.out.println("7");
    }
}
public class ExtendsTest2 {
    public static void main(String[] args) {
        Student s = new Student() ;
        System.out.println("--------------");
        Student s2 = new Student(10) ;
    }
}


result
//153246
//3247

8. Composition of class

1. Member variable, construction method, member method

2. In inheritance

(1) the names of member variables (child and parent) are consistent and follow the principle of proximity

(2) construction method: all constructions of subclasses default to the parameterless construction method of the parent class

The parameterless constructor of the subclass accesses the parameterless constructor of the parent class

The parameterized constructor of the subclass accesses the parameterized constructor of the parent class

(3) member method: if the member method names of the subclass and the parent class are inconsistent, they can be called separately

If the names are inconsistent, first find -- > in the child class, and then find -- > in the parent class. If there is no parent class, an error will be reported

9. Rewrite

1) What is rewriting

If there is the same method declaration as the parent class in the subclass, it is called method override

2) purpose

The subclass has its own functions and needs to override the functions of the parent class

3) The difference between method rewriting and method overloading

Method rewrite: as like as two peas in the inheritance relation, the subclass has the same method declaration as the parent class (purpose: the subclass has its own function, and needs to override the function of the parent class).

Method overloading: multiple functions are provided in a class. These functions have the same method name, different parameter lists and are independent of the return value (purpose: to improve the scalability of a function)

The parameter list is different:
1) Different types
2) Different number
3) Consider the order of parameter types

4) final keyword

1. There are some functions that do not want the subclass to override the parent class. The function of the parent class is independent and provides the keyword final (final and unchangeable)

2. Characteristics

(1) Class can be modified, but class cannot be inherited

(2) Member methods can be modified. Member methods cannot be overridden

(3) You can modify a variable, which is now a constant

3. Modify basic data type and reference data type

(1) final modifies the basic data type. The data value corresponding to the basic data type can no longer be assigned

(2) The final modifier refers to the data type, and the address value of the reference data type will not change

10. When to use inheritance relationship

(1) Do not use inheritance for partial functionality

(2) If class A is a kind of class B, or class B is a kind of class A, inheritance can be used at this time

4, Polymorphism

1. What is polymorphism

Different forms of a thing at different times

2. Prerequisites for polymorphism

(1) The inheritance relationship extends must exist

(2) There must be a method override, and the subclass needs to override the function of the parent class

(3) A parent class reference must point to a child class object

format

class Fu{}

class Zi extends Fu{
}

Fu f = new Zi();

3. Polymorphic member access characteristics

(1) For member variables

Compile to the left, run to the left (things that use parent classes)

(2) Methods for members in polymorphic

Compile to the left and run to the right (subclass overrides parent class)

(3) Member methods are static (static member methods are not method rewrites and can be accessed directly by class names)

Compile to the left, run to the left

(4) Construction method

If there is an inheritance relationship, the parent class needs to be initialized first, and then the child class needs to be initialized

4. Benefits

(1) improve code reusability (guaranteed by inheritance)

(2) Improve code extensibility (guaranteed by polymorphism)

5. Malpractice

You cannot access subclass specific functions

How to solve

1. Concrete subclasses create concrete subclass objects

2. "Downward transformation * -------- > target data type variable name = (target data type) initialization value;

                 Zi  z =   (Zi) f ; Revert to subtype

6. Possible exceptions during downward transformation

To use downward transformation, you must have a parent class reference pointing to the child class object Fu f = new Zi();

Follow the format of downward Transformation: Zi z = (Zi)f;


If the downward transformation is not used properly, Java Lang.classcastexception: class conversion exception: (it belongs to runtime exception)
Problems will occur when the instance in the current heap memory is not of this type!

5, Abstract

1. What is abstraction

Some functions of something are only declared, and there is no method body

2. Keywords, formats

1. Keyword abstract

2. Format:

Abstract class: abstract class name {}

Abstract method: permission modifier (for example, public) absteact returns value type method body (formal parameter list);

3. Characteristics of abstract classes

(1) A class with abstract methods must be an abstract class

(2) Abstract classes do not necessarily have abstract methods, but can also have non abstract methods

(3) Abstract classes cannot be instantiated and objects cannot be created

You can instantiate by creating a specific subclass (create an object), and the abstract class is polymorphic Fu fu = new Zi(); Fu type abstract type

(4) Two cases of abstract words

Abstract subclasses are abstract classes and have no meaning

Only when the subclass of an abstract class is a concrete class can it be new

4. Core purpose

Force the subclass to override all the abstract methods of the parent class

5. Characteristics of abstract class members

(1) Member variables:

Variables can be defined; Constants can also be defined and modified by fina

(2) Member method:

You can define either abstract or non abstract methods

If it is defined as an abstract method: keyword abstract (shown)

(3) Construction method:

There are parametric structures / nonparametric structures, hierarchical initialization

6. abstract and keywords

Conflicting:

(1) Private: private, which can only be accessed in this class; The purpose of adding abstract needs to be overridden by subclasses and the functions of subclasses need to be called

(2) Static: static methods are loaded with the loading of classes. Static methods are not rewritten. Their access is through

Class name Method name (); The pumping method has no method body

(3) Final: the member method modified by final cannot be overridden, but the abstract method needs to be overridden

You can follow:

(1)public

(2) Default modifier

(3)protected 

Vi. interface

1. The essence of interface

Reflect the additional expansion function of a real-world thing

2. Format of interface

Interface interface name {}

The method in the interface cannot have a method body, hide the public abstract keyword, and can only be an abstract method

3. Characteristics of abstract classes

(1) Cannot instantiate (cannot create object)

(2) How to instantiate: instantiate through the sub implementation class (concrete class) of the interface ---------- interface polymorphism

4. Relationship between subclasses and interfaces

implements implementation relationship

Format: class interface name implements interface {}

Example:

interface Inter{  }

class InterImpl implements Inter{  }

5. Member characteristics of interface

Member methods: can only be abstract methods (public abstract is omitted and not written)

Member variable: can only be constant (public static final is omitted and not written)

Construction method: no construction method

7, Class

Concrete class, abstract class, interface

Specific class: class name {}

Abstract class: abstract class name {}

Interface: interface interface name {}

(1) Between classes: Extensions inheritance relationship; Only single inheritance is supported, and multiple inheritance is not supported, but multiple inheritance can be supported

(2) Relationship between class and interface: implementation relationship; A class can inherit multiple interfaces while inheriting another class, separated by commas

(3) Between interfaces: Extensions inheritance relationship; It supports not only single inheritance, but also multiple inheritance, separated by commas

8, The difference between abstract classes and interfaces

1. Member difference

In abstract classes

Member variable: it can be either a constant or a variable

Member method: it can be either an abstract method (Abstract cannot be omitted) or a non abstract method

Construction method: there are both nonparametric and parametric structures; The purpose is to initialize the data of the current class

In interface

Member variable: can only be constant (the modifier public static final can be omitted)

Member methods: can only be abstract methods (public abstract is omitted and not written)

Construction method: no construction method

2. Relationship

Specific class: class name {}

Abstract class: abstract class name {}

Interface: interface interface name {}

(1) between classes: Extensions inheritance relationship; Only single inheritance is supported, and multiple inheritance is not supported, but multiple inheritance can be supported

(2) relationship between class and interface: implementation relationship of implements; A class can inherit multiple interfaces while inheriting another class, separated by commas

(3) interface to interface: extends inheritance relationship; It supports not only single inheritance, but also multiple inheritance, separated by commas

3. Design concept

Interface classes cannot be instantiated. They are instantiated through specific subclasses. There is an inheritance relationship, reflecting the "is a" relationship

Interface: it cannot be instantiated. It is instantiated through a specific subclass. Whoever implements the interface has additional functions and the relationship of "like a"

9, Research on formal parameter problem - reference type

If the formal parameter of a method is a class, how to pass the parameter

1. Concrete class: when calling this method, the actual parameters need to pass the object of the current collective class

class Student{
    public void study(){
        System.out.println("Love life");
    }
}

class StudentDemo{
    public void method(student s){
        s.study();
    }
}

public class StudentTest{
    public static void main(String[] ages){
        //Create StudentDemo object
        StudentDemo sd = new StudentDemo();
        //Create Student object
        Student s = new Student();
        sd.method(s);
    }
}

2. Pumping to class: the actual parameters of this method need to pass the sub object of the abstract class (Abstract polymorphism)

//Define the Person abstract class
public abstract class Person{
    public abstract void work();//Define an abstract method
}
//Define the PersonDemo class
class PersonDemo{
    public void show(Person person){
        person.work();
    }
}

//Defines a subclass of the Person abstract class
class Programmer extends  Person{
    public void work() {
        System.out.println("Programmer learning java");
    }
}
//test
public class PersonTest {
    public static void main(String[] args) {
        //Create a PersonDemo class object
        PersonDemo pd = new PersonDemo() ;
        //Abstract class polymorphism
        Person p = new Programmer() ;
        pd.show(p);
     }
}

3. Interface: when calling this method, the actual parameters need to pass the sub implementation class object of the current interface (interface polymorphism)

//Define an interface
interface love{
    void love();
}

//Define a LoveDemo class
class LoveDemo{
    public void function(Love L){
        L.love();
    }
}

//Define the sub implementation class object of an interface
class LovePerson implements Love{
    public void love(){
        Systen.out.println("Love life love you");
    }
}

//test
public class LoveTest {
    public static void main(String[] args) {
        //Create a LoveDemo object
        LoveDemo ld = new LoveDemo();
        //Interface polymorphism
        Love l = new LovePerson();
        id.function(l);
    }
}

If the return value of a method is a reference type, how to return

1. Concrete class: the method returns the current concrete class object (new class name)

class Student{
    public void study(){
        System.out.println("Good Good Study,Day Day Up!");
    }
}

//StudentDemo class
class StudentDemo {
    public Student show() { //The return value of the method is the reference type - concrete class: the concrete object of the current class to be returned
        // Student s = new Student() ;
        //return s ;
        //Anonymous object
        return new Student();
    }
}
//Test class
public class StudentTest {
    public static void main(String[] args) {
        //Create StudentDemo class object
        StudentDemo sd = new StudentDemo() ;
        Student s = sd.show(); 
        s.study();
        System.out.println("-----------------------");
        //Anonymous object
        Student s = new StudentDemo().show();
        s.study();
    }
}

2. Abstract class: subclass object that needs to return the abstract class

abstract class Person{
    public  abstract void work() ;
}
//PersonDemo class
class PersonDemo{
    public Person method(){ 
        //Abstract class polymorphism
        // Person p = new Worker() ;
        //return p ;
        //Anonymous object
        return new  Worker() ;
    }
}
//Define a subclass that inherits the Person class
class Worker extends  Person{
    public void work() {
        System.out.println("Workers keep going to work");
    }
}
//Test class
public class PersonTest {
    public static void main(String[] args) {
        PersonDemo pd = new PersonDemo();
        Person p = pd.method();
        p.work() ;
        System.out.println("---------------------------------");
        //Anonymous object
        Person p = new PersonDemo().method();
        p.work() ;
    }
}

3. Interface: the subclass implementation object of the interface needs to be returned

interface  Mary{
    public abstract  void mary() ;
}
//MaryDemo class
class MaryDemo{
    public Mary function(){
        //Subclass objects that need to provide interfaces
        //Interface polymorphism
       // Mary mary = new You() ;
       // return mary ;
        //One step
        return new You() ;
    }
}
//Define a class
class You implements  Mary{
    public void mary() {
        System.out.println("Get married,Very happy...");
    }
}

//Test class
public class LoveTest {
    public static void main(String[] args) {
         MaryDemo md = new MaryDemo();
         Mary m =  md.function();
         m.mary();
         System.out.println("----------------------");
         //Anonymous object
         Mary m = new MaryDemo().function();
         m.mary();
    }
}

10, Package

Access rights

In the current class under the same packageIn subclasses / unrelated classes under the same packageIn subclasses under different packagesIn unrelated classes under different packages
private        √
Default modifier        √          √
protected         √          √      √
public        √          √      √        √

11, Inner class

1. Definition

In one class, you can define another class; The inner class can access all members of the outer class, including private.

//External class
class Outer{
    //Member variable
    public int num = 100 ;
    private int num2 = 200 ;

    //Member inner class
    class Inner{ 
        public void method(){
            System.out.println("method Inner");
            System.out.println();
            System.out.println(num2);
        }
    }
    //Member methods of external classes
    public void show(){
        //Method of the internal class of the accessed member --- > accessed by creating an internal class object
        //method() ; Wrong -- Method method method of this class accessed
        Inner inner = new Inner() ;
        inner.method();
    }
}

2. How do external classes directly access member methods of internal classes

Format: external class name Internal class name = object name = external class object Internal class objects;

class Outer2{
    private int num = 20 ;

    //Member inner class
    class Inner2{
        public void show(){
            System.out.println(num);
        }
    }

    //Member methods of external classes
    public void method(){
       System.out.println("111");
    }
}

//Test class
public class InnerClassDemo2 {
    public static void main(String[] args) {

        //External class name Internal class name object name = external class object Internal class objects;
        //Applicable to: directly accessing members of the internal class of a member through an external class (prerequisite: the internal class of the current member is a non static class)
        Outer2.Inner2 oi = new Outer2().new Inner2() ;
        oi.show() ;
    }
}

3. How to directly access members of static member inner classes

The static member inner class is regarded as the static member access of the outer class

Format:

External class name Internal class name object name = new external class name Internal class name ();

class Outer{
    //Define non static member variables
    public int num = 50 ;
    private static int num2 = 20 ;

    //Defining member inner classes: static       
    static class Inner{
        public void show(){
            System.out.println(num2);
        }
        public static void show(){
            System.out.println(num2);
        }
    }
}
//Test class
public class InnerClassDemo {
    public static void main(String[] args) {
        //External class name Internal class name object name = new external class name Internal class name ();
        Outer3.Inner3 oi = new Outer3.Inner3() ;
        oi.show();
        oi.show2() ; //Static -- object name access is not recommended
        System.out.println("------------------------------");
        //Another way of show2()
        Outer3.Inner3.show2(); //show2() static method

    }
}

Topics: Java