Object oriented (understand!)

Posted by kiwiwilliam on Sun, 26 Dec 2021 02:40:27 +0100

ps: Learning Meet the crazy God My study notes

Process oriented & object oriented

Process oriented thought

  1. The steps are clear and simple, what to do in the first step, what to do in the second step
  2. Facing the process, it is suitable to deal with some relatively simple problems

Object oriented thought

  1. Birds of a feather flock together, the thinking mode of classification. Thinking about problems will first solve those classifications needed, and then think about these classifications separately. Finally, process oriented thinking is carried out on the details under a certain classification.

(for example, building a house: first you need @ 1 chief designer, then subdivide it into @ 2 foundation, painting... And then divide it into @ 3... @ 4...) all parts are objects, and each @ is the process

  1. Object oriented is suitable for dealing with complex problems and problems requiring multi person cooperation!

For the complex description, in order to grasp it from the macro and analyze it reasonably as a whole, we need to analyze the whole system with the idea of facing the object. However, it still needs process oriented thinking to deal with the crowd operation.

What is object oriented

  1. Object oriented programming (OOP)
  2. The essence of object-oriented programming is to organize code by class and data by object
  3. abstract
  4. Three characteristics

encapsulation

/*
    1.Improve program security and protect data
    2.Actual details of hidden code
    3. Unified interface
    4.System maintainability increased

* */
//main method 
public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();

        s1.setName("Secretary Luo");
        System.out.println(s1.getName());

        s1.setAge(87);
        System.out.println(s1.getAge());

    }
}

//Self defined method
//Private: private
public class Student {

    //Property private
    private String name;//full name
    private int id;//Student number
    private char sex;//Gender
    private int age;

    //Provide some methods that can manipulate this property
    //Provide some public get and set methods

    //get gets this data
    public String getName(){
        return this.name;
    }

    //set sets a value for this data
    //alt+insert to quickly create set and get
    public void setName(String name){
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age>0&&age<120) {
            this.age = age;
        }else {
            System.out.println("Illegal input!!");
        }
    }
}

inherit

package com.OOP.demo05;
//Parent class
//In Java, all classes inherit object directly or indirectly
public class Person {
    //public
    //protected
    //default
    //private
    public int money =10_0000_0000;

    public void say(){
        System.out.println("Said a word");
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}

//Main method
package com.OOP.demo01;

import com.OOP.demo05.Student;

public class Application {
    public static void main(String[] args) {
        Student student = new Student();

        student.say();
        System.out.println(student.money);


    }
}

  • java has only single inheritance, not multiple inheritance (a son has only one biological Baba, and Baba can have multiple sons)
  • private things cannot be inherited
  • ctrl+H can be opened

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-jvyqu7m-1628500190588) (C: \ users \ Xiao Luo \ Desktop\Typore \ special image Typora\QQ screenshot 20210305214946.png)]

super

//Parent class
package com.OOP.demo05;
//In Java, all classes inherit object directly or indirectly
public class Person {
    protected String name = "Secretary Luo";

    public Person() {
        System.out.println("Person Parameterless output");
    }

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

//Subclass
package com.OOP.demo05;

public class Student extends Person{
    private String name = "qingjiang";

    public Student() {
        //Hidden code, calling the parameterless construction of the parent class
        super();//Calling the constructor of the parent class must be on the first line of the subclass constructor
        System.out.println("Student Parameterless output");
    }

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

    public void test(String name){
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);
    }
    public void test1(){
        print();
        this.print();
        super.print();
    }
}

super Note:
    1.super The constructor of the parent class must be called in the first instance of the constructor
    2. super Must only appear in a subclass's method or constructor
    3.super and this Constructor cannot be called at the same time!
Vs this:
    The objects represented are different:
        this : The object itself is the caller
        super : Represents the application of the parent object
    premise
        this : Can be used without inheritance
        super: Can only be used in inheritance conditions
    Construction method
        this(): Construction of this class
        super(): Construction of parent class

Method rewrite

package com.OOP.demo01;

import com.OOP.demo05.A;
import com.OOP.demo05.B;

public class Application {

    /*Static methods are very different from non static methods
    Static method: the method call is only related to the one defined on the left
    Non static definition: child class overrides parent class
    Rewriting can sometimes be understood as overriding
     */


    public static void main(String[] args) {
        B b = new B();
        b.test();

        //A reference to a parent class points to a child class
        A a = new B();
        a.test();
    }
}

package com.OOP.demo05;
//Subclass
public class B extends A {

    @Override//Functional notes!
    public void test() {
        super.test();
    }
}

package com.OOP.demo05;
//Parent class
//Rewriting is the rewriting of methods and has nothing to do with properties
public class A {
    public  void test(){
        System.out.println("A-->test()");
    }
}

Override: inheritance relationship is required. The child class inherits the parent class!

  1. Method names must be the same
  2. The parameter list must be the same
  3. Modifier: the scope can be expanded but not reduced: public > protected > Default > private
  4. Throw exception: the range can be narrowed but not enlarged. Exception (large) - > classnotfoundexception (small)

Override: the subclass method and the parent method must be the same, and the method body is different!

Why rewrite:

  • The function of the parent class and the child class are not necessarily required or satisfied
  • Shortcut key: alt+insert: override (override)

polymorphic

package com.OOP.demo01;

import com.OOP.Demo06.Person;
import com.OOP.Demo06.Student;

public class Application {
    public static void main(String[] args) {

        //The actual type of an object is determined
        //new Student();
        //new Person();

        //Object can execute those methods, mainly depending on the type on the left, which has little to do with the type on the right
        //Can point to a reference type; Uncertain: the parent class reference points to the child class

        //The methods that students can call are their own or inherit the parent class
        Student s1 = new Student();
        //Person can point to subclasses, but cannot call methods unique to subclasses
        Person s2 = new Student();
        Object s3 = new Student();

        s1.run();
        s2.run();

        s1.say();
        ((Student)s2).say();

    }
}

//Parent class
package com.OOP.Demo06;

public class Person {

    public void run(){

    }
}


//Subclass
package com.OOP.Demo06;

public class Student extends Person {
    @Override
    public void run() {
        System.out.println("son");
    }

    public void say(){
        System.out.println("hehe");
    }
}

Polymorphic considerations

  1. Polymorphism is the polymorphism of methods, and there is no polymorphism of attributes
  2. Parent and child classes are related, type conversion exception! ClassCastException
  3. Existence conditions: inheritance relationship, method override, parent class reference pointing to child class object! Father a = new son();
  4. Cannot override:
    1. static method belongs to class, not instance
    2. final constant
    3. Private private

instanceof

  1. instanceof is a binary operator in Java, similar to = =, >, < and other operators
  2. Its function is to test whether the object on its left is a specific instance of the class on its right, and return the boolean data type
  1. From the perspective of epistemology, there are objects before classes. Objects are concrete things. Classes are abstract, which are abstractions of objects (people – > teachers)
  2. From the perspective of code running, there are classes before objects. Class is the template of the object (RMB template – > a large number of RMB)

Pri vate private

instanceof

  1. instanceof is a binary operator in Java, similar to = =, >, < and other operators
  2. Its function is to test whether the object on its left is a specific instance of the class on its right, and return the boolean data type
  1. From the perspective of epistemology, there are objects before classes. Objects are concrete things. Classes are abstract, which are abstractions of objects (people – > teachers)
  2. From the perspective of code running, there are classes before objects. Class is the template of the object (RMB template – > a large number of RMB)

Topics: Java OOP Polymorphism