12 引用传递
引用传递(1)
1 class Demo{ 2 int temp = 30 ; // 此处为了方便,属性暂时不封装 3 }; 4 public class RefDemo01{ 5 public static void main(String args[]){ 6 Demo d1 = new Demo() ; // 实例化Demo对象,实例化之后里面的temp=30 7 d1.temp = 50 ; // 修改temp属性的内容 8 System.out.println("fun()方法调用之前:" + d1.temp) ; 9 fun(d1) ; 10 System.out.println("fun()方法调用之后:" + d1.temp) ; 11 } 12 public static void fun(Demo d2){ // 此处的方法由主方法直接调用 13 d2.temp = 1000; // 修改temp值 14 } 15 };
内存分析:
引用传递(2)
1 class Demo{ 2 String temp = "hello" ; // 此处为了方便,属性暂时不封装 3 }; 4 public class RefDemo03{ 5 public static void main(String args[]){ 6 Demo d1 = new Demo() ; // 实例化Demo对象,实例化之后里面的temp=30 7 d1.temp = "world" ; // 修改temp属性的内容 8 System.out.println("fun()方法调用之前:" + d1.temp) ; 9 fun(d1) ; 10 System.out.println("fun()方法调用之后:" + d1.temp) ; 11 } 12 public static void fun(Demo d2){ // 此处的方法由主方法直接调用 13 d2.temp = "MLDN"; // 修改temp值 14 } 15 };
内存分析:
引用传递(3)
1 public class RefDemo02{ 2 public static void main(String args[]){ 3 String str1 = "hello" ; // 实例化字符串对象 4 System.out.println("fun()方法调用之前:" + str1) ; 5 fun(str1) ; // 调用fun()方法 6 System.out.println("fun()方法调用之后:" + str1) ; 7 } 8 public static void fun(String str2){ // 此处的方法由主方法直接调用 9 str2 = "MLDN" ; // 修改字符串内容 10 } 11 };
内存分析:
第一,第二类似(fun(Demo d2)),第三比较特殊(fun(String str2)),注意fun方法接收的类,String是比较特殊的类,其内容不可改变。
一对一关系(关系模型----JavaEE开发中最重要的部分)
1 class Person{ // 定义Person类 2 private String name ; // 姓名 3 private int age ; // 年龄 4 private Book book ; // 一个人有一本书 5 private Person child ; // 一个人有一个孩子 6 public Person(String name,int age){ 7 this.setName(name) ; 8 this.setAge(age) ; 9 } 10 public void setName(String n){ 11 name = n ; 12 } 13 public void setAge(int a){ 14 age = a ; 15 } 16 public String getName(){ 17 return name ; 18 } 19 public int getAge(){ 20 return age ; 21 } 22 public void setBook(Book b){ 23 book = b ; 24 } 25 public Book getBook(){ 26 return book ; 27 } 28 public void setChild(Person c){ 29 child = c ; 30 } 31 public Person getChild(){ 32 return child ; 33 } 34 }; 35 class Book{ // 定义Book类 36 private String title ; // 标题 37 private float price ; // 价格 38 private Person person ; // 一本书属于一个人 39 public Book(String title,float price){ 40 this.setTitle(title) ; 41 this.setPrice(price) ; 42 } 43 public void setTitle(String t){ 44 title = t ; 45 } 46 public void setPrice(float p){ 47 price = p ; 48 } 49 public String getTitle(){ 50 return title ; 51 } 52 public float getPrice(){ 53 return price ; 54 } 55 public void setPerson(Person p){ 56 person = p ; 57 } 58 public Person getPerson(){ 59 return person ; 60 } 61 }; 62 public class RefDemo06{ 63 public static void main(String arg[]){ 64 Person per = new Person("张三",30) ; 65 Person cld = new Person("张草",10) ; // 定义一个孩子 66 Book bk = new Book("JAVA SE核心开发",90.0f) ; 67 Book b = new Book("一千零一夜",30.3f) ; 68 per.setBook(bk) ; // 设置两个对象间的关系,一个人有一本书 69 bk.setPerson(per) ; // 设置两个对象间的关系,一本书属于一个人 70 cld.setBook(b) ; // 设置对象间的关系,一个孩子有一本书 71 b.setPerson(cld) ; // 设置对象间的关系,一本书属于一个孩子 72 per.setChild(cld) ; // 设置对象间的关系,一个人有一个孩子 73 System.out.println("从人找到书 --> 姓名:" + per.getName()+";年龄:" 74 + per.getAge() +";书名:" + per.getBook().getTitle() + ";价格:" 75 + per.getBook().getPrice()) ; // 可以通过人找到书 76 System.out.println("从书找到人 --> 书名:" + bk.getTitle() + ";价格:" 77 + bk.getPrice() + ";姓名:" + bk.getPerson().getName() + ";年龄:" 78 + bk.getPerson().getAge()) ; // 也可以通过书找到其所有人 79 // 通过人找到孩子,并找到孩子所拥有的书 80 System.out.println(per.getName() + "的孩子 --> 姓名:" 81 + per.getChild().getName() + ";年龄:" + per.getChild().getAge() 82 + ";书名:" + per.getChild().getBook().getTitle() + ";价格:" 83 + per.getChild().getBook().getPrice()) ; 84 } 85 };

浙公网安备 33010602011771号