Jump to content

Object serialization: Difference between revisions

m
Spelling/grammar/aesthetics
m (Alphabetized)
m (Spelling/grammar/aesthetics)
Line 1:
{{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. Read the file ''objects.dat'' and print the contents of each serialized object.
 
=={{header|Ada}}==
Line 198:
 
=={{header|PHP}}==
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();
$serializedObj = serialize($myObj);
In order to unserializeun-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 unserializationun-serialization takes place, or the class' methods will be lost.
 
=={{header|Python}}==
Line 246:
=={{header|Ruby}}==
class Being
def initialize(specialityspecialty=nil)
@specialityspecialty=specialityspecialty
end
def to_s
"(object_id = #{object_id})\n"+"(#{self.class}):".ljust(12)+to_s4Being+(@specialityspecialty ? "\n"+" "*12+@specialityspecialty : "")
end
def to_s4Being
Line 263:
end
class MamalMammal < Earthling
def initialize(type)
@type=type
end
def to_s4Earthling
"I am champion in taking care of my offspring and eating eveythingeverything I can find, except mamalsmammals of type #{@type}."
end
end
Line 289:
diverseCollection=[]
diverseCollection << (marsian=Being.new("I come from Mars and like playing hide and seek."))
diverseCollection << (me=MamalMammal.new(:human))
diverseCollection << (nemo=Fish.new(0.99))
diverseCollection << (jannakeMaan=Moonling.new)
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.