修饰符 返回值类型 方法名(){
//方法体
return 返回值;
}
public int max(int a, int b) {
return a > b ? a : b; //三元运算符
}
return
- return结束方法 返回一个结果
- 返回值和返回值类型相同
- void默认返回值为空
- 静态方法static「直接调用」
类名.方法名
Student.test();
- 非静态方法「实例化」
对象类型 对象名=对象值
Student student = new Student(); student.say();
- 值传递
public class Demo04 {
public static void main(String[] args) {
int a=1;
System.out.println(a);
System.out.println("---");
Demo04.change(a);
System.out.println(a);
}
//返回值为空
public static void change(int a){
a=10;
}
}
- 引用传递:对象(本质还是值传递)
public class Demo05 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name); //null
Demo05.change(person);
System.out.println(person.name); //测试名
}
public static void change(Person person){
//person是一个对象 指向person这个类 ---Person person = new Person();
person.name ="测试名";
}
}
//定义一个person类
class Person{
String name; //默认值null
}



