Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1549295
  • 博文数量: 157
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4116
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-14 18:26
文章分类

全部博文(157)

文章存档

2014年(67)

2013年(90)

分类: Java

2014-04-01 09:02:16

引用传递的本质也是传值。

通过引用传递可以改变实参对象的内容,但不能改变实参对象。


public class Cup {
    String owner; //水杯的主人
    String content;
    Cup() {
    }
    Cup(String owner, String content) {
        this.owner = owner;
        this.content = content;
    }
    public static void changeContent(Cup c) {
        System.out.println("在changeContent方法里面1:" + c.hashCode());
        c.content = "普洱茶";
        c = new Cup("张无忌", "可口可乐");
        System.out.println("在changeContent方法里面2:" + c.hashCode());
    }
    public void showContent() {
        System.out.println(this.owner + "的水杯,里面装着:" + this.content);
    }
    public static void main(String[] args) {
        Cup c = new Cup("张三丰", "碧螺春");
        c.showContent();
        System.out.println("----------------------");
        System.out.println("在主方法里面:" + c.hashCode());
        Cup.changeContent(c);
        c.showContent();
    }
}
/*
    张三丰的水杯,里面装着:碧螺春
    ----------------------
    在主方法里面:9800632
    在changeContent方法里面1:9800632
    在changeContent方法里面2:521452
    张三丰的水杯,里面装着:普洱茶
*/



this指向当前对象,指调用当前方法的那个对象。

1,指向当前对象。

2,调用构造方法。
   
public class Person {
    String name;
    String gender;
    int age;
                                                        
    Person() {
        System.out.println("一个人诞生了。。。。。。");
        this.name = "无名氏";
        this.gender = "男";
        this.age = 18 ;
    }
                                                        
    Person(String name, int age) {
        this(); //必须放在构造方法的第一句
        this.name = name;
        this.age = age;
    }
                                                        
    Person(String name, String gender, int age) {
        this(name, age);
        this.gender = gender;
    }
                                                        
    public void introduce () {
        System.out.println("我叫:" + this.name + ",性别: " + this.gender + ",今年" + this.age +"岁。");
    }
                                                        
    public static void main(String[] args) {
        // Person p = new Person();
        Person p = new Person("郭美美", "女", 20);
        p.introduce();
    }
}
/*
 -------------------------
    一个人诞生了。。。。。。
    我叫:郭美美,性别: 女,今年20岁。
 -------------------------
*/


1,设计学生类

2,设计图书类

3,学生成员方法中传递图书的引用。

=============================

书名:java编程思想,借书人:无名氏

----------结束之后--------

书名:java编程思想,借书人:李明

=============================

 
//图书类
class Books {
    String name; //书名
    String author; //作者
    String press; // 出版处
    String lender; //借书人
    Books(String name, String author, String press) {
        this.name = name;
        this.author = author;
        this.press = press;
        this.lender = "无名氏";
    }
                                                   
    public void showInfo() {
        System.out.println("书名:" + this.name + ",借书人:" + this.lender);
    }
}
//学生类
public class Students {
    String sid; //学号
    String name; //姓名
    int age; //年龄
                                                   
    Students(String sid, String name, int age) {
        this.sid = sid;
        this.name = name;
        this.age = age;
    }
                                                   
    public void lendBook(Books book) {
        //book = new Books("java设计模式", "四人帮", "清华大学出版社");
        /*
         * 加上上面一行后,显示
         * =================================
                书名:java编程思想,借书人:无名氏
                ----------结束之后--------
                书名:java编程思想,借书人:无名氏
         * =================================
         */
        book.lender = this.name;
                                                       
    }
                                                   
    public static void main(String[] args) {
        Books b = new Books("java编程思想","侯敏", "电子工业出版社");
        Students s = new Students("S001", "李明", 20);
        b.showInfo();
        s.lendBook(b);
        System.out.println("----------结束之后--------");
        b.showInfo();
    }
}


阅读(1747) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~