方法的回顾和调用
package com.oop.demo;
import java.io.IOError;
import java.io.IOException;
//return代表方法结束,返回一个结果
//下方就是一个类
public class Demo01 {
//main方法启动这个程序,一个真正的程序里面只有一个main方法
public static void main(String[] args) {
int c= max(1,3);
System.out.println(c);
}
/*
修饰符 返回值类型 方法名(){
//方法体
return 返回值;
}
*/
public String sayHello(){
return "hello,world";
}
//return代表方法结束,返回一个结果,可以为空
public void hello(){
return;
}
public static int max(int a,int b){
return a>b? a : b;//三元运算符
}
//数组下标异常:arrayindexoutofbounds
//抛出异常,throws抛出的意思,Exception异常
public void readFile(String file) throws IOException{
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.oop.demo;
public class Demo02 {
//静态方法加了static
public static void main(String[] args) {
// Student.say();
//实例化类来调用非静态方法,用new关键字
//new Student();//不常用
//常用//对象类型 对象名 = 对象值
Student student = new Student();
student.say();
}
//非静态方法不加static
public void say(){
System.out.println("学生说话了");
}
//static和类一起加载的
public static void a(){
}
//类实例化之后才存在
public void b(){
a();
}
}
-------------------------------------------------------------------------------------------------------------------------
package com.oop.demo;
//实际参数和形式参数
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Demo03 demo03 = new Demo03();
//实际参数和形式参数的类型要对应!!!
int s = demo03.add(1,2);
System.out.println(s);
//System.out.println("a+b的和为:"+demo03.add(1,2));
}
public int add(int a,int b){
return a+b;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.oop.demo;
//值传递
public class Demo04 {
public static void main(String[] args) {
int a = 1;
System.out.println(a);
Demo04.change(a);
System.out.println(a);
}
//返回值为空
public static void change(int a){
a = 10;
}
}
----------------------------------------------------------------------------------------------------------------------------------------
package com.oop.demo;
//引用传递:传递对象,本质还是值传递
public class Demo05 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);
Demo05.change(person);
System.out.println(person.name);
}
public static void change(Person person){
//person是一个对象:指向的是--->person这个类
//Person person = new Person();这是一个句体的人,可以改变
person.name = "王";
}
}
//定义了一个类,有一个属性:name
class Person{//类下面的字段就属性
String name;//String属性默认值为null
}