跟随学习第二十六天
public class Demo01{
public static void main(String args[]){//一个程序正常只有一个main方法,又称主方法
}
//返回值方法
//修饰符 + 返回值类型 + 方法名
public String name(){
return "Hello,world!"
}
//不需要返回值的方法
public int max(int a,int b){
return a>b ? a : b;//如果a>b输出a,否则输出b;运用到了三元运算符 (条件 ? 前值 :后值)
}
//break和return的区别:break跳出循环,return跳出方法
}
//方法的调用
public class Demo02{
public static void main(String[] args){
//我调用下面的静态Student类,类名 + 方法名
Student.say();
//调用非静态类math
Math math = new math();
}
//方法分成静态和非静态方法 区别:静态定义时有加上static,非静态没有加
//形式参数和实际参数
public void print{
int add = Demo02.add(a:1,b:2)//当调用下面的add方法是要保持形式参数和实际参数相同类型
//形式参数就是(int a,int b)这是不存在的值,实际参数是(a:1,b:2)前面的int就是为了让两种参数进行统一类型int类型
}
public void add(int a,int b){
return a+b;
}
}
public class Student{
public static void say(){
System.out.println("学生上课开小差");
}
}
public class math{
public void max(int a,int b){
return a>b ? a : b;
}
}
浙公网安备 33010602011771号