Classes and Objects
Object is an instantiation of the class
Declare a class, create an object (instance of a class)
# Classes and objects
'' '
to create a class
class class name:
class contents inside
' ''
class CLl:
Pass
'' '
instance of a class:
A CLl = ()
' ''
# Constructor (constructor)
#self: Method in class must be added the self parameter
#__ init __ (self, arguments)
# constructor practical significance: Initialize
class CL2:
DEF __init __ (self):
Print ( "the I AM CL2 self ! ")
# Add parameters to the class: add parameters to the constructor
class CL3 is:
DEF the __init __ (Self, name, Job):
Print ( "My name IS" + name + "My Job IS" + Job)
# Properties: class variable inside: self property name.
Class CL4:
DEF the __init __ (Self, name, Job):
self.myname name =
self.myjob = Job
Method #: class inside a function: def method name (self, parameters)
class CL5:
DEF MyFunc1 (self, name):
Print ( "Hello" + name)
class cl6:
def __init__(self,name):
self.myname=name
def myfunc1(self):
print("hello "+self.myname)
# Inheritance (single inheritance, multiple inheritance)
# one family has a father, mother, son, daughter, father can speak, the mother can write, the eldest son inherited his father
# daughter inherits the parents, and new ability to listen to something, small son inherited his father, but optimization (weaken) the father's ability to speak
#
# father's class (base class)
class father ():
DEF speak (Self):
Print ( "! the I CAN speak")
# single inheritance: class subclass (parent)
class Son (father):
Pass
# mother class
class mother ():
DEF the Write (Self):
Print ( "the I CAN the Write")
# multiple inheritance
class Daughter (father, mother):
DEF the listen (Self):
Print ( "the I CAN the listen!")
# rewrite (overloaded)
class son2 (Father):
DEF Speak (Self):
( "! the I CAN Speak 2") Print