引用传递
1.三道引用范例
1.1 范例一
package ref;
class Demo1{
int temp=30;//此处暂时不封装
}
public class RefDemo1 {
public static void main(String[] args) {
//初始化对象
Demo1 d1=new Demo1();
//对temp1重新赋值
d1.temp=50;
System.out.println("重新赋值后的值为多少:"+d1.temp);//50
//调用fun方法之后
fun(d1);//大胆猜一下这里会输出多少
System.out.println(d1.temp);//1000
}
public static void fun(Demo1 d2){
d2.temp=1000;
}
}
1.2范例二
package ref;
public class RefDemo2 {
public static void main(String[] args) {
String str1= "hello";
//调用func方法之前输出的是什么?
System.out.println(str1);//hello
func(str1);
System.out.println(str1);//hello
}
public static void func(String str2){
str2="MOON";
}
}
1.3范例三
package ref;
class Demo2{
String temp="hello";//此处暂时不封装
}
public class RefDemo3 {
public static void main(String[] args) {
//初始化对象
Demo2 d1=new Demo2();
//对temp1重新赋值
d1.temp="world";
System.out.println("重新赋值后的值为多少:"+d1.temp);//world
//调用fun方法之后
fun(d1);
System.out.println("调用fun方法后的值:"+d1.temp);//MLDN
}
public static void fun(Demo2 d2){
d2.temp="MLDN";
}
}
本程序和第一个程序是一样的,都输出了不同的值
2.接受本类引用
package ref;
class Demo{
private int temp=30;
public void fun(Demo d2){
d2.temp=50;
}
public int getTemp() {
return temp;
}
public void setTemp(int temp) {
this.temp = temp;
}
}
public class RefDemo5 {
public static void main(String[] args) {
Demo d1=new Demo();
d1.setTemp(50);
d1.fun(d1);
System.out.println(d1.getTemp());//50
}
}
3范例:一对一
3.1 加入一个人有一本书,一本书属于一个人。书具有的属性:title、price、person;人具有的属性:name、age、book。
package ref;
class Person{
private String name;
private int age;
private Book book;//一个人有一本书
public Person(String name,int age){
this.setName(name);
this.setAge(age);
//this.setBook(book);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
}
class Book{
private String title;
private float price;
private Person person;//一本书只属于一个人
public Book(String title,float price){
this.setTitle(title);
this.setPrice(price);
//this.setPerson(person);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
public class RefDemo6 {
public static void main(String[] args) {
Person per=new Person("张三",40);
Book bk=new Book("三国演义",12.5f);
per.setBook(bk);
bk.setPerson(per);
System.out.println("从人找到书===》姓名:"+per.getName()+",年龄:"
+per.getAge()+",书名:"+per.getBook().getTitle()+",书的价格:"
+per.getBook().getPrice());
System.out.println("从书找到人===》书名:"+bk.getTitle()+",书的价格:"
+bk.getPrice()+",人名字:"+bk.getPerson().getName()+",年纪:"
+bk.getPerson().getAge());
}
}
E:\Java\jdk\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:50539,suspend=y,server=n -javaagent:E:\Java\IDEA\intelliij\.IntelliJIdea2019.1\system\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "E:\Java\jdk\jre\lib\charsets.jar;E:\Java\jdk\jre\lib\deploy.jar;E:\Java\jdk\jre\lib\ext\access-bridge-64.jar;E:\Java\jdk\jre\lib\ext\cldrdata.jar;E:\Java\jdk\jre\lib\ext\dnsns.jar;E:\Java\jdk\jre\lib\ext\jaccess.jar;E:\Java\jdk\jre\lib\ext\jfxrt.jar;E:\Java\jdk\jre\lib\ext\localedata.jar;E:\Java\jdk\jre\lib\ext\nashorn.jar;E:\Java\jdk\jre\lib\ext\sunec.jar;E:\Java\jdk\jre\lib\ext\sunjce_provider.jar;E:\Java\jdk\jre\lib\ext\sunmscapi.jar;E:\Java\jdk\jre\lib\ext\sunpkcs11.jar;E:\Java\jdk\jre\lib\ext\zipfs.jar;E:\Java\jdk\jre\lib\javaws.jar;E:\Java\jdk\jre\lib\jce.jar;E:\Java\jdk\jre\lib\jfr.jar;E:\Java\jdk\jre\lib\jfxswt.jar;E:\Java\jdk\jre\lib\jsse.jar;E:\Java\jdk\jre\lib\management-agent.jar;E:\Java\jdk\jre\lib\plugin.jar;E:\Java\jdk\jre\lib\resources.jar;E:\Java\jdk\jre\lib\rt.jar;E:\Java\project\out\production\Demo01;E:\Java\IDEA\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar" ref.RefDemo6
Connected to the target VM, address: '127.0.0.1:50539', transport: 'socket'
从人找到书===》姓名:张三,年龄:40,书名:三国演义,书的价格:12.5
从书找到人===》书名:三国演义,书的价格:12.5,人名字:张三,年纪:40
Disconnected from the target VM, address: '127.0.0.1:50539', transport: 'socket'
Process finished with exit code 0
3.2如果在有一个孩子类,一个孩子只有一本书,一本书只属于一个孩子。
!!!!!!我不知道哪里错了,希望大佬指正。