15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Objects 573<br />

After you define a class, you can create an object from that class by declaring<br />

a variable as a new class type. In Perl, you create an object by creating a<br />

constructor method commonly called new, such as<br />

Book VI<br />

Chapter 3<br />

my $variablename = classname->new();<br />

In Python, create an object like this:<br />

objectname = new classname();<br />

Perl and Python<br />

To use inheritance in Perl, use the @ISA variable inside a new class, such as<br />

package newobject;<br />

use class2inheritfrom;<br />

@ISA = qw(class2inheritfrom);<br />

To use inheritance in Python, identify the class to inherit from when you<br />

create a new class, such as<br />

class ClassName (class2inheritfrom):<br />

Data<br />

Data<br />

def methodname (self):<br />

Commands<br />

Commands<br />

Commands<br />

To inherit from multiple classes in Python, define additional classes, separated<br />

by a comma, such as<br />

class ClassName (class2inheritfrom, anotherclass):<br />

Data<br />

Data<br />

def methodname (self):<br />

Commands<br />

Commands<br />

Commands

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!