(1)引用传递

注意画出内存分析图。

例子:

class Person{

       private String name ;

       private int age;

       private Book book ;       // 一个人有一本书

    private Person child;//一个人有一个孩子

       public Person(String n,int a){

              this.setName(n) ;

              this.setAge(a) ;

       }

       public void setBook(Book b){

              book = b ;

       }

       public void setName(String n){

              name = n ;

       }

       public void setAge(int a){

              age = a ;

       }

       public Book getBook(){

              return book ;

       }

       public String getName(){

              return name ;

       }

       public int getAge(){

              return age ;

       }

};

class Book{

       private String title ;

       private float price ;

       private Person person ;////一本书属于一个人,

       public Book(String t,float p){

              this.setTitle(t) ;

              this.setPrice(p) ;

       }

       public void setPerson(Person p){

              person = p ;

       }

       public void setTitle(String t){

              title = t ;

       }

       public void setPrice(float p){

              price = p ;

       }

       public Person getPerson(){

              return person ;

       }

       public String getTitle(){

              return title ;

       }

       public float getPrice(){

              return price ;

       }

};

 

public class RefDemo04{

       public static void main(String args[]){

              Person per = new Person("张三",30) ;

              Book bk = new Book("Java基础",89.0f) ;

              per.setBook(bk) ;           // 一个人有一本书

              bk.setPerson(per) ; // 一本书属于一个人

              System.out.println(per.getBook().getTitle()) ;      // 由人找到其所拥有书的名字

              System.out.println(bk.getPerson().getName()) ;   // 由书找到人的名字

       }

};

为了表示两个不同类的对象之间的联系,则可以在两个类中分别加入类的对象,并为它设置值。

 

2this关键字

this可以调用类中的属性:this.属性;

可以调用类中的方法:this.方法()

可以调用类中的构造方法:this()

还可以表示当前对象:this

class Person{

       private String name ;

       private int age;

       public Person(){

              System.out.println("新的对象产生了。") ;

       }

       public Person(String name){

              this() ;     // 调用无参构造

              this.name = name ;

       }

       public Person(String name,int age){

              this(name) ;    // 调用有一个参数的构造方法

              this.age = age ;

       }

       public void setName(String name){

              this.name = name ;

       }

       public void setAge(int age){

              this.age = age ;

       }

       public String getName(){

              return this.name ;

       }

       public int getAge(){

              return this.age ;

       }

};

public class ThisDemo05{

       public static void main(String args[]){

              Person per = new Person("张三",30) ;

              System.out.println(per.getName() + " --> " + per.getAge()) ;

       }

};

使用this调用构造方法的时候,this语句应该放在第一句。在使用this调用构造方法的时候至少有一个构造方法是没有使用this()调用的,而此构造方法将作为调用的出口,一般这个出口都会使用无参构造完成。

  当前对象:正在调用类中方法的对象。

如果直接输出一个已经实例化的对象,则输出的是这个对象的地址。

this.属性表示的是当前对象的属性。

(3)对象的比较

类中的对象能访问类中的私有属性。两个对象的比较,比较的是两个对象的属性是相等;另外如果两个对象的引用地址相同则是相等的对象。

class Person{

       private String name ;

       private int age;

       public Person(String name,int age){

              this.name = name ;

              this.age = age ;

       }

       public boolean compare(Person per){

              if(this==per){ // 地址相等

                     return true ;

              }

              if(this.name.equals(per.name)&&this.age==per.age){

                     return true ;

              }else{

                     return false ;

              }

       }

       public String getName(){

              return this.name ;

       }

       public int getAge(){

              return this.age ;

       }

};

 

public class CompareDemo03{

       public static void main(String args[]){

              Person per1 = new Person("张三",30) ;

              Person per2 = new Person("张三",30) ;

              if(per1.compare(per1)){

                     System.out.println("是同一个人!") ;

              }

       }

};

posted on 2011-01-21 19:40  魔战  阅读(442)  评论(0编辑  收藏  举报