Basis of Class
It
is possible to develop all programs with Python both in a procedural way and in
an object-oriented way. To develop simple hacking programs, it is convenient to
use a procedural manner. However, to develop complex programs that are needed
for operation in an enterprise environment, it is necessary to structure the
program. An object-oriented language can be used to improve productivity during
development by allowing for reusability and inheritance. If you use an
object-oriented language, it is possible to develop a program that is logically
constructed.
The
basic structure to declare a class is as follows.
class name: #(1) def __init__(self, argument): #(2) def functioin(argument): #(3) class name(inherited class ame): #(4) def functioin (argument): |
(1) Create a Class: If you specify a class
name after using the reserved word “class”, the class is declared.
(2) Constructor: The “__ init__” function
is a constructor that is called by default when the class is created. The “self”
pointing to the class itself is always entered as an argument into the constructor.
In particular, the constructor may be omitted when there is no need to
initialize.
(3) Function: It is possible to declare a
function in the class. An instance is then generated to call the function.
(4) Inheritance: In order inherit from
another class, the name of the inherited class must be used as an argument when
the class is declared. Inheritance supports the use of member variables and
functions of the upper class as is.
Creating a Class
Through this example, let us
find out use for the class declaration, initialization, and inheritance by
replacing Example 4-2 with a class.
class Hero: #(1) def __init__(self, name,
age, weight): #(2) self.name = name #(3) self.age = age self.weight = weight def printHero(self): #(4) print "\n" print
"--------------------------------------" print
"1.name:" , self.name #(5) print "2.age:"
, self.age print
"3.weight:" , self.weight class MyHero(Hero): #(6) def __init__(self, inSkill,
inPower, idx): Hero.__init__(self,
"hong gil dong", 18, 69.3) #(7) self.skill = inSkill self.power = inPower self.idx = idx def printSkill(self): print "4.armed
weapon:" , self.skill + "[ power:" , self.power[self.idx],
"]" skill =
["sword","spear","bow","axe"] power = [98.5, 89.2, 100, 79.2] querySkill = raw_input("select weapon: ") i=0 for each_item in skill: if(each_item == querySkill): myHero =
MyHero(querySkill, power, i) #(8) myHero.printHero() #(9) myHero.printSkill() i = i+1 print "--------------------------------------" print "\n" |
Example 1-3 Creating
a Class
(1) Class Declaration: Declare the class “Hero”.
(2) Constructor
Declaration: Declare the constructor that takes three arguments and the
“self” representing the class itself.
(3) Variable Initialization: Initialize the class variables by
assigning the arguments.
(4) Function Declaration: Declare the “printHero” function in the
class.
(5) Using Variables: Use class variables in the format of “self.variable
name”.
(6) Class Inheritance: Declare the “MyHero” class that inherits the “Hero”
class.
(7) Calling the
Constructor: Generate and initialize the object by calling the constructor
of the upper class.
(8) Creating a
Class: Generate a “MyHero” class. Pass along the arguments required to the
constructor.
(9) Calling
Class Function: The tasks are run by
calling the functions that are declared for the “myHero” object.