Day7: object oriented programming 1

Object oriented programming oop

For complex things, we need the idea of object-oriented, that is, constantly refine the classification, and then deal with it with the idea of process oriented at the last level

Call of supplementary method

Plus static is static method, and vice versa is non - static method

Cross package call: static methods can be used across packages in the form of class name Method name such as

student.score();

Non static method calls require instantiation in the form of

Student student=new Student();
student.score();

A: call two methods in the same package

If both methods are static or non static, you can call B in a and a in B

Differences between static and non static method calls

Not all static methods need to be instantiated. Static methods can be called directly, regardless of whether they cross packages or not

Reference passing and value passing

Reference passing

public class Dome05 {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);
        change(person);
        System.out.println(person.name);
    }
    public static String change(Person person){
        person.name="lll";
        return person.name;
    }
}
class Person{
    String name;
}

pass by value

public class Demo06 {
    public static void main(String[] args) {
        int a=10;
        System.out.println(a);
        Demo06.change(a);//Return empty, a or 10
        System.out.println(a);
    }
    public static void change(int a){
        int b=20;
        a=b;
    }
}

Creation of classes and objects

Class is an abstract data type, and object is a concrete instance of abstract concept

The essence of object-oriented programming is to organize code in the way of class, and the organization of an object encapsulates data

Note: 1 For a project specification, there is only one main method, and the call needs to be instantiated

2. Different classes will generate corresponding objects in the stack. The object name is the name of the reference variable, which is docked and assigned with the template in the heap

3.this. The attribute is in the method and can be assigned with the passed in parameter

constructor

Methods are instantiated, and the reason why classes can be instantiated is that there is a constructor. Even if there is nothing, there will be an invisible method

Features: 1 Must be the same as the name of the class

2. There is no return type and void cannot be written

3. The essence of new is to call the constructor

4. After the parameter structure is defined, the definition must be displayed if there is no parameter, and the shortcut key is ALT+insert

encapsulation

High aggregation: the internal data operation of the class is completed by itself, and external operation is not allowed. It is encapsulated with the private modifier

Low coupling: only a small number of methods are exposed for external use, ge or set

characteristic:

  1. Improve program security and protect data
  2. Implementation details of hidden code
  3. Unified interface
  4. Increased maintainability of the system

demonstration

student class

public class Student {
    private int age;//encapsulation
    //Receive and return data
    public int getAge(){
        return age;
    }
    //Set data
    public void setAge(int age){
        this.age=age;
    }
}

main method

Student s1 = new Student();//Create new object s1
        s1.setAge(20);//Call the method to set the age to 20
        System.out.println(s1.getAge());

inherit

characteristic:

  1. Inheritance is the relationship between classes. In addition, there are aggregation, composition and dependency

  2. There is a parent class between inheritance and a child class inherits the parent class. The two have a dependency relationship, which is represented by extensions

    The format is modifier + type + subclass name + extends + parent class name

  3. The subclass inherits all the methods of the parent class. CTRL+H can see the inheritance relationship

  4. Public has the highest priority among modifiers, and public is generally used for inheritance The attributes are generally private. The encapsulated attributes are then operated by get and set

  5. All classes inherit object directly or indirectly by default, which is similar to the parameterless construction of constructors

  6. Java has only single inheritance, not multiple inheritance, that is, a subclass has only one parent class, while the parent class has multiple subclasses

demonstration

person class, parent class

public class Person {
    public void say(){
        System.out.println("He said hello,I'm yours Java teacher");
    }
}

Teacher class, subclass

public class Teacher extends Person{
    private String course;//encapsulation
    public String getCourse(){
        return course;
    }
    public void setCourse(String course){
        this.course=course;
    }
}

main method. Since the subclass inherits the say() method of the parent class, the teacher object can also be used

public static void main(String[] args) {
        Teacher teacher = new Teacher();
        teacher.setCourse("java");
        System.out.println("The subjects taught are:"+teacher.getCourse());
        teacher.say();
    }

Added by jonker on Tue, 08 Mar 2022 21:58:29 +0200