One, class declaration and class body
Two, local variables and member variables
Class Lader{
float bottom; //这是变量的声明
float computerArea(){
//这是方法的定义
}
}
Member variable: also known as instance variable, member variable with static added is also known as class variable/static variable
Local variable: local variable is local variable, there is no other name
- When declaring that the member variable is global → powerful → it has a default initial value, you don’t need to assign an initial value to it; but if you need to assign an initial value to it, you must assign the initial value while declaring it, not Assign initial value individually
- Local variables: There is no default initial value.
class A{
int a=12; //ok
int x;
x=6; //error
}
Three, instance variables and class variables
class A{
int x; //实例变量
static int y; //类变量
}
What is the difference between the two? See Tencent Weiyun "Chapter Three ⑥ Instance Variables and Class Variables"
Four, instance method, class method, construction method
Class Lader{
float bottom; //这是变量的声明
float computerArea(){
//这是方法的定义
}
}
Construction method: no return value, the method name is the same as the class name.
Class method: static modified.
Instance method: except the above two are all instance methods, the instance method is a general method
Common methods:
//无参数的方法头
int sprak(){
return 12;
}
//有参数的方法头
int add(int x,int y,int z){
return x+y+z;
}
//无返回值的方法头
void speak(){
System.out.println("无返回值是无return,不是无输出")
}
// public/private + static +void/int/double +方法头的
public static void myMethod(){
}
10. Two ways to pass values to x and y in Class A
给Class A中x,y传值的两种方式,看清这两种传值的方式,你看代码的时候会大大节约时间
方式一:调用a.setXY(1,2);来传值
Class A{
int x,y;
void setXY(int m,int n){
x=m;
y=n
}
Int getXY(){
return x,y
}
}
public class Hekko {
public static void main(String args[]){
A a=new A();
a.setXY(1,2);
k,t=a.getXY();
}
}
方式二:a.x=1; a.y=2;的方式传值
Class A{
int x,y;
void speak(){
System.out.println("我没有动x和y")
}
Int getXY(){
return x,y
}
}
public class Hekko {
public static void main(String args[]){
A a=new A();
a.x=1;
a.y=2;
a.sprak();
k,t=a.getXY();
}
}
Using the above knowledge, you can see the code below and it should be fast