day06 object oriented Basics

This learning content comes from station b: dark horse programmer

Note: I was speechless and failed to punch in. Normally, this should be the fourth day. After listening to the object-oriented part, I didn't understand it. I listened to it again. Now I feel a little on the road, but I know it's only the simplest, and there's more difficult to wait for me. There's a long way to go. Keep working hard! If you see this article can help you, giving me a praise or comment is also the biggest driving force for me to update! Thank you. If there is any mistake in the content, you are welcome to criticize and correct!!!

I What to master when facing the object

1. What is oriented programming

1.1 what are classes and objects

1.2 how to design class

Code implementation: car class


public class Car {
    //Member variable
    String name;
    double price;
    //method
    public void start(){
        System.out.println(name + "It's on");
    }
    public void run(){
        System.out.println("The price is:" + price + "of" + name +"Run fast");
    }
}

Code implementation: test class

//Learn to design objects and use them
public class Test1 {
    public static void main(String[] args) {
        //Create a car object
        Car c = new Car();
        c.name = "bmw";
        c.price = 38.9;
        System.out.println(c.name);
        System.out.println(c.price);
        c.start();
        c.run();

        System.out.println("=============");
        Car c1 = new Car();
        c1.name = "Benz";
        c1.price = 39.5;
        System.out.println(c1.name);
        System.out.println(c1.price);
        c1.start();
        c1.run();
    }
}

1.3 precautions for defining classes

Code implementation: Student class

public class Student {
    public String name;
    boolean flag;
}
class Animal{

}
class Dog{

}

Code implementation: test class

//Objective: to understand several supplementary precautions of definition class and prepare for subsequent knowledge learning
public class Test2 {
    public static void main(String[] args) {
        /*1,The first letter of the class is recommended to be capitalized to meet the hump mode, and keywords cannot be used,
             Must be a legal identifier and meaningful
          2,Multiple class classes can be defined in a Java file, but only one class is a public modifier,
             Moreover, the class name modified by public must become the code file name
             In actual development, it is recommended that only one class be defined in a code file
          3,The complete definition format of member variable is: modifier data type variable name = initialization value:
             Generally, there is no need to specify the initialization value, and there is a default value
             Rules for default values:
             byte short int long    O
             double flat            0.0
             boolean                false
             String Reference type null
         */
        Student s = new Student();
        System.out.println(s.name);
        System.out.println(s.flag);

    }
}

2. Object memory diagram

2.1 two object memory diagram

2.2 two variables refer to the same object

Code implementation: Student class

public class Student {
    String name;
    char sex;
    String hobby;
    public void study(){
        System.out.println("name:" + name + ",Gender:" + sex + ",Hobbies:" + hobby
        + "Start learning!");
    }
}

Code implementation: test class

//Objective: to understand the memory mechanism of two variables pointing to one object
public class Test {
    public static void main(String[] args) {
        //1. Create student object
        Student s1 = new Student();
        s1.name = "Xiao Ming";
        s1.sex = 'male';
        s1.hobby = "Sleep, play, study";
        s1.study();

        //Assign the s1 variable to a student type variable s2
        Student s2 =s1;
        System.out.println(s2);
        System.out.println(s1);

        s2.hobby ="Love to ask questions";
        System.out.println(s2.name);//Xiao Ming
        System.out.println(s2.sex);//male
        System.out.println(s1.hobby);//Love to ask questions
        s2.study();
    }
}

When the variable is assigned null, Java has an automatic garbage collector for regular cleaning

3. Learn to use constructors

3.1 function and classification format of constructor

Code implementation: car class

public class Car {
    String name;
    double price;

    //Parameterless constructor
    public Car(){
        System.out.println("==The parameterless constructor was called=");
    }
    //Parametric constructor
    public Car(String n,double p){
        System.out.println("==A constructor with parameters was called=");
        name = n;
        price = p;
    }
}

Code implementation: test class

//Objective: to understand constructors and understand the role of two types of constructors
public class Test {
    public static void main(String[] args) {
        //Get the object by calling the constructor
        Car c = new Car();
        c.name = "bmw";
        c.price = 38.9;
        System.out.println(c.name);
        System.out.println(c.price);

        //Calling a parameterized constructor
        Car c2 = new Car("Benz",50);
        System.out.println(c2.name);
        System.out.println(c2.price);
    }
}

3.2 precautions for constructor

 
3.3 constructor summary

 

4.this keyword

4.1 what is this keyword

 

Code implementation: car class

public class Car {
    String name;
    double price;
    //Parametric constructor
    public Car() {
        System.out.println("In a parameterless constructor this:" + this);
    }
    public Car(String name, double price) {
        this.name = name;
        this.price = price;
    }
    public void gowith(String name) {
        System.out.println(this.name + "And" + name + "match");
    }
    public void run() {
        System.out.println("In method this:" + this);
    }
}

Code implementation: test class

//Objective: to understand the function of this keyword
public class Test {
    public static void main(String[] args) {
        //Create a car object
        Car c = new Car();
        c.run();

        System.out.println(c);

        System.out.println("==================");
        Car c2 = new Car("Benz",50);
        System.out.println(c2.name);
        System.out.println(c2.price);

        c2.gowith("bmw");

    }
}

4.2 function of this keyword

 4.3 summary

 

5. Packaging

5.1 what is encapsulation

5.2 how to make packaging better

Code implementation: Student class

public class Student {
    //1. Member variables are decorated with private and can only be accessed in this class
    private int age;
    //2. Provide a complete set of getter and setter methods to expose their value and assignment
    public void setAge(int age){
        if(age >= 0 && age <=200 ){
            this.age = age;
        }else {
            System.out.println("There is a problem with your age data:");
        }
    }
    public int getAge(){
        return age;//You can directly return age in the same class
    }

}

Code try: test class

//Objective: learn how to package better.
public class Test2 {
    public static void main(String[] args) {
        Student s = new Student();
        s.setAge(23);//assignment
        System.out.println(s.getAge());//Value
    }
}

6. Standard JavaBean s

6.1 what is a JavaBean

 

Code implementation: User class

public class User {
    //1. Use private for member variables
    private String name;
    private double height;
    private double salary;

    //3. A parameterless constructor is required, and a parameterless constructor is optional

    public User() {
    }
    public User(String name, double height, double salary) {
        this.name = name;
        this.height = height;
        this.salary = salary;
    }
    
    //2. A complete set of setter and getter methods must be provided for member variables

    public String getName() {
        return name;
    }

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

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

Code implementation: test class

//Objective: remember the writing requirements of JavaBean s
public class Test {
    public static void main(String[] args) {
        //1. The parameterless constructor creates an object that encapsulates a user's information
        User u1 = new User();
        u1.setName("Hangzhou Cai Xukun");
        u1.setHeight(183);
        u1.setSalary(19999);
        System.out.println(u1.getName());
        System.out.println(u1.getHeight());
        System.out.println(u1.getSalary());

        //3. A parameter constructor creates an object to encapsulate a user information
        User u2 = new User("Hangzhou delireba",170,19999);
        System.out.println(u2.getName());
        System.out.println(u2.getHeight());
        System.out.println(u2.getSalary());
    }
}

7. Differences between member variables and local variables

II Summary

 

 

 

 

Tags: Java programming language

Posted by AlanG on Mon, 23 May 2022 21:53:09 +0300