JAVASE Foundation - day08 (Object Oriented)

Keywords: Java jvm Mobile Attribute

08.01 Object Oriented (Overview and Classification of Code Blocks) (Understanding) (Interview questions, development is not used or rarely used)

  • A: Overview of Code Blocks
    • In Java, code enclosed with {} is called a block of code.
  • B: Code Block Classification
    • According to its location and declaration, it can be divided into local code block, construction code block, static code block, synchronous code block (multi-threaded explanation).
  • C: Application of Common Code Blocks
    • a: Local code block
      • Appears in the method; Limits the variable life cycle, releases early, enhances the memory utilization
    • b: Construct code blocks (initialization blocks)
      • Outside methods in a class; the same code in multiple constructor methods is stored together, and each call is executed and executed before the constructor method is constructed.
    • c: Static code block
      • Appears outside the method in the class, and modifies it with static; it is used to initialize the class, execute it when it is loaded, and execute it only once.
      • Usually used for loading drivers

08.02_Object-Oriented (Code Block Interview Questions) (Mastery)

  • A: Look at the results of the program.
  • class Student {
        static {
            System.out.println("Student Static code block");
        }
    
        {
            System.out.println("Student Construct code blocks");
        }
    
        public Student() {
            System.out.println("Student Construction method");
        }
    }
    
    class Demo2_Student {
        static {
            System.out.println("Demo2_Student Static code block");
        }
    
        public static void main(String[] args) {
            System.out.println("I am main Method");
    
            Student s1 = new Student();
            Student s2 = new Student();
        }
    }
    

08.03_Object-Oriented (Demonstration of Inheritance Cases) (Mastery)

  • A: Extensions
    • Let classes have relationships with classes, child-parent relationships
  • B: Inheritance Case Demonstration:
    • Animals, cats, dogs
    • Define two attributes (color, number of legs) and two functions (eating, sleeping)
  • C: Case demonstration
    • Before using inheritance
  • D: Case demonstration
    • After using inheritance
class Demo1_Extends {
    public static void main(String[] args) {
        Cat c = new Cat();
        c.color = "FLOWER";
        c.leg = 4;
        c.eat();
        c.sleep();

        System.out.println(c.leg  + "..." + c.color);
    }
}
/*
* A:Inheritance (extends)
    * Let classes have relationships with classes, child-parent relationships 
* B:Inheritance Case Demonstration:
    * Animals, cats, dogs
    * Define two attributes (color, number of legs) and two functions (eating, sleeping)
* C:Case demonstration
    * Before using inheritance
* D:Case demonstration
    * After using inheritance
*/
class Animal {
    String color;                   //Animal colour
    int leg;                        //Number of animal legs

    public void eat() {             //The Function of Eating
        System.out.println("Having dinner");
    }

    public void sleep() {           //Sleeping function
        System.out.println("Sleep");
    }
}

class Cat extends Animal {

}

class Dog extends Animal {

}

/*
extends It means inheritance.
Animal Parent class
Cat Dog and Dog are subclasses
*/

08.04_Object-Oriented (Benefits and Disadvantages of Inheritance) (Mastery)

  • A: Benefits of Inheritance
    • a: Enhanced code reusability
    • b: Enhanced code maintainability
    • c: The premise of polymorphism is to make the relationship between classes.
  • B: The Disadvantage of Inheritance

    • Class coupling is enhanced.

    • Principles of development: high cohesion, low coupling.

    • Coupling: the relationship between classes and classes
    • Cohesion: The ability to accomplish something by oneself

08.05_Object Oriented (Inheritance Characteristics of Classes in Java) (Master)

  • A: Inheritance Characteristics of Classes in Java
    • a:Java only supports single inheritance, not multiple inheritance. (A son can only have one father)
      • Some languages support multiple inheritance, formats: extends class 1, class 2,...
    • b:Java supports multi-level inheritance (inheritance system)
  • B: Case demonstration
    • Inheritance Characteristics of Classes in Java
      • If you want to use all the functions of this system to create objects with the lowest class
      • If you want to see the general function of this system, look at the top class.

08.06_Object-Oriented (Notes on Inheritance and When to Use Inheritance) (Mastery)

  • A: Attentions to Inheritance
    • a: Subclasses can only inherit all non-private members of the parent class (member methods and member variables)
    • b: Subclasses cannot inherit the construction method of the parent class, but they can access the construction method of the parent class through the super keyword.
    • c: Don't inherit for some functions
    • Project Manager's Name, Number, Salary Bonus
    • Programmer Name and Number Salary
  • B: When to use inheritance

    • Inheritance actually embodies a relationship: "is a".
      Person
      Student
      Teacher
      Fruits
      Apple
      Banana
      A mandarin orange

    The hypothesis method is adopted.
    If there are two classes A,B. Inheritance can be considered only if they agree that A is one of B or B is one of A.

08.07_Object-Oriented (Relations between Membership Variables in Inheritance) (Mastery)

  • A: Case demonstration
    • a: Variables with different names
    • b: Variables with the same name
      Variables with the same name in the parent class are just examples in the lecture. This is not the case in development.
      Subclasses inherit the parent class in order to use its members, so it is meaningless to define a member variable with the same name.

08.08 Object Oriented (Difference and Application of this and super) (Mastery)

  • A: What do this and super represent?
    • this: Represents the reference to the current object. Whoever calls me, I represent whoever.
    • super: Reference representing the parent class of the current object
  • Differences between B:this and super
    • a: Call member variables
      • This. A member variable calls a member variable of this class, or a member variable of a parent class.
      • Sup. Member variable calls member variable of parent class
    • b: Call the constructor
      • this(... ) Call the construction method of this class
      • super(... ) Call the construction method of the parent class
    • c: Call member methods
      • This. The member method calls the member method of this class, or the method of the parent class.
      • Sup. member method calls member method of parent class

08.09_Object-Oriented (Relationships between Constructions in Inheritance) (Mastery)

  • A: Case demonstration
    • By default, all constructors in subclasses access constructors for parent hollow parameters
  • B: Why?

    • Because subclasses inherit data from the parent class, they may also use data from the parent class.
    • Therefore, before subclass initialization, we must first complete the initialization of parent data.

    • Actually:

      • The first statement of each constructor defaults to be the top-most parent of the super() Object class.

08.10_Object-Oriented (Attentions to Constructing Method in Inheritance) (Mastery)

  • A: Case demonstration
    • The parent class has no parametric construction method. What about the child class?
    • super solution
    • this solution
  • B: Notes
    • super(... ) Or this(... It must appear on the first statement of the constructor
class Demo6_Extends {
    public static void main(String[] args) {
        Son s1 = new Son();
        System.out.println(s1.getName() + "..." + s1.getAge());
        System.out.println("--------------------");
        Son s2 = new Son("Zhang San",23);
        System.out.println(s2.getName() + "..." + s2.getAge());
    }
}
/*
* A:Case demonstration
    * The parent class has no parametric construction method. What about the child class?
    * super Solve
    * this Solve
* B:Matters needing attention
    * super(…)Or this(... It must appear on the first statement of the constructor
*/
class Father {
    private String name;            //Full name
    private int age;                //Age

    //public Father() {// empty parameter construction
    //System.out.println("Father empty parameter construction");
    //}

    public Father(String name,int age) {    //Parametric structure
        this.name = name;
        this.age = age;
        System.out.println("Father Parametric structure");
    }

    public void setName(String name) {  //Set name
        this.name = name;
    }

    public String getName() {           //Get name
        return name;
    }

    public void setAge(int age) {       //Age setting
        this.age = age;
    }

    public int getAge() {               //Age of acquisition
        return age;
    }
}

class Son extends Father {
    public Son() {                      //Empty parameter structure
        this("Wang Wu",25);              //Construction methods in this class
        //Sup ("Lisi, 24);// Call the construction method in the parent class

        System.out.println("Son Empty parameter structure");
        //this.name = "zhangsan";
    }

    public Son(String name,int age) {   //Parametric structure
        super(name,age);
        System.out.println("Son Parametric structure");
    }
}

08.11_Object-Oriented (Interview Questions in Inheritance) (Mastery)

  • A: Case demonstration

  • Look at the result of program writing 1
    class Fu{
    public int num = 10;
    public Fu(){
    System.out.println("fu");
    }
    }
    class Zi extends Fu{
    public int num = 20;
    public Zi(){
    System.out.println("zi");
    }
    public void show(){
    int num = 30;
    System.out.println(num);
    System.out.println(this.num);
    System.out.println(super.num);
    }
    }
    class Test1_Extends {
    public static void main(String[] args) {
    Zi z = new Zi();
    z.show();
    }
    }
    30,20,10
class Test2_Extends {
    public static void main(String[] args) {
        Zi z = new Zi();
    }
    /*
    1,jvm The main method is called, and main is stacked
    2,When you encounter Zi z = new Zi(); you load Fu.class and Zi.class into memory, and then create objects, when Fu.class loads into memory.
    The static code block of the parent class is loaded with Fu.class, and the static code block of the subclass is loaded with Zi.class when Zi.class is loaded into memory.
    The first output, the static code block Fu, and the second output, the static code block Zi
    3,Take the Zi class construction method, because java is hierarchically initialized, first initialize the parent class, then initialize the subclass, so first go the parent class construction, but in the execution
    When a parent class is constructed, it is found that the parent class has a constructor block, which takes precedence over the execution of the constructor method.
    The third output construction block Fu and the fourth output construction method Fu
    4,Fu Class initialization ends, subclass initialization, the fifth output is the construction code block Zi, the construction method Zi
    */
}
class Fu {
    static {
        System.out.println("Static code block Fu");
    }

    {
        System.out.println("Construct code blocks Fu");
    }

    public Fu() {
        System.out.println("Construction method Fu");
    }
}

class Zi extends Fu {
    static {
        System.out.println("Static code block Zi");
    }

    {
        System.out.println("Construct code blocks Zi");
    }

    public Zi() {
        System.out.println("Construction method Zi");
    }
}

Static code block Fu
Static code block Zi
Construct code block Fu
Construction method Fu
Construct code block Zi
Construction method Zi

08.12_Object-Oriented (Membership Method Relations in Inheritance) (Mastery)

  • A: Case demonstration
    • a: Method of different names
    • b: Method of homonymy

08.13_Object-Oriented (Overview of Method Rewriting and Its Application) (Mastery)

  • A: What is method rewriting
    • Rewrite: The same method appears in the child parent class (note: the return value type can be the child parent class, which we have learned about object-oriented).
  • B: Application of method rewriting:
    • When the subclass needs the function of the parent class and the subclass of the functional subject has its own specific content, the method in the parent class can be overridden. In this way, it follows the function of the parent class and defines the specific content of the subclass.
  • C: Case demonstration
    • A: Define a mobile class.
class Demo7_Phone {
    public static void main(String[] args) {
        Ios8 i = new Ios8();
        i.siri();
        i.call();
    }
}

/*
B:Application of method rewriting:
    * When the subclass needs the function of the parent class and the subclass of the functional subject has its own specific content, the method in the parent class can be overridden. In this way, it follows the function of the parent class and defines the specific content of the subclass.
    ios7 System siri speak English
    ios8 System siri speaks Chinese
*/

class Ios7 {
    public void call() {
        System.out.println("Phone");
    }

    private void siri() {
        System.out.println("speak English");
    }
}

class Ios8 extends Ios7 {
    public void siri() {
        System.out.println("Speak Chinese");
        super.siri();
    }
}

Speak Chinese
Phone

08.14_Object-Oriented (Notes for Method Rewriting) (Mastery)

  • A: Notes for Method Rewriting

    • a: Private methods in parent classes cannot be overridden
      • Because parent private method subclasses simply cannot inherit
    • b: When subclasses override parent methods, access cannot be lower
      • It's better to agree.
    • c: The parent static method and the subclass must also be rewritten by the static method

      • In fact, this is not a method rewriting, but the phenomenon is true, as to why it is not a method rewriting, I will explain in polymorphism (static can only cover static)
    • When subclasses override parent methods, it's best to declare exactly the same.

  • B: Case demonstration
    • Method Rewriting Notes
class Demo8_Two oars {
    public static void main(String[] args) {
        DayOne d = new DayOne();
        d.Flirt with hot chicks();
        d.print();
    }
}
/*
    * a:Private methods in parent classes cannot be overridden
        * Because parent private method subclasses simply cannot inherit
    * b:When subclasses override parent methods, access cannot be lower
        * It's better to agree.
    * c:Parent static methods, subclasses must also be rewritten by static methods
        * In fact, this is not a method rewriting, but the phenomenon is true, as to why it is not a method rewriting, I will explain in polymorphism (static can only cover static)

    * When subclasses override parent methods, it's best to declare exactly the same.
*/
class Two oars {
    public void sing() {
        System.out.println("Sing revolutionary songs");
    }

     public void Flirt with hot chicks() {
        System.out.println("Singing a Red Song to Solve Mrs. Lin Xi's Chorus Bird");
    }

    public static void print() {
        System.out.println("Fu print");
    }
}

class DayOne extends Two oars {
     public int Flirt with hot chicks(int x) {
        System.out.println("Forced Entry");
        return 0;
    }



    public static void print() {                //Static state can only cover static state, but it is not rewritten. It is explained in detail when polymorphism occurs.
        System.out.println("Zi print");
    }
}

08.15 Object-Oriented (Method Rewritten Interview Questions) (Mastery)

  • A: Rewritten interview questions

    • The difference between Override and Overload? Can Overload change the return type?
    • overload can change the return value type by looking only at the parameter list
    • Method rewriting: The same method as the method declaration in the parent class appears in the subclass. Return values are identical (or child-parent) with respect to the type of return value

    • Method overloading: The method names in this class are the same and the parameter lists are different. It has nothing to do with the return value type.

    • When a subclass object calls a method:

      • Find the child class itself, then the parent class.

08.16 Object-Oriented (Using Pre-Inheritance Student and Teacher Cases) (Mastery)

  • A: Case demonstration
    • Use of pre-inheritance student and teacher cases
    • Attribute: Name, Age
    • Behavior: Eating
    • Teachers have their own way: lecturing.
    • Students have their own way of learning.

08.17 Object-oriented (using inherited student and teacher cases) (mastery)

  • A: Case demonstration
    • Use of inherited student and teacher cases

08.18_Object-Oriented (Cat and Dog Case Analysis, Implementation and Testing) (Mastery)

  • A: Case Study of Cats and Dogs
  • B: Case demonstration
    • Cat and Dog Case Succession Edition
    • Attributes: Hair color, number of legs
    • Behavior: Eating
    • Cat-specific Behavior: Catch Mouse
    • Dog-specific Behavior: Watchdog Look Home

08.19_Object-Oriented (final keyword modifies classes, methods, and characteristics of variables) (Master)

  • A:final overview
  • B:final Modification Characteristics
    • Modify classes, classes cannot be inherited
    • Modifying variables, variables become constants and can only be assigned once
    • Modification method, method can not be rewritten
  • C: Case demonstration
    • final modification characteristics

08.20_Object-Oriented (final keyword modifies local variables) (master)

  • A: Case demonstration

    • Demonstrate it internally or in the method declaration.

    • The basic type is that the value cannot be changed

    • The reference type is that the address value cannot be changed and the attributes in the object can be changed.

08.21_Object-Oriented (final modification variable initialization timing) (mastery)

  • The Initialization Timing of A:final Modified Variables
    • Display Initialization
    • Before the object is constructed

Posted by drewklue on Fri, 22 Mar 2019 03:36:53 -0700