Java classes and objects

Posted by PierceCoding on Fri, 25 Feb 2022 09:14:52 +0100

1, Object oriented

face Object is an idea to solve problems, which mainly depends on the interaction between objects to complete a thing .
Process oriented: when a function needs to be implemented, each specific step should be handled in detail
Object oriented: when I need to implement a function, I don't care about the specific steps, but find someone who already has the function to help me.

2, Class definition

Class is used to ( object ) To describe , mainly describing the entity ( object ) What attributes does it have ( Appearance size, etc ) , what functions ( For what ), after the description is completed, the computer can recognize it.

Define a class to simulate "student" things, which has two components.


Attribute (what is):
Name
Age
Behavior (what can be done):
Eat
Sleep
Study

Corresponding to java classes

Member variables (properties):
     String name;// full name
     int age;// Age
Member method (behavior):
public void eat() {} / / eat
public void sleep() {} / / sleep
public void study() {} / / learning

be careful:
1. Members are uniformly written as public

2. Member variables are directly defined in the class and outside the method

3. The class name shall be in the form of large hump

Define a student class

public class Student {
    public String name; 
    public short age;  
    
   public void eat(){
        System.out.println("having dinner!");
    }

    public void sleep(){
        System.out.println("sleep");
    }
    public void study(){
        System.out.println("study!");
    }

}
matters needing attention:
1. Generally, only one class is defined in a file
2. main The class in which the method is located is generally used public modification ( be careful: Eclipse By default public Modified class main method )
3. public The modified class must be the same as the file name
4. Don't modify it easily public The name of the modified class. If you want to modify it, you can modify it through the development tool

3, Class instantiation

Defining a class is equivalent to defining a new type in the computer. The process of creating objects with class types is called class instantiation.

Usually, a class cannot be used directly. You need to create an object according to the.

//1. Guide Package

//It is under the same package and can be omitted

//2. Create, format
Student stu = new Student();

//3. Use
//Object name Member variable name
System.out.println(stu.name);
System.out.println(stu.age);

//Change the value content of the member variable in the object
System.out.println("======================");
stu.name="Wang Aoao";
stu.age=18;
System.out.println(stu.name);
System.out.println(stu.age);
System.out.println("======================");

//4. Member method
// Use member method: object name Member method name (parameter)
stu.eat();
stu.sleep();
stu.study();
matters needing attention
1,new Keyword is used to create an instance of an object .
2. Use . To access properties and methods in an object .
3. You can create multiple instances of the same class .

4, Differences between member variables and local variables

1. The defined positions are different

Local variables: inside the method

Member variable: written directly in the class outside the method

2. Different scope of action

Local variable: it can only be used in the method. It cannot be used when the method is out

Member variable: the whole class can be used universally

3. The default value is different

Local variable: there is no default value. If you want to use it, you must assign it manually

Member variable: if there is no assignment, there will be a default value. The rule is the same as that of array

4. Different memory locations

Local variable: in stack memory

Member variable: in heap memory

5. Different life cycle

Local variable: it is born as the method goes into the stack and disappears as the method goes out of the stack

Member variable: it is born as the object is created and disappears as the object is garbage collected

6, Object oriented features

Three characteristics of object-oriented: encapsulation, inheritance and polymorphism.

Encapsulation in Java:
1. The method is a kind of encapsulation
2. The keyword private is also a kind of encapsulation

7, Private keyword

Problem Description: when defining the age of Person, unreasonable values cannot be prevented from being set
Solution: modify the member variables to be protected with the private keyword.

Once private is used for decoration, it can still be accessed freely in this class.
But! It cannot be accessed beyond the scope of this class.

Indirect access to private member variables defines a pair of Getter/Setter methods
Must be called setXXX or getXXX naming rules.
For Getter, there can be no parameters, and the return value type corresponds to the member variable;
For Setter, there can be no return value. The parameter type corresponds to the member variable.

String name;

private int age;

public void show(){
    System.out.println("My name is"+name+",this year"+age+"Years old.");
}

//This member method is specifically used to set data
public void setAge(int num){

 if(num<100 && num>0){
        age = num;
    }else{
        System.out.println("data error");
    }

}
//This member method is dedicated to obtaining the data of age
public int getAge(){
    return age;
}

!!! For Boolean values in basic types, the Getter method must be written in the form of isXXX, and

setXXX unchanged

private boolean male;//Is it a man
public void setMale(boolean b){
    male = b;
}
public boolean isMale() {
    return male;
}

8, this keyword

When the local variable of the method and the member variable of the class have the same name, the local variable shall be used preferentially according to the "proximity principle".
If you need to access member variables in this class, you need to use the format:
this. Member variable name

Whoever invokes the method is this

String name;
//The parameter is the name of the other party
//Name is your own name
public void sayHello(String name){
    System.out.println(name+",Hello! I am"+this.name);
}

9, Construction method

Construction method is a method specially used to create objects. When we create objects through shutdown word new, we are actually calling the construction method.


Format:
public class name (parameter type parameter name){
Method body
}

be careful:
1. The name of the constructor must be exactly the same as the name of the class, even the case;
2. The constructor should not write the return value type, not even void;
3. Constructor cannot return a specific return value;
4. If no tolerance and construction methods are written, the compiler will give a construction method by default, and do nothing without parameters and method body;
public Stuednt02(){}
5. Once at least one constructor is written, the compiler will no longer give away;
6. Construction methods can also be overloaded.
Overload: the method name is the same, but the parameter list is different.

public class Student02 {

    //Member variable
    private String name;
    private int age;

    //Construction method without parameters
    public Student02(){
        System.out.println("The parameterless construction method is executed!");
    }

    //Construction method with parameters
    public Student02(String name,int age){
        System.out.println("The full parameter construction method is implemented!");
        this.name = name;
        this.age = age;
    }

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

    public String getName(){
        return name;
    }

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

    public int getAge(){
        return age;
    }
}
Note: the constructor is used to initialize the members in the object and is not responsible for opening up space for the object.

10, Define a standard class

A standard class usually has the following four components:

1. All member variables should be decorated with the private keyword
2. Write a pair of children Getter/Setter methods for each member variable
3. Write a parameterless construction method
4. Write a construction method with parameters

Such standard classes are also called Java beans

Topics: Java