方法回顾
//Demo01 类
public class Demo01 {
//main 方法
public static void main(String[] args) {
}
/*
修饰符 返回值类型 方法名(...){
//方法体
return 返回值; //return表示方法结束,返回一个结果
}
*/
public String sayHello(){
return "hello,world";
}
public int max(int a , int b){
return a>b? a: b;//三元运算符
}
//IO流 读文件 抛出异常
public void readFile(String file) throws IOException {
}
}
静态方法与非静态方法
//学生类
public class Student {
//静态方法
/*
类名.方法名 //使用方法
static方法是和类一起加载的,时间片较早
*/
public static void say1(){
System.out.println("学生说话了");
}
//非静态方法
/*
需要把类实列化 通过new关键字
非静态方法在类实列化之后才存在
*/
public void say2(){
System.out.println("学生说话了");
}
}
Student.say1();//static 静态方法的调用
//实列化这个Student这个类,然后使用say2方法
Student student = new Student();//对象类型 对象名 = 对象值;
student.say2();//调用方法
值传递与引用传递
//java是值传递
public static void main(String[] args) {
int a = 1;
Demo04.change(a);
System.out.println(a);//输出a的值为1
}
//返回值为空
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 = new Person();这是一个具体的人,可以改变属性
person.name = "秦江";
}
}
//一个类只能有一个public class 但是可以有多个class
//定义了一个person类,有一个属性:name
class Person{
String name;//默认null
}
标签:静态方法,14,回顾,void,person,static,oop,public,String
From: https://www.cnblogs.com/Mc9r4dy/p/18263661