1, the definition of inheritance
Inheritance means: You can use all the features of an existing class, and without having to rewrite these functions extend the case of the original class.
(1) Create a new class by inheriting called "sub-class" or "derived class."
(2) inherited classes are called "base class", "parent" or "superclass."
Succession process, that is, from the general to the particular process. To achieve inheritance, can be achieved by "inheritance" (Inheritance) and "combination" (Composition).
In certain OOP languages, subclasses inherit a plurality of base classes. But under normal circumstances, a subclass can have only one base class to implement multiple inheritance can be achieved through multiple inheritance.
2, inherited classification
Implementation of the concept of inheritance are mainly two types: inheritance, interface inheritance.
(1) without implementation inheritance refers to the ability of the encoded additional properties and methods of the base class;
(2) the name of interface inheritance is only the attributes and methods, but subclass must provide the ability to implement the (parent subclass reconstruction method);
When considering the use of inheritance, one thing to note that the relationship between the two classes should be "part of" relationship.
Abstract class defines the general properties and methods created by subclasses only.
OO development paradigm roughly: abstract class division → → Object classes organized as a hierarchical structure (inherited and synthesized) Example → class with design and implementation stages.
3, the sample code
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: ZhengzhengLiu
# Class inheritance
classPeople:
def__init__(self,name,age):
self.name = name
self.age = age
defeat(self):
print("%s is eating..."%self.name)
defsleep(self):
print("%s is sleeping..."%self.name)
deftalk(self):
print("%s is talking..."%self.name)
classMan (People): # class inherits the parent class People
defmake_money(self):
print("%s is making money..."%self.name)
defsleep(self):
People.sleep (self) # extension of the parent class method
print("man is sleeping...")
classWomen(People):
defshop(self):
print("%s is shopping..."%self.name)
m1 = Man("Jack","20")
m1.eat()
m1.make_money()
m1.sleep()
w1 = Women("Amy","25")
w1.talk()
w1.shop()
#operation result:
Jackiseating...
Jackismaking money...
Jackissleeping...
manissleeping ...
Amyistalking...
Amyisshopping...
4, the subclass reconstruct the two methods of the parent class constructor
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: ZhengzhengLiu
# Class inheritance
#class People: # Classic
classPeople (object): # new-style class
def__init__(self,name,age):
self.name = name
self.age = age
defeat(self):
print("%s is eating..."%self.name)
defsleep(self):
print("%s is sleeping..."%self.name)
deftalk(self):
print("%s is talking..."%self.name)
classMan (People): # class inherits the parent class People
def__init__(self,name,age,money):
#People .__ init __ (self, name, age) # (Method a) reconstructing of the constructor, not the parent class add attributes
super (Man, self) .__ init __ (name, age) # (Method 2) to be reconstructed constructor (New Class written) using super
self.money = money
print("%s have money %s$"%(self.name,self.money))
defmake_money(self):
print("%s is making money..."%self.name)
defsleep(self):
People.sleep (self) # extension of the parent class method
print("man is sleeping...")
classWomen(People):
defshop(self):
print("%s is shopping..."%self.name)
m1 = Man("Jack","20",10)
m1.eat()
m1.make_money()
m1.sleep()
w1 = Women("Amy","25")
w1.talk()
w1.shop()
#operation result:
Jack have money10$
Jackiseating...
Jackismaking money...
Jackissleeping...
manissleeping ...
Amyistalking...
Amyisshopping...
5, multiple inheritance
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: ZhengzhengLiu
# Class inheritance
#class People: # Classic
classPeople (object): # new-style class
def__init__(self,name,age):
self.name = name
self.age = age
self.friends = []
defeat(self):
print("%s is eating..."%self.name)
defsleep(self):
print("%s is sleeping..."%self.name)
deftalk(self):
print("%s is talking..."%self.name)
classRelationship(object):
defmake_friends(self,obj):
print("%s is making friends with %s"%(self.name,obj.name))
self.friends.append(obj)
classMan (People, Relationship): # multiple inheritance
def__init__(self,name,age,money):
#People .__ init __ (self, name, age) # (Method a) reconstructing of the constructor, not the parent class add attributes
super (Man, self) .__ init __ (name, age) # (Method 2) to be reconstructed constructor (New Class written) using super
self.money = money
print("%s have money %s$"%(self.name,self.money))
defmake_money(self):
print("%s is making money..."%self.name)
defsleep(self):
People.sleep (self) # extension of the parent class method
print("man is sleeping...")
classWomen (People, Relationship): # multiple inheritance
defshop(self):
print("%s is shopping..."%self.name)
m1 = Man("Jack","20",10)
w1 = Women("Amy","25")
m1.make_friends(w1)
w1.name ="liu"
print(m1.friends)
#operation result:
Jack have money10$
Jackismaking friendswithAmy
[<__main__.Women object at0x0057FA30>]
6, the order of succession of the new class and Classic
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: ZhengzhengLiu
classA (object): # new-style class
def__init__(self):
print("A")
classB(A):
def__init__(self):
print("B")
classC(A):
def__init__(self):
print("C")
classD(B,C):
def__init__(self):
pass
#print("D")
obj = D()
7, inheritance example - schools, teachers and students
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: ZhengzhengLiu
# Inherit instance (the new class) - Analog schools, teachers and students
classSchool(object):
def__init__(self,name,addr):
self.name = name
self.addr = addr
self.students = []
self.stuffs = []
defenroll (self, stu_obj): # Student Registration
print ( "% s student for registration"% stu_obj.name)
self.students.append(stu_obj)
defheir (self, staff_obj): # hire teachers
print ( "hire teachers% s"% staff_obj.name)
self.stuffs.append(staff_obj)
classSchoolMember(object):
def__init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
deftell(self):
pass
classTeacher(SchoolMember):
def__init__(self,name,age,sex,salary,course):
super(Teacher,self).__init__(name,age,sex)
self.salary = salary
self.course = course
deftell(self):
print('''
----- info of Teacher:%s -----
Name:%s
Age:%s
Sex:%s
Salary:%s
Course:%s
'''%(self.name,self.name,self.age,self.sex,self.salary,self.course))
defteach(self):
print("%s is teaching course [%s]"%(self.name,self.course))
classStudent(SchoolMember):
def__init__(self,name,age,sex,stu_id,grade):
super(Student,self).__init__(name,age,sex)
self.stu_id = stu_id
self.grade = grade
deftell(self):
print('''
----- info of Student:%s -----
Name:%s
Age:%s
Sex:%s
Stu_id:%s
Grade:%s
'''%(self.name,self.name,self.age,self.sex,self.stu_id,self.grade))
defpay_tuition(self,amount):
print("%s has paied tuition for $%s"%(self.name,amount))
# Instantiated
school = School("qinghua","beijing")
t1 = Teacher("Jack","30","M","20000","Python")
t2 = Teacher("Amy","28","F","15000","Linux")
s1 = Student("liu","23","M","1701","Python")
s2 = Student("wang","25","F","1702","Linux")
# Display information call students and teachers
t1.tell()
s1.tell()
school.heir (t1) # t1 hire teachers
school.enroll (s1) # s1 student registration
school.enroll(s2)
print(school.stuffs)
print(school.students)
# First teacher hired to teach
school.stuffs[0].teach()
forstuinschool.students:
stu.pay_tuition(5000)
#operation result:
----- info of Teacher:Jack -----
Name:Jack
Age:30
Sex:M
Salary:20000
Course:Python
----- info of Student:liu -----
Name:liu
Age:23
Sex:M
Stu_id:1701
Grade:Python
Teachers hired Jack
liu students for registration
wang student for registration
[<__main__.Teacher object at0x0059FDB0>]
[<__main__.Student object at0x0059FDF0>, <__main__.Student object at0x0059FE10>]
Jackisteaching course [Python]
liu has paied tuitionfor$5000
wang has paied tuitionfor$5000
8, polymorphism (polymorphisn) - An interface, a variety of forms
(1) the definition of
Polymorphism (polymorphisn) that allows you to set the parent object becomes equal and one or more of his sub-object technology,
After this assignment, the parent object can operate in different ways depending on the characteristics of the current assigned to its child objects.
Simply put, in one sentence: allow assignment of a subclass type pointer to the parent class type pointer.
Polymorphic role: We know package may hide implementation details, such modular codes; Inheritance spreading code modules may exist (s); object thereof is to - code reuse.
The polymorphism is a further object to achieve - Interface reuse! Polymorphic role is to class inheritance and derivation time, ensure that the correct call when a property of any instance of a class "family tree."
Pyhon many polymorphic syntax is supported, such as len (), sorted (), you pass the string to len returns the length of the string, it returns a list of transfer length of the list.
(2) Sample code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: ZhengzhengLiu
classAnimal(object):
def__init__(self,name):
self.name = name
deftalk(self):
raiseNotImplementedError("Subclass must implement abstract method")
Polymorphism # - An interface, a variety of forms
@staticmethod
defanimal_talk(obj):
obj.talk()
classCat(Animal):
deftalk(self):
print("%s Meow!"%self.name)
classDog(Animal):
deftalk(self):
print("%s Woof! Woof!"% self.name)
d = Dog("A")
#d.talk()
c = Cat("B")
#c.talk()
# Polymorphism
Animal.animal_talk(d)
Animal.animal_talk(c)
#operation result:
A Woof! Woof!
B Meow!
9, weapon Object Oriented Design - Domain Modeling
(1) the definition of
From the beginning of the domain model, we started to object-oriented analysis and design process, we can say, the domain model is complete from requirements analysis to bridge the object-oriented design.
Domain model, by definition, is a field of modeling requirements involved, the argument is more popular business model.
(2) the domain model has two main functions:
Explore important concepts business
Establish a relationship between the concept of business
(3) three character field modeling
Domain model is so important, domain modeling approach summarize is "to find the noun"! Even a simple find such an operation term, also involves the analysis and refining, rather than simply picking out can,
In this case, analysts and designers experience and skills can come in handy. But the analysis also does domain model is relatively simple, even without a wealth of experience and superb skills, at least to complete a usable domain model.
A key question: Where to find the model because the field is "demand to object-oriented bridge", can think of:? Looking from the demand model, specifically, it is to find from use cases.
Summarized: domain modeling approach is to "find a noun from use cases." Of course, after finding the noun, in order to be more consistent with object-oriented requirements and characteristics.
We also need to further improve on these terms, this is the next step: add properties, even relationships!
Finally, we conclude that the three character domain modeling method: find a noun, add attributes, even relationships.