2018-03-06
First, the method of the class:
In Java, "functions" in C + + + are called "methods", which means "How to do something."
The composition of the method: name, parameter, return value, and method body.
public
class
Hero {
String name;
//姓名
float
hp;
//血量
float
armor;
//护甲
int
moveSpeed;
//移动速度
//获取护甲值
float
getArmor(/*Argument list*/){
//float是调用方法后返回的数据类型。
Argument list: The type and name of the method name and parameter list that defines the information passed to the method together uniquely identifies a method.
return
armor;
}
//坑队友
void
keng(){
Void is no return value
System.out.println(
"坑队友!"
);
}
}
Methods in Java can only be created as part of a class, and are invoked through an object. Attempting to invoke a method that does not exist on an object will give you a compile error message.
By Object Invocation Method:
Objectname.methodname (Arg1,arg2,arg3,...);
Formal parameters: Used to receive externally-passed data variables when a method is invoked
Parameter type: Data type of formal parameter
Return value: Method is returned to the program data that called it after execution is completed
Return value type: Method The data type of the result to return
Arguments: Data that is actually passed to the method form parameter when the method is called
Second, the example method:
Method declarations are called class methods that are decorated with static, and are called instance methods without static adornments.
The difference between a class method and an instance method:
Methods in a class can be called to each other, where instance methods can invoke instance methods or class methods in the class;
However, a class method can only invoke a class method in the class, and cannot invoke an instance method;
All member variables, including instance variables or class variables, can be called in an instance method.
However, class methods can only manipulate class variables. The instance method must be called through the object, and the class method can be called directly from the class name.
Java Basics/Eighth lesson: Object-Oriented/Java class methods and instance methods