简体   繁体   English

使用对象引用变量调用方法

[英]calling methods using object reference variable

I read an example of serialization in the SCJP book . 我在SCJP书中读到了序列化的一个例子。 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(); 我想知道什么时候调用d.getCollar().getCollarSize(); which method gets executed... and please explain what kind of method calling is this in java. 哪个方法被执行...请在java中解释这是什么样的方法调用。 how can we use two methods like this. 我们怎样才能使用这样的两种方法。

i want to know when we call d.getCollar().getCollarSize(); 我想知道什么时候调用d.getCollar().getCollarSize(); which method gets executed... 哪个方法被执行...

JAVA invoke/execute gettCollar() on object d , and returns a instance of Color . JAVA在对象d上调用/执行gettCollar() ,并返回Color的实例。 getCollarSize() will be invoked on Collar instance returned by d.getColor() method. getCollarSize()将在被调用Collar由返回的实例d.getColor()方法。

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... Object firstMethod() ,它返回一些object并且调用secondMethod() ,返回一些Object并调用thirdMethod() ,依此类推...

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. 在你的情况下, d.getCollar().getCollarSize() d是类Dog的对象,在其上调用getCollar()方法,该方法返回Collar实例,同时再调用getCollarSize()

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. 在上面两行中首先创建Dog类的Collar实例和Call Parameterised构造函数。

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; 在上面的代码中,您将Collar的实例化对象分配给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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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