方法的结构
一个方法包括一个方法头和一个方法体。具体包含修饰符、返回值类型、方法名、参数类型和方法体
package method;
public class Demo01 {
public static void main(String[] args) {
int www=add(4,9);
System.out.println(www);
}
//创建方法 add
public static int add(int a,int b){//这里的a,b就是形参而上面的www就说实参
/*使用 static 修饰符修饰的属性(成员变量)称为静态变量,
也可以称为类变量,常量称为静态常量,
方法称为静态方法或类方法,它们统称为静态成员,归整个类所有。*/
/*static 修饰的成员变量和方法,从属于类。
普通变量和方法从属于对象。
静态方法不能调用非静态成员,编译会报错。*/
return a+b;
}
}
比较大小
package method;
public class Demo02 {
public static void main(String[] args) {
int max =max(99,66);
System.out.println(max);
}
public static int max(int a,int b){
int result=0;
if(a==b){
System.out.println("两个数相等");
}
if(a>b){
result=a;
}else {
result=b;
}
return result;
}
}