回顾方法的定义和调用
回顾 方法:
方法
package com.oop.demo01;
import java.io.IOException;
//Demo01 类
public class Demo01 {
//main 方法
public static void main(String[] args) {
}
/*
修饰符 返回值类型 方法名(...){
//方法体
return 返回值;
}
*/
//结束方法,返回一个结果
public String sayHello() {
return "Hello,world";
}
public void hello() {
return;
}
public int max(int a, int b) {
return a > b ? a : b;//三元运算符
}
//数组下标越界:Arrays index out of bounds
//异常
public void readFire(String file) throws IOException{
}
}
静态方法和非静态方法
package com.oop.demo01;
public class Demo02 {
public static void main(String[] args) {
//静态方法 : 类.方法()
Student.say();
//非静态方法
//实例化这个类 new
//对象类型 对象名 = 对象值
Student stu = new Student();
stu.say1();
}
//static修饰时,和类一起加载
public static void a(){
//b();//此时不可调用
}
//类实例化之后才存在
public void b(){
}
}
接下
package com.oop.demo01;
//学生类
public class Student {
//静态方法
public static void say() {
System.out.println("静态:学生说话了");
}
//非静态方法
public void say1() {
System.out.println("非静态:学生说话了");
}
}
值传递和引用传递
package com.oop.demo01;
//值传递
public class Demo04 {
public static void main(String[] args) {
int a = 1;
System.out.println(a);
change(a);
System.out.println(a);//1
}
//返回值为空
public static void change(int a) {
a = 10;
}
}
package com.oop.demo01;
//引用传递:对象 ,本质还是值传递
public class Demo05 {
public static void main(String[] args) {
Person per = new Person();
System.out.println(per.name);//null
Demo05.change(per);
System.out.println(per.name);//天
}
//返回值为空
public static void change(Person per) {
//person是一个对象: 指向的 --->Person per = new Person();这是一个具体的人,可以改变属性!
per.name = "天";
}
}
class Person {
String name;
}