java 引用机制_Java引用机制——reference

所谓引用传递就是指将堆内存空间的使用权交给多个栈内存空间。

例子<1>

public class Aliasing {

int temp = 30;

public static void main(String[] args) {

// TODO 自动生成的方法存根

Aliasing d1 = new Aliasing();

d1.temp = 50;

System.out.println(d1.temp);

fun(d1);

System.out.println(d1.temp);

}

public static void fun (Aliasing d2){

d2.temp = 1000;

}

}

34d4c0ee7e1473815558621839c06dbe.png

例子<2> 其中传递的是string对象,由于string的内容是不可以修改,所以str1的值还是hello,如果传递的是对象的string属性,那是可以修改的

public class Aliasing {

int temp = 30;

public static void main(String[] args) {

// TODO 自动生成的方法存根

String str1 = "hello";

System.out.println(str1);

fun(str1);

System.out.println(str1);

}

public static void fun (String str2){

str2 = "hello2";

}

}

4abf3a70b03c27abcbab6e2b7b3fe346.png

例子<3>传递的是对象的string属性

public class Aliasing {

String temp = "hello";

public static void main(String[] args) {

// TODO 自动生成的方法存根

Aliasing d1 = new Aliasing();

d1.temp = "world";

System.out.println(d1.temp);

fun(d1);

System.out.println(d1.temp);

}

public static void fun (Aliasing d2){

d2.temp="HELLO";

}

}

d25c3783668b2fd2494821d6f9273636.png

一对一关系   例子

一个人对应一本书,一本书对应一个人

class Person{

private String name;

private int age;

private Book book;

public Person(String name,int age){

this.setName(name);

this.setAge(age);

}

public String getName(){

return name;

}

public void setName(String n){

name = n;

}

public int getAge(){

return age;

}

public void setAge(int a){

age = a;

}

public Book getBook(){

return book;

}

public void setBook(Book b){

book = b;

}

}

class Book{

private String title;

private float price;

private Person person;

public Book(String title,float price){

this.setTitle(title);

this.setPrice(price);

}

public String getTitle(){

return title;

}

public void setTitle(String t){

title = t;

}

public float getPrice(){

return price;

}

public void setPrice(float p){

price = p;

}

public Person getPerson(){

return person;

}

public void setPerson(Person person){

this.person = person;

}

}

public class reference {

public static void main(String[] args) {

// TODO 自动生成的方法存根

Person per = new Person("zhangsan",30);

Book bk = new Book("JAVA SE kaifa",90.0f);

per.setBook(bk);

bk.setPerson(per);

System.out.println(" name "+per.getName()+" age "+per.getAge()+" book "+per.getBook().getTitle()+" price "+per.getBook().getPrice());

System.out.println(" title "+bk.getTitle()+" price "+bk.getPrice()+" person "+bk.getPerson().getName()+" age "+bk.getPerson().getAge());

}

}

一个人对应一本书,一本书对应一个人,一个孩子对应一本书,一本书对应一个孩子,一个人对应一个孩子

class Person{

private String name;

private int age;

private Book book;

private Person child;

public Person(String name,int age){

this.setName(name);

this.setAge(age);

}

public String getName(){

return name;

}

public void setName(String n){

name = n;

}

public int getAge(){

return age;

}

public void setAge(int a){

age = a;

}

public Book getBook(){

return book;

}

public void setBook(Book b){

book = b;

}

public Person getChild(){

return child;

}

public void setChild(Person child){

this.child = child;

}

}

class Book{

private String title;

private float price;

private Person person;

public Book(String title,float price){

this.setTitle(title);

this.setPrice(price);

}

public String getTitle(){

return title;

}

public void setTitle(String t){

title = t;

}

public float getPrice(){

return price;

}

public void setPrice(float p){

price = p;

}

public Person getPerson(){

return person;

}

public void setPerson(Person person){

this.person = person;

}

}

public class reference {

public static void main(String[] args) {

// TODO 自动生成的方法存根

Person per = new Person("zhangsan",30);

Person cld = new Person("zhangcao",10);

Book bk = new Book("JAVA SE kaifa",90.0f);

Book b = new Book("11111",30.0f);

per.setBook(bk);

bk.setPerson(per);

cld.setBook(b);

b.setPerson(cld);

per.setChild(cld);

System.out.println(" name "+per.getName()+" age "+per.getAge()+" book "+per.getBook().getTitle()+" price "+per.getBook().getPrice());

System.out.println(" title "+bk.getTitle()+" price "+bk.getPrice()+" person "+bk.getPerson().getName()+" age "+bk.getPerson().getAge());

System.out.println(" cldname "+per.getChild().getName()+" age "+per.getChild().getAge()+" book "+per.getChild().getBook().getTitle()+" price "+per.getChild().getBook().getPrice());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值