面向对象编程3

day21 面向对象编程3

一、继承入门

面对对象的三大特征:继承,封装,多态

# 使用继承

class Animal:  # 它作为父类(基类)
    head = 1

    def run(self):
        print('走路')

    def eat(self):
        print('吃饭')


# Dog子类和Person子类都继承Animal父类
class Dog(Animal):
    def speak(self):
        print('汪汪叫')

class Person(Animal):
    def work(self):
        print('工作')


dog = Dog()
dog.eat()
print(dog.head)

二、多继承

多继承以后,属性重复了
查找顺序是:对象自己------》类----》父类(先后顺序)

class Animal:
    def run(self):
        print('走')


class Coder:
    def work(self):
        print('工作')


class Person(Animal, Coder):
    def eat(self):
        print('吃饭')
person=Person()
person.run()
person.work()

三、类的类型

1、新式类

如果一个对象继承了object,这个类就是新式类
Python3中,所有类默认都继承了object(不写,也会继承 object类),所以所有类都是新式类

2、经典类

没有继承任何父类的类,叫经典类,只有python2中才有
,需要手动继承object
新式类和经典类的属性查找顺序是不一样的

四、选课系统一部分

class Person:
    def __init__(self, name):
        self.name = name


class Student(Person):
    def __init__(self, linux_score, python_score, name):
        Person.__init__(self,  name)
        self.linux_score = linux_score
        self.python_score = python_score


class Teacher(Person):
    def change_score(self, obj, new_linux_score, new_python_score):
        obj.linux_score = new_linux_score
        obj.python_score = new_python_score


s1 = Student(80, 90, 'zhangsan')
t1 = Teacher('lqz')
t1.change_score(s1,90,90)
print(s1.linux_score)
print(s1.python_score)

五、属性查找

对象的属性是:指的是对象的属性和方法

class F1():
    def s1(self):
        print('f1---s1s1s1')

    def s2(self):
        self.s1()
        print('f1---s2s2s2')


class F2(F1):
    def s1(self):
        print('f2---s1s1s1')

    # def s2(self):
    #     print('f2---s2s2s2')
    #

class F3(F2):
    def s1(self):
        print('f3---s3s3s3')

    # def s2(self):
    #     print('f3---s2s2s2')


class F4(F3):
    def s1(self):
        print('f4---s1s1s1')

    # def s2(self):
    #     print('f4---s2s2s2')

f4=F4()
f4.s2()

多层继承关系的属性查找顺序,永远是是从最下层开始找起的(self.属性)(有的情况下ctral+鼠标右键是不准确的)

六、派生

1、定义

子类比父类扩展的属性和方法统称为派生
父类-----基类
子类-----派生类

class Person(): #父类
    def run(self):
        print('走路')
    def speak(self):
        print('说话')

class Teacher(Person): #派生类
    def teach(self): #派生方法
        print('教书')

2、派生类中如何使用父类的属性


class Person:  # 父类,基类
    school='oldboy'
    def run(self):
        print('run')
    def speak(self):
        print('speak')
        
#方式一:可能存在问题(子类中如果有speak方法,就使用不到父类的了)
class Teacher(Person):

    def speak(self):
        print('teacher 的 speak')
    def teach(self):
        # 需要调用父类的speak方法(如何调用)
        # self.speak()  # 不是常用的方式
        print('教课')

#方式二:指名道姓的使用父类中的方法
class Teacher(Person):

    def speak(self):
        print('teacher 的 speak')

    def teach(self):
        # 需要调用父类的speak方法(如何调用),指名道姓的使用
        Person.speak(self)
        print('教课')
        # 使用父类的属性
        print(Person.school)



# 方式三:通过super关键字

class Teacher(Person):

    def speak(self):
        print('teacher 的 speak')

    def teach(self):
        # 需要调用父类的speak方法(如何调用),指名道姓的使用
        # super()是一个特殊对象,代指父类对象
        # print(super())
        super().speak()  # 父类对象调用自己的绑定方法,会自动传值
        print(super().school)   # 调用父类的属性


teacher=Teacher()
teacher.teach()

七、通过super实现选课系统案例

class Person():
    def __init__(self, name, sex, age):
        self.name = name
        self.sex = sex
        self.age = age


class Student(Person):
    def __init__(self, name, sex, age):
        super().__init__(name, sex, age)
        self.course = []

    def choose_course(self, course):
        if course not in self.course:
            self.course.append(course)
            print('你已成功选择%s这门课'%(course.name))
        else:
            print('你已选过这门课')


class Teacher(Person):
    def __init__(self, name, sex, age, salary):
        super().__init__(name, sex, age)
        self.salary = salary


class Course():
    def __init__(self, name, semester, cost):
        self.name = name
        self.semester = semester
        self.cost = cost

    def p_info(self):
        print('学科:%s,时长:%s,学费:%s' % (self.name, self.semester, self.cost))


linux = Course('linux', '6个月', 16888)
python = Course('python', '6个月', 18888)

zhangsan = Student('zhangsan', 'male', 18)
zhangsan.choose_course(linux)
zhangsan.choose_course(linux)
zhangsan.choose_course(python)
for course in zhangsan.course:
    course.p_info()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值