面对对象基础,

定义一个狗的类,

class dog,

#! /usr/bin/env python

class dog:
    def __init__(self,name,gender,type):
        self.name=name
        self.gender=gender
        self.type=type

    def bark(self):
        print('one dog %s is %s,wangwang' %(self.name,self.type))

    def yao_ren(self):
        print('%s zai yaoren '%(self.name))


dog1=dog('alex','female','jingba')
dog2=dog('kevinx','female','lachang')

dog1.bark()
dog2.yao_ren()
View Code

 学校的类,继承基础,

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('hire a new man %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 is:%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('oldboy','shahe')

t1 = Teacher('oldboy',56,'M',30000,'Linux')
t2 = Teacher('alex',22,'M',20000,'PythonDevOps')

s1 = Student('mark',32,'M',10001,'PythonDevOps')
s2 = Student('kevin',22,'M',10002,'Linux')

t1.tell()
s2.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)
View Code

---info of Teacher is:oldboy----
Name = oldboy
Age = 56
Sex = M
Salary = 30000
Course = Linux
---info of Student:kevin----
Name:kevin
Age":22
Sex:M
Stu_id:10002
Grade:Linux
hire a new man oldboy
mark 为学院办理注册手续
kevin 为学院办理注册手续
[<__main__.Student object at 0x0000000002340780>, <__main__.Student object at 0x00000000023407B8>]
[<__main__.Teacher object at 0x0000000002340710>]
oldboy is teaching course [Linux]
mark has paid tuition for $5000
kevin has paid tuition for $5000

 

多态例子:

class Animal(object):
    def __init__(self,name):
        self.name = name

    @staticmethod
    def animal_talk(obj):
        obj.talk()

    def talk(self):
        pass

class Dog(Animal):
    def talk(self):
        print('wangwangwang!!!')

class Cat(Animal):
    def talk(self):
        print('miaomiaom!!!')

d = Dog('mark')
c = Cat('kevin')

# d.talk()
# c.talk()
Animal.animal_talk(d)  #直接用Animal来调用接口,避免使用d,c来调用接口,达到多态的目的,
Animal.animal_talk(c)
View Code

wangwangwang!!!
miaomiaom!!!

 

posted @ 2017-04-25 17:43  寻_找北极星  阅读(114)  评论(0)    收藏  举报