其实OOP编程的主要作用也是使你的代码修改和扩展变的更容易,那么小白要问了,既然函数都能实现这个需求了,还要OOP干毛线用呢? 呵呵,说这话就像,古时候,人们打仗杀人都用刀,后来出来了枪,它的主要功能跟刀一样,也是杀人,然后小白就问,既然刀能杀人了,那还要枪干毛线,哈哈,显而易见,因为枪能更好更快更容易的杀人。函数编程与OOP的主要区别就是OOP可以使程序更加容易扩展和易更改。
#!/usr/bin/env python
# Author:Xue Bao
#定义一个类
class Dog:
#传名字
def __init__(self,name):
self.name = name
def bulk(self):
print("%s: wang wang wang!" % self.name)
#
d1 = Dog("张三")
d2 = Dog("李四")
d3 = Dog("王麻")
d1.bulk()
d2.bulk()
d3.bulk()
#!/usr/bin/env python
# Author:Xue Bao
#写了四个类
class School(object):
def __init__(self,name,addr):
self.name = name
self.addr = addr
self.students = []
self.staffs = []
def enroll(self,stu_obj):
print("为学员%s 办理注册手册" %stu_obj.name)
self.students.append(stu_obj)
def hire(self,staff_obj):
self.staffs.append(staff_obj)
print("雇佣新员工%s 办理注册手册" %staff_obj.name)
#写的时候先搭建框架
class SchoolMember(object):
#构造函数
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
def tell(self):
pass
class Teacher(SchoolMember):
def __init__(self,name,age,sex,salary,course):
super(Teacher,self).__init__(name,age,sex)
self.salary = salary
self.course = course
def tell(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)) #在老师这重构方法
def teach(self):
print("%s is teaching course [%s]" %(self.name,self.course))
class Student(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
def tell(self):
#打印自己信息
print ( '''
------info of Teacher:%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)) #重构方法
def pay_tuition(self,amount):
print("%s has paid tution for $%s" %(self.name,amount))
#实例化学校
school = School("老男孩IT","海淀")
t1 = Teacher("oldboy",56,"MAN",20000,"linux")
t2 = Teacher("alex",22,"MF",2000,"python")
s1 = Student("test",36,"test",1001,"python")
s2 = Student("test2",26,"test2",1002,"linux")
t1.tell()
s1.tell()
school.hire(t1)
school.enroll(s1)
school.enroll(s2)
print(school.students)
print(school.staffs)
school.staffs[0].teach()
for stu in school.students:
stu.pay_tuition(5000)
#!/usr/bin/env python
# Author:Xue Bao
#写了四个类
class School(object):
def __init__(self,name,addr):
self.name = name
self.addr = addr
self.students = []
self.staffs = []
def enroll(self,stu_obj):
print("为学员%s 办理注册手册" %stu_obj.name)
self.students.append(stu_obj)
def hire(self,staff_obj):
self.staffs.append(staff_obj)
print("雇佣新员工%s 办理注册手册" %staff_obj.name)
#写的时候先搭建框架
class SchoolMember(object):
#构造函数
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
def tell(self):
pass
class Teacher(SchoolMember):
def __init__(self,name,age,sex,salary,course):
super(Teacher,self).__init__(name,age,sex)
self.salary = salary
self.course = course
def tell(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)) #在老师这重构方法
def teach(self):
print("%s is teaching course [%s]" %(self.name,self.course))
class Student(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
def tell(self):
#打印自己信息
print ( '''
------info of Teacher:%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)) #重构方法
def pay_tuition(self,amount):
print("%s has paid tution for $%s" %(self.name,amount))
#实例化学校
school = School("老男孩IT","海淀")
t1 = Teacher("oldboy",56,"MAN",20000,"linux")
t2 = Teacher("alex",22,"MF",2000,"python")
s1 = Student("test",36,"test",1001,"python")
s2 = Student("test2",26,"test2",1002,"linux")
t1.tell()
s1.tell()
school.hire(t1)
school.enroll(s1)
school.enroll(s2)
print(school.students)
print(school.staffs)
school.staffs[0].teach()
for stu in school.students:
stu.pay_tuition(5000)