An object is a combination of a feature (variable) and a skill (function).
and a class is a collection of features and skills common to a series of objects.
classTeacher:lesson="python" def __init__(self,name,color,age):#only do the initialized liveSelf.name =nameif notisinstance (NAME,STR):RaiseTypeError Self.color=Color Self.age= AgedefJineng (self):Print('New Skills') T1= Teacher (' as','Pink', 13)#T1 can be an instance of the teacher class, or an object
classTeacher:#featuresSchool ='Oldboy'Lesson_default='python' #Skills def __init__(self,name,age,sex,lesson2): Self.name=name Self.age=Age Self.sex=Sex Self.lesson2=Lesson2defSpeak (self):Print('Lectures') defEat (self):Print('Eat') T1= Teacher ('Alex',' at','male','operation and Maintenance') T2= Teacher ('Egon',' -','male','operation and Maintenance')
Class Usage One: instantiating the resulting object
Usage of Class Two: Property reference
Print (teacher.__dict__)
# Properties of the object
# Properties of the class
# property, which is a dictionary
# object to see a property, first from the object properties, if not, from the class to find
Inherited:
Inheritance is a way to create new classes, and the benefit is that you can reduce duplicate code
Inheritance is the relationship between classes and classes, and what is a relationship of what
classpeople:def __init__(self,name,age,sex): Self.name=name Self.age=Age Self.sex=SexdefWalk (self):Print('%s is walking'%self.name)deffoo (self):Print('From father%s'%self.name)classTeacher (People): School='Oldboy' def __init__(self,name,age,sex,level,salary): People.__init__(self,name,age,sex) Self.level=Level Self.salary=SalarydefBar (self): People.foo (self)Print('From teacher')classStudent (people):PassT= Teacher ('Egon', 13,'male', 10,3000)#print (t.name,t.age)Print(T.__dict__) T.walk () T.bar ()
Combination
The combination is a kind of what has what the relationship
classpeople:def __init__(self, name, age, year, Mon, day): Self.name=name Self.age=Age Self.birth=Date (year, Mon, day) # combinationdefWalk (self):Print('%s is walking'%self.name)classDate:def __init__(self, year, Mon, day): Self.year=Year Self.mon=Mon self.day= DaydefTell_birth (self):Print('born in <%s> year <%s> month <%s> Day'%(Self.year, Self.mon, self.day))classTeacher (people):def __init__(self, name, age, year, Mon, day, level, salary): people.__init__(self, name, age, year, Mon, day) self.level=Level Self.salary=SalarydefTeach (self):Print('%s is teaching'%self.name)classStudent (people):def __init__(self, name, age, year, Mon, day, Group): People.__init__(self, name, age, year, Mon, day) Self.group=GroupdefStudy (self):Print('%s is studying'%self.name) T= Teacher ('Egon', 12, 1991, 12, 12, 10, 1000) T.teach () T.walk ()Print(T.level)Print(T.birth.tell_birth ())
Interface
1. Subclasses must have a method of the parent class
2. Subclasses must implement a method that is the same as the name of the parent class's method
classFile:#defining an interface interface class to mimic the concept of an interface, there is no interface keyword in Python to define an interface. defRead (self):#Fixed interface function Read Pass defWrite (self):#defining an interface function write PassclassTXT (File):#text that implements read and write specifically defdu (self):Print('How to read text data') defXie (self):Print('How to read text data')classSata (File):#disk, specifically implemented read and write defRead (self):Print('How to read hard disk data') defWrite (self):Print('How to read hard disk data')classProcess (File):defRead (self):Print('method of reading process data') defWrite (self):Print('method of reading process data') txt=Txt () disk=Sata () process=Process () txt.du ()#Disk.read ()#Process.read ()
Subclasses must implement a method that is the same as the name of the parent class's method
ImportABCclassFile (metaclass=ABC. Abcmeta): @abc. AbstractmethoddefRead (self):Pass@abc. AbstractmethoddefWrite (self):PassclassTXT (File):#text that implements read and write specifically defRead (self):Pass defWrite (self):PassT= Txt ()
Python3 object-oriented, class, inheritance, composition, derivation, interface, subclass reuse Parent-class methods