class Book {
private String title;
private double price;
private Owner owner;
public Book(){
System.out.println("此处输出50行代码");
}
public Book(String title){
this(); //this 调用本类构造方法
this.title = title; // this.属性表示的是本类属性,这样即使与方法中的参数重名也可以明确定位
}
public Book(String title, double price) {
this(title); //调用本类构造方法
this.price = price; // this.属性表示的是本类属性,这样即使与方法中的参数重名也可以明确定位
}
public void setOwner(Owner owner){
this.owner = owner ;
}
public Owner getOwner(){
return this.owner ;
}
// setter、getter略
public void print(){
System.out.println("产生新的一个对象。");
System.out.println("当前对象内存地址 = " + this ); //引用当前对象
}
public String getInfo() {
this.print(); // this 可以调用本类的普通方法
return "书名:" + title + ",价格:" + price;
}
}
class Message {
private int num = 10;
public Message(int num){
this.num = num ;
}
public void setNum(int num){
this.num = num ;
}
public int getNum(){
return this.num ;
}
}
class Owner {
private String name ;
private int ownerID ;
private Book book ; // book ownered by Owner
private Owner child ; // the child of the owner, if null,then no child
public Owner(String name, int ownerID){
this.name = name ;
this.ownerID = ownerID ;
}
public void setBook(Book book){
this.book = book ;
}
public Book getBook(){
return this.book;
}
public void setChild(Owner child){
this.child = child ;
}
public Owner getChild(){
return child;
}
public String getInfo(){
return "OwnerID " + this.ownerID + " OwnerName " + this.name ;
}
}
public class Object210404 {
public static void main(String args[]) {
Book book = new Book("Java开发", 89.2);
Book booka = new Book();
Book bookb = new Book("JavaB");
Book bookc = new Book("javaC",99);
Book bookd = bookc; //引用传递
System.out.println(book.getInfo());
System.out.println(booka.getInfo());
System.out.println(bookb.getInfo());
System.out.println(bookc.getInfo());
System.out.println(bookd.getInfo());
long maxControl = Runtime.getRuntime().maxMemory();//获取虚拟机可以控制的最大内存数量Byte
long currentUse = Runtime.getRuntime().totalMemory(); //当前可用内存数量
System.out.println(maxControl);
System.out.println(currentUse);
//第一道引用传递
Message msgOne = new Message(20);
System.out.println(msgOne.getNum()); //输出 20
funOne(msgOne);
System.out.println(msgOne.getNum()); //output 30
//第二道引用传递
String msg = "Hello" ;
fun(msg);
System.out.println(msg); //输出 Hello
Book bookx = new Book("bookxyinxue",110);
Book booky = new Book("book二十四时辰",120);
Owner ownerx = new Owner("hongfei",11);
Owner ownerxchild = new Owner("zjoy",12);
ownerx.setChild(ownerxchild);
bookx.setOwner(ownerx);
booky.setOwner(ownerxchild);
ownerx.setBook(bookx);
ownerxchild.setBook(booky);
System.out.println(ownerx.getBook().getInfo() ); // search for book info by Ownername
System.out.println(ownerx.getChild().getBook().getInfo() );//search for the child's book by the name of owner
System.out.println(bookx.getOwner().getInfo());
}
public static void funOne(Message tempOne){
tempOne.setNum(30);
}
public static void fun(String temp){
temp = "World";
}
}
执行后的输出如下:```
D:\Java>java Object210404
此处输出50行代码
此处输出50行代码
此处输出50行代码
此处输出50行代码
产生新的一个对象。
当前对象内存地址 = Book@1be6f5c3
书名:Java开发,价格:89.2
产生新的一个对象。
当前对象内存地址 = Book@e9e54c2
书名:null,价格:0.0
产生新的一个对象。
当前对象内存地址 = Book@65ab7765
书名:JavaB,价格:0.0
产生新的一个对象。
当前对象内存地址 = Book@1b28cdfa
书名:javaC,价格:99.0
产生新的一个对象。
当前对象内存地址 = Book@1b28cdfa
书名:javaC,价格:99.0
1031798784
65011712
20
30
Hello
此处输出50行代码
此处输出50行代码
产生新的一个对象。
当前对象内存地址 = Book@4c873330
书名:bookxyinxue,价格:110.0
产生新的一个对象。
当前对象内存地址 = Book@119d7047
书名:book二十四时辰,价格:120.0
OwnerID 11 OwnerName hongfei