简体   繁体   中英

calling methods using object reference variable

I read an example of serialization in the SCJP book . Below i have posted that example source code

import java.io.*;
public class SerializeDog {
    public static void main(String[] args) {
        Collar c = new Collar(3);
        Dog d = new Dog(c, 5);
        System.out.println("before: collar size is "+ d.getCollar().getCollarSize());
        try {
            FileOutputStream fs = new FileOutputStream("testSer.ser"); 
            ObjectOutputStream os = new ObjectOutputStream(fs);
            os.writeObject(d);
            os.close();
        }catch (Exception e) { 
            e.printStackTrace(); 
        }
        try {
            FileInputStream fis = new FileInputStream("testSer.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            d = (Dog) ois.readObject();
            ois.close();
        } catch (Exception e) { 
            e.printStackTrace(); 
        }
        System.out.println("after: collar size is "**+ d.getCollar().getCollarSize());**
    }
}

class Dog implements Serializable {
    private Collar theCollar;
    private int dogSize;
    public Dog(Collar collar, int size) {
        theCollar = collar;
        dogSize = size;
    }
    public Collar getCollar() { 
        return theCollar; 
    }
}

class Collar implements Serializable {
    private int collarSize;
    public Collar(int size) { 
        collarSize = size; 
    }
    public int getCollarSize() { 
        return collarSize; 
    }
}

i want to know when we call d.getCollar().getCollarSize(); which method gets executed... and please explain what kind of method calling is this in java. how can we use two methods like this.

i want to know when we call d.getCollar().getCollarSize(); which method gets executed...

JAVA invoke/execute gettCollar() on object d , and returns a instance of Color . getCollarSize() will be invoked on Collar instance returned by d.getColor() method.

This is how, we chain the method invocation.

You can split the method call into two steps

Collar collar = d.getCollar();
int collarSize = co.getCollarSize();
object.firstMethod().secondMethod().thirdMethod()

Execution is always goes from left to right . On Object firstMethod() is called which returns some object and on that secondMethod() is called which returns some Object and on that thirdMethod() is called and so on...

In your case d.getCollar().getCollarSize() d is object of class Dog on which getCollar() method is called which returns instance of Collar on which again getCollarSize() is called.

This is called Method Chaining

In your comment if I am not wrong you want to ask it is a normal uninstantiated variable and how one can call method on it?

    Collar c = new Collar(3);
    Dog d = new Dog(c, 5);

In above two lines First you create Collar instance and Call Parameterised contructor of Dog class.

private Collar theCollar;
private int dogSize;
public Dog(Collar collar, int size) {
    theCollar = collar;
    dogSize = size;
}

Here in above code you assign that instantiated object of Collar to private Collar theCollar;

d.getCollar()返回一个'Collar'类型的对象,你在这个对象上调用第二个方法getCollarSize()。

The same as this statements:

Collar co = d.getCollar();//invoke the getCollar method of a Dog object
int size = co.getCollarSize();//invoke the getCollarSize of a Collar object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM