温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python继续找对象的示例分析

发布时间:2022-01-17 09:08:44 来源:亿速云 阅读:146 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关python继续找对象的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

    面向对象三大特征:封装、继承、多态

    python继续找对象的示例分析

    1、封装(提高程序的安全性)

    class Car:
        def __init__(self,brand):
            self.brand=brand
        def start(self):
            print('自行车已被蹬跑')
    car=Car('自行车')
    car.start()
    print(car.brand)

    运行结果

    自行车已被蹬跑
    自行车

    python继续找对象的示例分析

    一开始它报错说没有定义name,我找老大一会不知道哪错了,原来是第六行

    self.name,那个时候写成,了。

    看在stu里边有哪些方法?就这样写

    python继续找对象的示例分析

    在类的外部可以通过_Student(类名)_ _age(不希望被访问的)进行访问

    class Student:
        def __init__(self,name,age):
            self.name=name
            self.__age=age #年龄不希望在类的外部使用,所以加了两个_
        def show(self):
            print(self.name,self.__age)
    stu=Student('张三',20)
    stu.show()
    #在类的外部使用name和age
    print(stu.name)
    print(dir(stu))
    print(stu._Student__age)
    张三 20
    张三
    ['_Student__age', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'show']
    20

    2、继承(提高代码的复用性)

    python继续找对象的示例分析

    python继续找对象的示例分析

    python继续找对象的示例分析

    class Person(object):
        def __init__(self,name,age):
            self.name=name
            self.age=age
        def info(self):
            print(self.name,self.age)
    class Student(Person):
        def __init__(self,name,age,stu_no):
            super().__init__(name,age)
            self.stu_no=stu_no
    class Teacher(Person):
        def __init__(self,name,age,teacherofyear):
            super(Teacher, self).__init__(name,age)
            self.teacherofyear=teacherofyear
    stu=Student('张三',20,'1001')
    teacher=Teacher('李四',34,10)
    stu.info()
    teacher.info()

    张三 20
    李四 34

    3、方法重写

    python继续找对象的示例分析

    此时只能输出学号,不满足需求

    class Person(object):
        def __init__(self,name,age):
            self.name=name
            self.age=age
        def info(self):
            print(self.name,self.age)
    class Student(Person):
        def __init__(self,name,age,stu_no):
            super().__init__(name,age)
            self.stu_no=stu_no
        def info(self):
            print(self.stu_no)
    class Teacher(Person):
        def __init__(self,name,age,teacherofyear):
            super(Teacher, self).__init__(name,age)
            self.teacherofyear=teacherofyear
    stu=Student('张三',20,'1001')
    teacher=Teacher('李四',34,10)
    stu.info()
    teacher.info()

    1001
    李四 34

    看下边的重载

    class Person(object):
        def __init__(self,name,age):
            self.name=name
            self.age=age
        def info(self):
            print(self.name,self.age)
    class Student(Person):
        def __init__(self,name,age,stu_no):
            super().__init__(name,age)
            self.stu_no=stu_no
        def info(self):
            super(Student, self).info()
            print(self.stu_no)
    class Teacher(Person):
        def __init__(self,name,age,teacherofyear):
            super(Teacher, self).__init__(name,age)
            self.teacherofyear=teacherofyear
    stu=Student('张三',20,'1001')
    teacher=Teacher('李四',34,10)
    stu.info()
    print('----------------------------')
    teacher.info()

    运行结果

    张三 20
    1001
    ----------------------------
    李四 34

    把教龄输出

    class Person(object):
        def __init__(self,name,age):
            self.name=name
            self.age=age
        def info(self):
            print(self.name,self.age)
    class Student(Person):
        def __init__(self,name,age,stu_no):
            super().__init__(name,age)
            self.stu_no=stu_no
        def info(self):
            super(Student, self).info()
            print(self.stu_no)
    class Teacher(Person):
        def __init__(self,name,age,teachofyear):
            super().__init__(name,age)
            self.teachofyear=teachofyear
        def info(self):
            super().info()
            print('教龄',self.teachofyear)
    stu=Student('张三',20,'1001')
    teacher=Teacher('李四',34,10)
    stu.info()
    print('----------------------------')
    teacher.info()

    运行结果

    张三 20
    1001
    ----------------------------
    李四 34
    教龄 10   

    4、object类

    python继续找对象的示例分析

    5、多态(提高程序的可拓展性和可维护性)

    python继续找对象的示例分析

    python继续找对象的示例分析

    Java就是静态语言,python就是动态语言

    6、特殊方法和特殊属性 特殊方法

    python继续找对象的示例分析

    两个特殊的方法----创建

    1初始化init

    2new

    特殊属性

    两个下划线开始,两个下划线结束就是特殊属性

    python继续找对象的示例分析

    绑定两个属性

    class A:
        pass
    class B:
        pass
    class C(A,B):
        def __init__(self,name,age):
            self.name=name
            self.age=age
    #创建C类的对象
    x=C('Jack',20)#x是C类的一个实例对象
    print(x.__dict__)
    {'name': 'Jack', 'age': 20}

    pycharm使用的小发现

    点击加号那里,就会释放,点击减号就会缩成这样,这说明了被缩起来的内容都是隶属于这个类的。

    python继续找对象的示例分析

    看最左侧出现了箭头,他的意思是重写person类中的方法

    python继续找对象的示例分析

    英文

    python继续找对象的示例分析

    关于“python继续找对象的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI