MENU

方法重载、匿名对象、引用传递

June 5, 2018 • Read: 2626 • Java阅读设置

1.方法重载

参数个数不同,参数类型不同都叫方法重载,返回值类型不同不叫重载

2.匿名对象

class Strudent{
    public void tell(){
        System.out.println("I am Student");
    }
}
public static void main(String args[]){
    Student stu1 = new Student();//正常创建对象
    stu1.tell();
    new Student().tell();//创建匿名对象
}

3.引用传递

/*常规传递*/
public satic void main(String args[]){
    String str1 = "hello";
    System.out.println(str1);//hello
    tell(str1);
    System.out.println(str1);//hello
}
public staitc void tell(String str2){
    str2 = "jike";
}

/*引用传递*/
class Ref2{
    String temp = "hello"
}
public static void main(String args[]){
    Ref2 r1 = new Ref2();
    r1.temp = "jike";
    tell(r1);
    System.out.println(r1.temp);//xueyuan
}
public static void tell(Ref2 r2){
    r2.temp = "xueyuan";
}

Last Modified: February 8, 2020
Archives Tip
QR Code for this page
Tipping QR Code
Leave a Comment