Java学习Day05 回顾方法
方法的定义
public class Demo01 {
public static void main ( String [ ] args) {
}
public String sayHello ( ) {
return "Hello World" ;
}
public int max ( int a, int b)
{
return a> b? a: b;
}
public void readFile ( String file) throws IOException {
}
}
静态方法和非静态方法的关系
public class Demo02 {
public static void main ( String [ ] args) {
Cat. Cat ( ) ;
Student student = new Student ( ) ;
student. say ( ) ;
}
public void a ( ) {
b ( ) ;
}
public static void b ( ) {
}
}
重新创建一个类
public class Student {
public void say ( ) {
System . out. println ( "学生说话了" ) ;
}
}
public class Cat {
public static void Cat ( ) {
System . out. println ( "这是一只猫" ) ;
}
}
形参和实参
public class Demo03 {
public static void main ( String [ ] args) {
int add = Demo03 . add ( 1 , 2 ) ;
System . out. println ( add) ;
}
public static int add ( int a, int b) {
return a+ b;
}
}
值传递
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 ;
}
}
引用传递
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. name = "御狐王" ;
}
}
class Person {
String name;
}