Object serialization: Difference between revisions
Content added Content deleted
m (Alphabetized) |
m (Spelling/grammar/aesthetics) |
||
Line 1: | Line 1: | ||
{{task}} |
{{task}} |
||
Create a set of data types based upon inheritance. Each data type or class should have a print command that displays the contents of an instance of that class to standard output. Create instances of each class in your inheritance hierarchy and display them to standard output. Write each of the objects to a file named ''objects.dat'' in binary form using serialization or marshalling. |
Create a set of data types based upon inheritance. Each data type or class should have a print command that displays the contents of an instance of that class to standard output. Create instances of each class in your inheritance hierarchy and display them to standard output. Write each of the objects to a file named ''objects.dat'' in binary form using serialization or marshalling. Read the file ''objects.dat'' and print the contents of each serialized object. |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Line 198: | Line 198: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
Serialization in PHP is straightforward. |
Serialization in PHP is straightforward. The built-in function [http://www.php.net/manual/en/function.serialize.php serialize()] handles it in a single statement. |
||
$myObj = new Object(); |
$myObj = new Object(); |
||
$serializedObj = serialize($myObj); |
$serializedObj = serialize($myObj); |
||
In order to |
In order to un-serialize the object, use the [http://www.php.net/manual/en/function.unserialize.php unserialize()] function. Note that the class of object must be defined in the script where un-serialization takes place, or the class' methods will be lost. |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 246: | Line 246: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
class Being |
class Being |
||
def initialize( |
def initialize(specialty=nil) |
||
@ |
@specialty=specialty |
||
end |
end |
||
def to_s |
def to_s |
||
"(object_id = #{object_id})\n"+"(#{self.class}):".ljust(12)+to_s4Being+(@ |
"(object_id = #{object_id})\n"+"(#{self.class}):".ljust(12)+to_s4Being+(@specialty ? "\n"+" "*12+@specialty : "") |
||
end |
end |
||
def to_s4Being |
def to_s4Being |
||
Line 263: | Line 263: | ||
end |
end |
||
class |
class Mammal < Earthling |
||
def initialize(type) |
def initialize(type) |
||
@type=type |
@type=type |
||
end |
end |
||
def to_s4Earthling |
def to_s4Earthling |
||
"I am champion in taking care of my offspring and eating |
"I am champion in taking care of my offspring and eating everything I can find, except mammals of type #{@type}." |
||
end |
end |
||
end |
end |
||
Line 289: | Line 289: | ||
diverseCollection=[] |
diverseCollection=[] |
||
diverseCollection << (marsian=Being.new("I come from Mars and like playing hide and seek.")) |
diverseCollection << (marsian=Being.new("I come from Mars and like playing hide and seek.")) |
||
diverseCollection << (me= |
diverseCollection << (me=Mammal.new(:human)) |
||
diverseCollection << (nemo=Fish.new(0.99)) |
diverseCollection << (nemo=Fish.new(0.99)) |
||
diverseCollection << (jannakeMaan=Moonling.new) |
diverseCollection << (jannakeMaan=Moonling.new) |