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;
	}
}

 

 例子<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";
	}
}

 

例子<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";
	}
}

 

一对一关系   例子

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

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());
	}

}
原文地址:https://www.cnblogs.com/tonglin0325/p/5186402.html