Java面向对象基础--引用传递分析

来源:互联网 发布:网络银行贷款 编辑:程序博客网 时间:2024/04/29 18:27
class Demo{int temp = 30 ;// 此处为了方便,属性暂时不封装};public class RefDemo01{public static void main(String args[]){Demo d1 = new Demo() ;// 实例化Demo对象,实例化之后里面的temp=30 d1.temp = 50 ;// 修改temp属性的内容System.out.println("fun()方法调用之前:" + d1.temp) ;fun(d1) ;System.out.println("fun()方法调用之后:" + d1.temp) ;}public static void fun(Demo d2){// 此处的方法由主方法直接调用d2.temp = 1000;// 修改temp值}};

public class RefDemo02{public static void main(String args[]){String str1 = "hello" ;// 实例化字符串对象System.out.println("fun()方法调用之前:" + str1) ;fun(str1) ;// 调用fun()方法System.out.println("fun()方法调用之后:" + str1) ;}public static void fun(String str2){// 此处的方法由主方法直接调用str2 = "MLDN" ;// 修改字符串内容}};


class Demo{String temp = "hello" ;// 此处为了方便,属性暂时不封装};public class RefDemo03{public static void main(String args[]){Demo d1 = new Demo() ;// 实例化Demo对象,实例化之后里面的temp=30 d1.temp = "world" ;// 修改temp属性的内容System.out.println("fun()方法调用之前:" + d1.temp) ;fun(d1) ;System.out.println("fun()方法调用之后:" + d1.temp) ;}public static void fun(Demo d2){// 此处的方法由主方法直接调用d2.temp = "MLDN";// 修改temp值}};


class Person{// 定义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 void setName(String n){name = n ;}public void setAge(int a){age = a ;}public String getName(){return name ;}public int getAge(){return age ;}public void setBook(Book b){book = b ;}public Book getBook(){return book ;}public void setChild(Person c){child = c ;}public Person getChild(){return child ;}};class Book{// 定义Book类private String title ;// 标题private float price ;// 价格private Person person ;// 一本书属于一个人public Book(String title,float price){this.setTitle(title) ;this.setPrice(price) ;}public void setTitle(String t){title = t ;}public void setPrice(float p){price = p ;}public String getTitle(){return title ;}public float getPrice(){return price ;}public void setPerson(Person p){person = p ;}public Person getPerson(){return person ;}};public class RefDemo06{public static void main(String arg[]){Person per = new Person("张三",30) ;Person cld = new Person("张草",10) ;// 定义一个孩子Book bk = new Book("JAVA SE核心开发",90.0f) ;Book b = new Book("一千零一夜",30.3f) ;per.setBook(bk) ;// 设置两个对象间的关系,一个人有一本书bk.setPerson(per) ;// 设置两个对象间的关系,一本书属于一个人cld.setBook(b) ;// 设置对象间的关系,一个孩子有一本书b.setPerson(cld) ;// 设置对象间的关系,一本书属于一个孩子per.setChild(cld) ;// 设置对象间的关系,一个人有一个孩子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()) ;// 也可以通过书找到其所有人// 通过人找到孩子,并找到孩子所拥有的书System.out.println(per.getName() + "的孩子 --> 姓名:"+ per.getChild().getName() + ";年龄:" + per.getChild().getAge()+ ";书名:" + per.getChild().getBook().getTitle() + ";价格:"+ per.getChild().getBook().getPrice()) ;}};