JAVA:深入引用

来源:互联网 发布:数据港千股千评 编辑:程序博客网 时间:2024/06/05 03:23
class Demo{  int x=10;}public class Demo010{  public static void main(String[] args){    Demo d=new Demo();    d.x=30;    fun(d);    System.out.println(d.x);  }  public static void fun(Demo temp){    temp.x=100;  }}//out :100//=======================public class Demo011{  public static void main(String[] args){    String str="hello";    fun(str);    System.out.println("str"+str);  }  public static void fun(String temp){    temp="world";    System.out.println("temp"+temp);  }}//out :tempworld//     strhello//=======================class Demo{  String x="xxxtest";}public class Demo012{  public static void main(String[] args){    Demo d=new Demo();    d.x="hello";    fun(d);    System.out.println("d.x"+d.x);  }  public static void fun(Demo temp){    temp.x="world";    System.out.println("temp.x"+temp.x);  }}//out :world//     world//=======================class Person{private String name ;private int age;private Book book ;// 一个人有一本书public Person(String n,int a){this.setName(n) ;this.setAge(a) ;}public void setBook(Book b){book = b ;}public void setName(String n){name = n ;}public void setAge(int a){age = a ;}public Book getBook(){return book ;}public String getName(){return name ;}public int getAge(){return age ;}};class Book{private String title ;private float price ;private Person person ;public Book(String t,float p){this.setTitle(t) ;this.setPrice(p) ;}public void setPerson(Person p){person = p ;}public void setTitle(String t){title = t ;}public void setPrice(float p){price = p ;}public Person getPerson(){return person ;}public String getTitle(){return title ;}public float getPrice(){return price ;}};public class Demo013{public static void main(String args[]){Person per = new Person("张三",30) ;Book bk = new Book("Java基础",89.0f) ;per.setBook(bk) ;// 一个人有一本书bk.setPerson(per) ;// 一本书属于一个人System.out.println(per.getBook().getTitle()) ;// 由人找到其所拥有书的名字System.out.println(bk.getPerson().getName()) ;// 由书找到人的名字}};

0 0