JAVA study notes 02-object-oriented

Object-oriented

1. Class declaration
public class class name {}

public class Student {
    
    
    String name;
    int age;
    public void study(){
    
    
        System.out.println("学习");
    }
}

2. The creation and use of objects
Create:
class name object name = new class name ();
use:
object name. attribute;
object name. method (parameter);

public class demo01Student {
    
    
    public static void main(String[] args) {
    
    
        Student stu=new Student();
        stu.study();
    }
}

Object as parameter:

    public static void function(Student s){
    
    
        s.study();
    }

Object as return value:

    public static void main(String[] args) {
    
    
        Student stu=getStudent("Ray",24);
        System.out.println(stu.name+" "+stu.age);
    }
    public static Student getStudent(String name,int age){
    
    
        Student s = new Student();
        s.name=name;
        s.age=age;
        return s;
    }

3. Member variables and local
counterparts. 1. The definition location is different.
Local variables: inside the method.
Member variables: outside the method, directly written in the class.
2. The scope of action is different.
Local variables: can only be used in the method. Can no longer be used except methods.
Member variables: the entire class can be used in general
3. The default values ​​are different.
Local variables: there is no default value. If you want to use it, you must manually assign a value.
Member variables: If there is no assignment, there will be a default value. The rules are the same as arrays
4. The memory location is not the same.
Local variables: located in the stack memory.
Member variables: located in the heap memory.
5. The life cycle is different.
Local variables: born as methods are pushed onto the stack, and disappear as methods are popped from the stack.
Member variables: born as objects are created, Disappear as objects are garbage collected

4.
Private keyword
Use the private keyword to modify the member variables that need to be protected. Once the private keyword is used to modify, then the class can still be accessed at will, but it cannot be accessed directly outside the scope of this class

public class Person {
    
    
    String name;
    private int age;
    public void show(){
    
    
        System.out.println(name+" "+age);
    }
}

Use get and set:

public class Person {
    
    
    String name;
    private int age;
    public void show(){
    
    
        System.out.println(name+" "+age);
    }

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

    public int getAge() {
    
    
        return age;
    }
}

5.this keyword
this is used to distinguish between the same name

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

6. Construction method The method
used to create the object
Syntax:
public class name (parameter type parameter name) { method body } Note: 1. The name of the construction method must be exactly the same as the name of the class 2. The construction method should not write the return value Type 3. The construction method cannot return a specific return value 4. If no construction method is written, the compiler defaults a construction method 5. Once a construction method is written, the compiler will no longer provide a construction method 6. The construction method can be overloaded








public class Person {
    
    
    private String name;
    private int age;
    public Student(){
    
    
        System.out.println("无参构造方法调用");
    }
    public Person(String name,int age){
    
    
        System.out.println("有参构造方法调用");
        this.name=name;
        this.age=age;
    }
    public void setAge(int age) {
    
    
        this.age = age;
    }
    public int getAge() {
    
    
        return age;
    }
    public void setName(String name) {
    
    
        this.name = name;
    }
    public String getName(){
    
    
        return name;
    }
}

Standard class:
1. All member variables are decorated with the private keyword
2. Write a bunch of get/set methods for each member variable
3. Write a parameterless construction method
4. Write a full parameter construction method

Guess you like

Origin blog.csdn.net/qq_44708714/article/details/106763367