📜  Java中的Java .rmi.MarshalledObject 类

📅  最后修改于: 2022-05-13 01:55:16.883000             🧑  作者: Mango

Java中的Java .rmi.MarshalledObject 类

Java.rmi.MarshalledObject是一个Java类,MarshalledObject 包含一个字节流,该字节流具有赋予其构造函数的对象的序列化表示,包含的对象使用用于编组和解组参数的相同序列化语义进行序列化和反序列化。

签名

public final class MarshalledObject extends Object implements Serializable

构造函数

MarshalledObject(T object) – MarshalledObject(T object) 初始化 MarshalledObject 类的新实例,其中包含所提供对象当前状态的序列化表示。

MarshalledObject m = new MarshalledObject(T object);

方法

MarshalledObject 类包含三个名为 -

  1. 哈希码()
  2. 得到()
  3. 等于(对象对象)

让我们分别讨论这个类的所有三种方法,以便更好地理解。开始了:

1. MarshalledObject.hashCode() 方法

它是Java.rmi.MarshalledObject 类的一部分,hashCode() 方法将返回与此 MarshalledObject 关联的哈希码。

句法:

public int hashCode().

方法返回类型: hashCode() 方法具有 int 返回类型,并将返回此 MarshalledObject 的哈希码

如何调用 hashCode() 方法

第一步:首先创建一个 MarshalledObject 的实例,并传递要序列化的对象。

MarshalledObject marshalledObject = new MarshalledObject(object);

第 2 步:现在调用 hashCode() 方法来获取这个 marshalledObject 的哈希码

int code = marshalledObject.hashCode();

示例: Java程序使用 MarshalledObject.hashCode() 方法获取哈希码

Java
// Java program to 
// get the hash code
import java.io.*;
import java.rmi.*;
  
// create a serialized class
class tmp implements Serializable {
    public int x;
}
class GFG {
    public static void main(String[] args)
    {
        // invoke getHashCode 
          // method to get hashCode
        getHashCode(new tmp());
    }
    @SuppressWarnings("unchecked")
    public static void getHashCode(tmp t)
    {
        try {
            MarshalledObject marshalledObject = new MarshalledObject(t);
            
            // invoke hashCode method for this
            // marshalledObject to get hash code
            System.out.println("Hash code for this marshalled object is " + marshalledObject.hashCode());
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}


Java
// Java program to get 
// copy of marshalledObject
import java.io.*;
import java.rmi.*;
  
// create a serialized class
class tmp implements Serializable {
    public int x;
}
class GFG {
    public static void main(String[] args)
    {
        // invoke get method to get 
          // copy of marshalledObject
        get(new tmp());
    }
    @SuppressWarnings("unchecked")
    public static void get(tmp t)
    {
        try {
            
            MarshalledObject marshalledObject = new MarshalledObject(t);
            
            // invoke get method for this
            // marshalledObject to get copy
              // of marshalled object
            System.out.println("Copy marshalled object is " + marshalledObject.get());
            System.out.println("Original marshalled object is " + t);
  
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}


Java
// Java program to compare 
// two marshalled object
import java.io.*;
import java.rmi.*;
  
// create a serialized class
class tmp implements Serializable {
    public int x;
}
class GFG {
    public static void main(String[] args)
    {
        // invoke compare method to to
          // compare the marshalled object
        compare(new tmp(),new tmp());
    }
    @SuppressWarnings("unchecked")
    public static void compare(tmp a,tmp b)
    {
        try {
            MarshalledObject marshalledObjectOne = new MarshalledObject(a);
            MarshalledObject marshalledObjectTwo = new MarshalledObject(b);
  
            // invoke equals method for this
            // marshalledObject to compare 
              // the marshalled object
            System.out.println("marshalledObjectOne and marshalledObjectTwo are same : "
                + marshalledObjectOne.equals(marshalledObjectTwo));
  
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}


输出:

Hash code for this marshalled object is -571669764

2. MarshalledObject.get() 方法

它是Java.rmi.MarshalledObject 类的一部分,get() 方法将返回包含的 marshalledObject 的新副本。

方法签名

public T get() throws IOException,
ClassNotFoundException.

方法返回类型: get() 方法将返回包含的 MarshalledObject 的副本。

参数: get() 方法无参数

异常: get() 方法可能会抛出 IOException、ClassNotFoundException。

如何调用 get() 方法?

第一步:首先创建一个 MarshalledObject 的实例,并传递要序列化的对象。

MarshalledObject marshalledObject = new MarshalledObject(object);

第 2 步:现在调用 get() 方法来获取这个 marshalledObject 的新副本。

Object obj = marshalledObject.get();

示例: Java程序使用 MarshalledObject.get() 方法获取 MarshalledObject 的副本

Java

// Java program to get 
// copy of marshalledObject
import java.io.*;
import java.rmi.*;
  
// create a serialized class
class tmp implements Serializable {
    public int x;
}
class GFG {
    public static void main(String[] args)
    {
        // invoke get method to get 
          // copy of marshalledObject
        get(new tmp());
    }
    @SuppressWarnings("unchecked")
    public static void get(tmp t)
    {
        try {
            
            MarshalledObject marshalledObject = new MarshalledObject(t);
            
            // invoke get method for this
            // marshalledObject to get copy
              // of marshalled object
            System.out.println("Copy marshalled object is " + marshalledObject.get());
            System.out.println("Original marshalled object is " + t);
  
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

输出:

Copy marshalled object is tmp@66cd51c3
Original marshalled object is tmp@63c12fb0

3. MarshalledObject.equals() 方法

它是Java.rmi.MarshalledObject 类的一部分,equals() 方法会将此 MarshalledObject 与另一个对象进行比较,如果两个序列化对象相同,则此方法将返回 true,否则返回 false。

方法签名

public boolean equals(Object obj).

方法返回类型: equals() 方法具有布尔返回类型,如果参数引用的 MarshalledObject 包含与此对象完全相同的序列化表示,它将返回 true。

方法参数: equals()方法有一个Object Type参数。

如何调用equals()方法?

第 1 步:首先创建 MarshalledObject 的实例并传递要序列化的对象。

MarshalledObject marshalledObjectOne = new MarshalledObject(object);
MarshalledObject marshalledObjectTwo = new MarshalledObject(object);

第 2 步:现在调用 equals() 方法将 marshalledObject 与传递给 equals() 方法的参数进行比较。

boolean isSame = marshalledObjectOne.equals(marshalledObjectTwo);

示例: Java程序使用 MarshalledObject.equals() 方法比较这个 MarshalledObject

Java

// Java program to compare 
// two marshalled object
import java.io.*;
import java.rmi.*;
  
// create a serialized class
class tmp implements Serializable {
    public int x;
}
class GFG {
    public static void main(String[] args)
    {
        // invoke compare method to to
          // compare the marshalled object
        compare(new tmp(),new tmp());
    }
    @SuppressWarnings("unchecked")
    public static void compare(tmp a,tmp b)
    {
        try {
            MarshalledObject marshalledObjectOne = new MarshalledObject(a);
            MarshalledObject marshalledObjectTwo = new MarshalledObject(b);
  
            // invoke equals method for this
            // marshalledObject to compare 
              // the marshalled object
            System.out.println("marshalledObjectOne and marshalledObjectTwo are same : "
                + marshalledObjectOne.equals(marshalledObjectTwo));
  
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

输出:

marshalledObjectOne and marshalledObjectTwo are same : true