Knowledge of java classes and objects 1

1. Java classes

Classes are the basic elements that make up a Java program, and a Java application program is composed of several classes. Classes are the most important "data types" in the Java language, and variables declared by classes are called objects.

A class definition consists of two parts: the class declaration and the class body. The basic format is as follows:

class 类名 {
    
    
    类体的内容
}

class is a keyword used to define a class. "class class name" is the declaration part of the class, and the class name must be a valid Java identifier. The two curly braces {} and the content between them are the class body.

For example:

class Factory {
    
    
    float a[];
    Workman zhang;
}

"class Factory" is called a class declaration, "Factory" is the class name, and "{float a[];Workman zhang;}" is the class body.
The purpose of a class is to abstract the common attributes and behaviors of a class of things, and use a certain grammatical format to describe the abstracted attributes and behaviors. The key to abstraction is to capture two aspects of things: attributes and behavior. Therefore, the content of the class body consists of two parts:

1) Variable declaration: used to reflect the properties of the object.

2) Method definition: methods can operate on variables declared in the class, reflecting the behavior of the object.

Two, Java member variables

The content of the class body is divided into variable declaration and method definition, and the variables declared in the variable declaration part are called member variables or domain variables.

The type of member variable can be any data type in Java, including basic types: integer, floating point, character, logical type; reference type: array, object and interface.

Member variables are valid throughout the class, and their validity has nothing to do with their writing positions in the class body. However, it is not recommended to write member variable declarations scattered between methods. People are accustomed to introducing attributes first and then introducing behaviors.

For example:

class Factory {
    
    
    float a[];
    Workman zhang;
}

The member variable a of the Factory class is an array of float type , and zhang is a variable declared by the Workman class, that is, an object.

3. Java method

The content of the class body is divided into variable declaration and method definition. The method definition includes two parts: method header and method body. The basic format is as follows:

方法头{
    
    
    方法体的内容
}

A method header consists of the method's type, name, and a pair of parentheses after the name with a list of parameters within it. There is no parameter list in the method header of the parameterless method definition, that is, there is nothing in the pair of parentheses after the method name.

For example:

int speak() //无参数的方法头
{
    
    
return 123;
}
int add(int x,int y,int z) //有参数的方法头
{
    
    
    return x+y+z;
}

According to the needs of the program, the data type returned by the method can be any data type in Java. When a method does not need to return data, the return type must be void . The parameters of the method are given in many method declarations, and the parameters are some variable declarations separated by commas. Method parameters can be any Java data type.

A pair of curly braces {,} after the method declaration and the contents between them are called the method body of the method. The content of the method body includes the declaration of local variables and Java statements, that is, member variables and local variables declared in the method body can be operated in the method body . Variables and method parameters declared in the method body are called local variables.

Fourth, the construction method of Java class

A constructor is a special method in a class that is used when a program creates an object with a class . The name of the constructor method in the class must be exactly the same as the name of the class it is in, and it has no type. It is allowed to write several constructors in a class, but it must be ensured that their parameters are different. Different parameters mean: the number of parameters is different, or the number of parameters is the same, but the type of a corresponding parameter in the parameter list is different.

Notice:

If there is no construction method written in the class, the system will default that the class has only one construction method, and the default construction method has no parameters, and there is no statement in the method body.

If one or more constructors are defined in the class, then Java does not provide a default constructor. For example: The following Point class has two constructors.

class Point {
    
    
    int x,y;
    Point () {
    
    
        x = 1;
        y = 1;
    }
    Point (int a,int b) {
    
    
        x = a;
        y = b;
    }
}

It is important to note that constructors have no type.

For example:

class Point {
    
    
    int x,y;
    Point () {
    
     //是构造方法
        x = 1;
        y = 1;
    }
    void Point (int a,int b) {
    
     //不是构造方法,该方法的类型是void
        x = a;
        y = b;
    }
    int Point () {
    
     //不是构造方法,该方法的类型是int
        return 12;
    }
}

5. Create objects in Java

A class is the most important data type in an object-oriented language, and a class can be used to declare variables. In object-oriented languages, variables declared with classes are called objects. Different from basic data types, after declaring an object with a class, you must also create the object, that is, assign the owned variables to the declared object, and determine the attributes of the object. When using a class to create an object, it is also called giving an instance of this class. In layman's terms, a class is a template for creating objects, and there is no object without a class. Creating an object includes two steps: the declaration of the object and the assignment of variables to the object.

1. The general format of object declaration is:

类的名字 对象名字;

For example:

Lader lader;

2. Assign the variable to the declared object:

Use the new operator and the constructor of the class to assign variables to the declared object, that is, to create the object . If there is no constructor in the class, the system will call the default constructor, which has no parameters and has no statement in the method body.

For example:

class Point {
    
    
    int x,y;
    Point (int a,int b) {
    
    
        x = a;
        y = b;
    }
}
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Point p1,p2; //声明对象p1和p2
        p1 = new Point (10,10); //为对象分配变量(使用new和类中的构造方法)
        p2 = new Point (23,45); //为对象分配变量(使用new和类中的构造方法)
    }
}

Six, Java uses objects

The purpose of abstraction is to produce classes, and the purpose of classes is to create objects with properties and behavior. Objects can not only manipulate their own variables to change the state, but also call methods in the class to generate certain behaviors.

By using the operator "." (the dot operator is also called a reference operator or an access operator), an object can access its own variables and call methods.

After the object is created, it has its own variables, which are the entities of the object. An object accesses its own variables by using the dot operator ".", and the access format is:

对象.变量;

After the object is created, you can use the dot operator "." to call the method in the class that created it, so as to produce certain behaviors. The calling format is:

对象.方法;

When an object calls a method, the member variables that appear in the method refer to the variables assigned to the object.

class XiyoujiRenwu {
    
    
    float height,weight;
    String head;
    void speak (String s) {
    
    
        head = "歪头";
        System.out.println(s);
    }
}
public class Main {
    
    
    public static void main(String[] args) {
    
    
        XiyoujiRenwu zhubajie,sunwukong; //声明对象
        zhubajie = new XiyoujiRenwu(); //为对象分配变量
        sunwukong = new XiyoujiRenwu();
        zhubajie.height = 1.80f; //对象给自己的变量赋值
        zhubajie.head = "大头";
        sunwukong.height = 1.60f; //对象给自己的变量赋值
        sunwukong.weight = 1000f;
        sunwukong.head = "长发飘飘";
        System.out.println("zhubajie的身高:"+zhubajie.height);
        System.out.println("zhubajie的头:"+zhubajie.head);
        System.out.println("sunwukong的重量:"+sunwukong.weight);
        System.out.println("sunwukong的头:"+sunwukong.head);
        zhubajie.speak("俺老猪想娶媳妇"); //对象调用方法
        System.out.print1n("zhubajie现在的头:"+zhubajie.head);
        sunwukong.speak("俺老孙想骗八戒背我"); //对象调用方法
        System.out.println("sunwukong现在的头:"+sunwukong.head);
    }
}

insert image description here

7. References and entities of Java objects

The variable declared by the class is called the object, and the object is the variable responsible for storing the reference to ensure that the object can operate the variable assigned to the object and call the method in the class, and the variable assigned to the object is called the entity of the object .

Point point = new Point()

Point: class name

point: object name

new Point(): the entity of the object

An object reference is the memory address where the object is stored.

注意:

在编写程序时要避免使用空对象。

我们把没有实体的对象称作空对象,空对象不能使用,即不能让一个空对象去调用方法产生行为。假如程序中使用了空对象,程序在运行时会出现异常NullPointerException。由于对象可以动态地被分配实体,所以Java编译器对空对象不做检查。

in conclusion:

If two objects declared by a class have the same reference, they both have exactly the same variable or entity. When the program uses a class to create two objects object1 and object2, the references of the two are different.

In Java, for two objects object1 and object2 of the same class, the following assignment operations are allowed:

object1 = object2;

In this way, what is stored in object1 will be the value of object2, that is, the reference of object2. Therefore, the variable owned by object1, that is, the entity, is exactly the same as object2.

Eight, the basic structure of Java classes and programs

A Java application program consists of several classes, which can be in one source file or distributed in several source files.

A Java application has a main class, that is, a class containing a main method, and the Java application starts executing from the main method of the main class.

When writing a Java application, you can write several Java source files, save the Java source files involved in the application in the same directory, compile and pass them separately, get the bytecode files required by the Java application, and then run main class.

A Java program can store the classes it uses in different source files, or it can store the classes it uses in one source file, and the classes in one source file can be used by multiple Java programs.

From the perspective of compilation, when a program needs to modify a certain class, it only needs to recompile the source file of this class, and it is not necessary to recompile the source files of other classes, which is very beneficial to system maintenance.

Notice:

Although there can be multiple classes in a Java source file, it is still recommended to write only one class in a Java source file.

Nine, Java parameter value passing

One of the most important parts of the method is the parameter of the method. The parameter belongs to the local variable. When the object calls the method, the parameter is allocated memory space, and the caller is required to pass the value to the parameter, that is, when the method is called, the parameter variable must have a specific value.

In Java, the value of the parameter variable in the method is a copy of the value specified by the caller. If the method changes the value of the parameter, it will not affect the value of the variable "passing value" to the parameter.

Passing values ​​of basic data type parameters:

For parameters of primitive data types, the value passed to the parameter cannot be at a higher level than the parameter.

For example: You cannot pass a float value to an int parameter, but you can pass a float value to a double parameter.

Passing by value for reference type parameters:

Java's reference data includes arrays, objects, and interfaces. When the parameter is a reference type, "pass by value" transfers the "reference" stored in the variable, not the entity referenced by the variable.

Notice:

For two reference variables of the same type, if they have the same reference, they will have the same entity. Therefore, if the entity referenced by the parameter variable is changed, the entity of the original variable will change in the same way; however, changing the parameter The "reference" stored in will not affect the "reference" stored in the variable to which the value is passed.

learn from this link

Summarize

Don't be afraid of losing, what you lose doesn't belong to you, and don't be afraid of being hurt, what can hurt you is your doom, the prosperity is three thousand, the indifferent is just a cloud, the troubles are countless, and the sunny day is when you think about it.

Guess you like

Origin blog.csdn.net/weixin_51884452/article/details/130577384