黑马程序员技术交流社区
标题: 面向对象学习心得 [打印本页]
作者: wp1063102686 时间: 2018-5-31 20:41
标题: 面向对象学习心得
面向对象有几个特性:
封装、继承
封装具备封装性(Encapsulation)的面向对象程序设计隐藏了某一方法的具体运行步骤,取而代之的是通过消息传递机制发送消息给它。封装是通过限制只有特定类的对象可以访问这一特定类的成员,而它们通常利用接口实现消息的传入传出。
继承继承性(Inheritance)是指,在某种情况下,一个类会有“子类”。子类比原本的类(称为父类)要更加具体化。
多态多态(Polymorphism)是指由继承而产生的相关的不同的类,其对象对同一消息会做出不同的响应。
抽象抽象(Abstraction)是简化复杂的现实问题的途径,它可以为具体问题找到最恰当的类定义,并且可以在最恰当的继承级别解释问题。
看具体的Python程序:
class Role(object): n = 123 #类变量 def __init__(self, name, role, weapon, life_value=100, money=15000): #构造函数 self.name = name self.role = role self.weapon = weapon self.life_value = life_value self.money = money def shot(self): print("shooting...") def got_shot(self): print("ad..., I got shot...") def buy_gun(self, gun_name): print("just bought %s" % gun_name) def __del__(self): print("I'm dead")r1 = Role("Alex", "police", \'AK47\')r2 = Role("Jack", "terrorist", "B22")- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
从这个程序可以看出类的几个组成,构造函数,析构函数,类的方法,类的属性,类变量。如何声明一个类,并且使用类的方法和属性。
如果要声明类的私有属性和方法,则在变量或者函数前面加__
def __private(self): print("I'm private")继承class School(object): def __init__(self, name, addr): self.name = name self.addr = addr self.students = [] self.staff = [] def enroll(self, stu_obj): print("为学员%s办理注册手续" % stu_obj.name) self.students.append(stu_obj) def hire(self, staff_obj): print("雇佣新员工%s" % staff_obj.name) self.staff.append(staff_obj)class SchoolMember(object): def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex def tell(self): passclass 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 Student %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 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)) def pay_tuition(self, amount): print("%s has paid tuition for $%s" % (self.name, amount))school = School("老男孩IT", "沙河")t1 = Teacher("Oldboy", 56, "MF", 2000000, "Linux")t2 = Teacher("Alex", 22, "M", 3000, "PythonDev")s1 = Student("ChenRonghua", 36, "MF", 1001, "Python")s2 = Student("xuliangwei", 19, "M", 1002, "Linux")t1.tell()s1.tell()school.hire(t1)school.enroll(s1)school.enroll(s2)print(school.staff)print(school.students)school.staff[0].teach()for stu in school.students: stu.pay_tuition(5000)- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
由上面的程序可以看到python中继承的用法。
多态python不支持多态,但是可以模拟出来多态的用法:
class Animal: def __init__(self, name): self.name = name def talk(self): passclass Cat(Animal): def talk(self): return 'Meow!'class Dog(Animal): def talk(self): return 'Woof! Woof!'animals = [Cat('Missy'), Dog('Lassie')]for animal in animals: print animal.name + ': ' + animal.talk()
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |