Getting to Know Java (Java Classes and Object-Classes)

one type

    A class is a carrier that encapsulates the properties and behaviors of an object. In the Java language, the properties of an object exist in the form of member variables, and the methods of an object exist in the form of member methods.    

    1.1 Member variables

    The properties of an object in Java are also called member variables.

    eg : Define a book class, the member variables correspond to the attributes of the class object, and set 3 member variables in the Book class, namely id, name and category, which correspond to the three book attributes of book number, book name and book category.

public class Book{
    private String name; //Define a member variable of String type
    public String getName(){ //Define a getName() method
        int id = 0; //local variable
        setName("Java"); //Call other methods in the class
        return id + this.name; //setting method return value
    }
    private void setName(String name){ //Define a setName() method
        this.name = name; //Assign parameters to member variables in the class
    }
    public Book getBook(){
        return this; //return the Book class reference
    }
}

    Classes are defined in Java using the class keyword, where Book is the name of the class. At the same time, three member variables are defined in the Book class. The type of the member variable can be set to the legal data type in Java. In fact, the member variable is an ordinary variable, and the initial value can be set for it or not. If no initial value is set, there will be a default value.

    1.2 Member methods

    The use of member methods in the Java language corresponds to the behavior of class objects. Taking the Book class as an example, it contains two methods, getName() and setName(), which are the methods for getting the book name and setting the book name respectively.

    Syntax: permission modifier return value type method name (parameter type parameter name) { ... // method body return return value; }

    A member method can have parameters, which can be objects or variables of basic data types. At the same time, member methods have the option of returning a value or not returning any value. If the method needs to return a value, you can use the return keyword in the method body, use After this keyword, the execution of the method will be terminated. No return value can be represented using the void keyword.

    The return value of the member method can be the result of the calculation or other desired values ​​and objects, and the return value type must be consistent with the value type returned by the method.

    Member methods can call other member methods and class member variables. At the same time, a variable can be defined in the member method, and this variable is a local variable.

    If a method contains a local variable with the same name as a member variable, access to this variable in the method is performed as a local variable. For example, the variable id has the value 0 in the getName() method, not the value of id in the member variable.

    Class member variables and member methods can also be collectively referred to as class members.

    1.3 Permission modifiers

    Permission modifiers in Java mainly include private , public and protected , which control access to classes and class member variables and member methods.

    If a member variable or member method of a class is modified as private, the member variable can only be used in this class, not visible in subclasses, and invisible to classes in other packages. If you set the access permissions of the member variables and member methods of a class to public, then in addition to using this data in this class, you can also use it in subclasses and classes in other packages. If a class's access permissions are set to private, the class will hide all data within it from users accessing it directly. If you need the data in the class to be used by subclasses or classes in other packages, you can set this class to public access. If a class uses the protected modifier, only subclasses of this class or other classes in this package can access member variables and member methods in this class.

    In this way, the classes modified by public and protected can be accessed by subclasses. If the subclass and the superclass are not in the same guarantee package, only the class modified by public can be accessed by subclasses. If the parent class does not allow access to its member variables through the subclass generated by integration, then this member variable of the parent class must be declared private.

Modifier permissions in the Java language
access package location private protected punlic
this class visible visible visible
Other classes or subclasses of the same package Invisible visible visible
Classes or subclasses of other packages Invisible Invisible visible

    When declaring a class, the public, protected and private modifiers are not used to set the permissions of the class, then the class is preset to the package access scope, that is, only a class in a package can call the member variables or member methods of this class.

    eg : Create the AnyClass class under the com.ljs package in the project, which uses the default access rights.

package com.ljs; //package
class AnyClass{ //Use default access permissions
    public void doString(){ //use the public modifier
        ....//Method body
    }
}

    Since the modifier of the class is the default modifier, that is, only other classes and subclasses in one package can access the class, and the doString() method in the AnyClass class is set to public access, even so, doString( ) method's access rights are still the same as the AnyClass class's access rights, because the Java language stipulates that the class's permission settings will constrain the class's members' permission settings.

    It can also be written like this:

package com.ljs; //package
class AnyClass{ //Use default access permissions
    void doString(){ //use the public modifier
        ....//Method body
    }
}

    1.4 Local variables

    If a variable is defined inside a member method, then the variable is called a member variable.

    For example, in the Book class, the id variable of the getName() method is a local variable. In fact, the formal parameter in the method can also be used as a local variable. For example, when the setName(String name) method is defined, the formal parameter of String name is regarded as a local variable.

    Local variables are created when the method is executed and destroyed when the method execution ends. Local variables must be assigned or initialized when used, otherwise a compilation error will occur.

public String getName(){ //Define a getName() method
        int id = 0 ; //If the initial value of this id is removed from the local variable, the compiler will error
        setName("Java"); //Call other methods in the class
        return id + this.name; //setting method return value
    }

    1.5 Valid Scope of Local Variables

    The effective scope of a local variable can be called the scope of the variable, and the effective scope of a local variable starts from the declaration of the variable to the end of the variable.

public void doString(String name){
    int id = 0; //The scope of the local variable id starts
    for(int i = 0 ; i < 10 ; i++){ //Start of scope of local variable i
        System.out.println(name + String.valueOf(i)); //The scope of the local variable i ends
    }
} //The scope of the local variable id ends

    Two local variables with the same name and type can be declared at the same time in scopes that are not nested with each other.

public void doString(String name){
    int id = 0; //The scope of the local variable id starts
    for(int i = 0 ; i < 10 ; i++){ //Start of scope of local variable i
        System.out.println(name + String.valueOf(i)); //The scope of the local variable i ends
    }
     for(int i = 0 ; i < 3 ; i++){ //Start of scope of local variable i
        System.out.println(i); //The scope of the local variable i ends
    }
} //The scope of the local variable id ends

    However, this declaration is not allowed in mutually nested areas. If the local variable id is defined again in the for loop of the method body, the compiler will report an error.

public void doString(String name){
    int id = 0; //The scope of the local variable id starts
    for(int i = 0 ; i < 10 ; i++){ //Start of scope of local variable i
        System.out.println(name + String.valueOf(i)); //The scope of the local variable i ends
    }
     for(int i = 0 ; i < 3 ; i++){ //Start of scope of local variable i
        System.out.println(i); //The scope of the local variable i ends
        int id = 7 ; //Repeat the definition of the local variable id, an error will be reported
    }
} //The scope of the local variable id ends

    Using a local variable outside the scope is a common mistake because there is no code that declares the local variable outside the scope.

    1.6 this keyword

    eg : Create a class file in which setName() is defined, and assign the parameter values ​​of the method to member variables in the class.

private void setName(String name){ //A setNamw() method is defined
    this,name = name; //Assign the parameter value to the member variable in the class
}

    The member variable has the same name as the formal parameter in the setName() method, both are name, so how to distinguish which variable is used in the class? In the Java language, the this keyword is used to represent the reference of the object of this class. The this keyword is implicitly used to refer to the member variables and methods of the object. This.name refers to the name member variable of the Book class, and this.name. The second name in the name = name statement refers to the formal parameter name. In essence, the function implemented by the setName() method is to assign the value of the formal parameter name to the member variable name.

    This can call member variables and member methods, but the most common way of calling in the Java language is to use "object.member variables" or "object.member methods" to call.

    this refers to an object of this class. When a local variable or method parameter overrides a member variable, add the this keyword to make it clear whether the class member or the local variable or method parameter is referenced.

    If you omit the this keyword and write it directly as name = name, it just assigns the parameter name to the parameter variable itself, and the value of the member variable name does not change, because the parameter name overrides the member variable name in the scope of the method.

    In addition to calling member variables or member methods, this can also be used as the return value of a method.

    eg : Create a class file, define the method of Book type in the class, and return it through the this keyword.

public Book getBook(){
    return this; //return the Book class reference
}

    In the getBook() method, the return value of the method is the Book class, so the method body uses the form of return this to return the Book class object.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325631834&siteId=291194637