Methods in java classes and objects

Java language is an object-oriented programming language, and object-oriented is a programming idea. Under the guidance of object-oriented thinking, use java language to design and develop computer programs. The objects in this refer to programs (in reality) All things, each thing has its own attributes (features) and behaviors. Object-oriented thinking is to abstract (extract) the attributes and behaviors of things by referring to things in reality in the process of computer programming. Come out and describe the design idea as a computer event.

-----------

The meaning of the object refers to a specific thing, that is, something that can be seen and touched in real life. In object-oriented programming, an object refers to a certain component in a computer system. In object-oriented programming, objects have two meanings, one of which is data and the other is action. The object is a combination of data and action. The object can not only perform operations, but also record the results of operations in time.
Class: is a collection of related attributes and behaviors . We can regard a class as a template for a class of things, and use the attribute characteristics and behavior characteristics of things to describe this kind of things.
Methods refer to the operations that objects can perform. Methods also have another name, called functions. The method is the definition function in the class, and its specific function is to describe the operation of the object.
Attribute: State
behavior of things: functions (actions) of things


Use of objects

After the method is declared, it will not be executed automatically. The main method needs to be called and executed, because
the grammatical format of the main method method call at the entrance of the program : If the method declaration format has a static modifier, then the method name ()
is called ; if the method declaration format is called If there is no parameter setting, then the corresponding parameter value needs to be passed in when calling. The name of the called method (corresponding parameter value);

       对象的使用**格式**
         创建对象:
                    类名 对象名 = new 类名();
                             当对象创建完毕,那么类中定义的成员变量和成员方法自动含有。
                            使用对象访问类中的成员:
                            对象名.成员变量;
                            对象名.成员方法();

Method definition and use

The " Method " in the Java language may also be called " Function " in other languages . For some complex code logic, if you want to reuse these codes and "use them at any time", you can put these codes in a curly brace "{}" and give them a name. When using the code, just find the name and call it directly.
First, the method contains a method header and a method body. Here are all the parts of a method:
Modifier: Modifier, which is optional, tells the compiler how to call the method. Defines the access type of the method. For example,
public protected permission modifier static static (class) modifier final and final (unchanged) modifier

Modifier Modifier name effect
public Public access modifier The classes, methods, constructors, and interfaces declared as public can be accessed by any other class.
private Private access modifier Variables declared as private access types can only be accessed by external classes through public getter methods in the class.
protected Protected access modifier Variables, methods, and constructors declared as protected can be accessed by any other class in the same package, and can also be accessed by subclasses in different packages.
static Static modifier The Static keyword is used to declare static methods independent of objects.
Final Final modifier Final variables can be initialized explicitly and can only be initialized once.

Return value type: The method may return a value. returnValueType is the data type of the return value of the method. Some methods perform the required operation, but do not return a value. In this case, use the keyword void .
Method name: is the actual name of the method. The method name and the parameter list together constitute the method signature.
Parameter type: The parameter is like a placeholder. When the method is called, pass the value to the parameter. This value is called an actual parameter or variable. The parameter list refers to the parameter type, order, and number of parameters of the method. Parameters are optional, and the method does not need to contain any parameters.
Method body: The method body contains specific sentences that define the function of the method.
First understand the two concepts about the method:
1. Parameters: refers to the data entered into the method, with these data, the method can execute logic.
2. Return value: refers to the data from the method, that is, the final result data after the method is executed.
The basic format of the current definition method:


修饰符   返回值类型  方法名称(参数类型   参数名称){
方法体
return    返回值;
}

Explanation of the definition format :

修饰符:现阶段固定为public static两个关键字。
返回值类型:方法最终产生的结果数据是什么类型。
方法名称:自定义的名称,见面知意,命名规则和变量一样。
参数类型:进入方法的数据是什么类型。
参数名称:进入方法的数据对应的变量名称。
方法体:方法内部执行的若干行代码。
return:俩个作用,1.结束方法的执行
                                2.并且将返回值返还给调用处。
返回值:方法最终产生的结果数据。

note:

The return value must correspond to the return value type.
If there are multiple parameters, they need to be separated by commas.
If the parameter does not exist, the parentheses can be left blank.
The order of definition of multiple methods does not matter.
You cannot define a method inside a method.
After the method is defined, it will not be executed if it is not called; if it is to be executed, it must be called.

There are three common ones:

单独调用。这种方式无法使用方法的返回值。格式:方法名称(参数值);
打印调用。这种方式可以将方法的返回值直接打印。格式:System.out.println(方法名称(参数值));
赋值调用。这种方式可以将方法的返回值赋值给一个变量,注意变量的数据类型必须和方法的返回值类型对应。格式:数据类型变量名称= 方法名称(参数值)

Guess you like

Origin blog.51cto.com/14954398/2544244