类的学习笔记python

You know what they say,"Never go grocery shopping when you're hungry",you grap the wrong things,and the same as a life,"Never go into a relationship when you feel lonely,you might grab the wrong thing."Think about it. Because when you are desperate,you grab anything you want,Not what you need. I want you to get only what you need,and that is the best.


# 开发时间:2023/6/8  17:31
class Student:  # Student为类的名称,由一个或多个单词组成,每个单词首字母大写,其余小写
    native_place = '上海'  # 直接写在类里的变量,称为类属性

    # 类属性:类中方法外的变量称为类属性,被该类的所有对象共享
    # 初始化方法
    def __init__(self, name, age):  # name,age为实例属性
        self.name = name  # self.name称为实例属性,进行了一个赋值操作,将局部变量name的值赋给了实例属性
        self.age = age

    # (在类内定义的称为方法 ,在类外定义的称为函数)
    # 实例方法
    def info(self):
        print('名字叫:', self.name, '年龄是:', self.age)

    # 类方法
    @classmethod  # 使用类名直接访问的方法
    def cm(cls):
        print('classmethod 类方法')

    # 静态方法
    @staticmethod  # 使用类名直接访问的方法
    def sm():
        print('staticmethod 静态方法')


print(id(Student))  # 2486548962640
print(type(Student))  # <class 'type'>
print(Student)  # <class '__main__.Student'>

# 对象的创建

# 实例名=类名()

# 创建Student类的对象实例
stu = Student('jack', 20)
print('==========stu==============')
print(id(stu))  #
print(type(stu))  #
print(stu)  #
print('===========1=============')
print(stu.name)  #
print(stu.age)  #
print(stu.native_place)  #
print(stu.info())  # 对象名.方法名
print(Student.info(stu))  # 类名.方法名(对象名)
print('===========2============')
print(stu.cm())  # classmethod 类方法
print(Student.cm())
print('===========3=============')
print(stu.sm())  # staticmethod 静态方法
print(Student.sm())
print('===========4=============')
print(Student.native_place)
stu1 = Student('张三', 20)
stu2 = Student('李四', 30)
print(stu1.native_place)
print(stu2.native_place)
Student.native_place = '天津'
print(stu1.native_place)
print(stu2.native_place)

print('===========AAAAAAAAAAAAAAAAAAAAAA=============')


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

    def eat(self):
        print(self.name + '在吃饭')


def show():
    print('我是show函数')


pro1 = Programer('tom', 20)
pro2 = Programer('jack', 40)
print(id(pro1))
print(id(pro2))
print('========为pro2动态绑定性别属性=========')
pro2.gender = '女'
pro1.show = show  # 动态绑定方法
print(pro1.name, pro1.age, pro1.show())
print(pro2.name, pro2.age, pro2.gender)

面向对象的三大特征:
封装:提高程序的安全性
继承:提高代码的复用性
多态:提高程序的可扩展性和可维护性

将数据(属性)和行为(方法)包装到类对象中。在方法内部对属性进行操作,在
类对象的外部调用方法。这样无需关心方法内部的具体实现细节,从而隔离了复杂度

在Python中没有专门的修饰符用于属性私有,如果改属性不希望在类对象访问,前边使用两个" __ "


# 开发时间:2023/6/8  21:40
'''
面向对象的三大特征:
封装:提高程序的安全性
继承:提高代码的复用性
多态:提高程序的可扩展性和可维护性

将数据(属性)和行为(方法)包装到类对象中。在方法内部对属性进行操作,在
类对象的外部调用方法。这样无需关心方法内部的具体实现细节,从而隔离了复杂度

在Python中没有专门的修饰符用于属性私有,如果改属性不希望在类对象访问,前边使用两个" __ "
'''


class Car:
    def __init__(self, brand, price):
        self.brand = brand
        self.__price = price  # 不希望在类的外部被使用,所以加两个" __ "

    def start(self):
        print('汽车已启动...')

    def show(self):
        print('Car show', self.brand, self.__price)  # 加两个" __ " 不希望在类的外部被使用,但是可以在类内部使用


car = Car('宝马X7', '500W')
car.start()
print(car.brand)
car.show()
# print(car.price)#加两个" __ " 不希望在类的外部被使用  AttributeError: 'Car' object has no attribute 'price'
print(dir(car))  # 找到这个对象的所有属性
print(car._Car__price)  # 加两个" __ " 也可以访问,但是不建议

'''
类的继承
任何一个类默认继承object类
'''


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

    def info(self):
        print('姓名:{0},年龄:{1}'.format(self.name, self.age))
        # 姓名:tom,年龄:20


# 定义子类 Student
class Student(Person):
    def __init__(self, name, age, score):
        super().__init__(name, age)
        self.score = score


# 定义子类 Teacher
class Teacher(Person):
    def __init__(self, name, age, teacher_year):
        super().__init__(name, age)
        self.teacher_year = teacher_year


# 多继承
class Professor(Teacher, Car):
    def __init__(self, name, age, teacher_year, brand, price, new_bee):
        Teacher.__init__(self, name, age, teacher_year)
        Car.__init__(self, brand, price)
        self.new_bee = new_bee

    def speak(self):
        print('Professor:', self.name, 'have worked', self.teacher_year, 'years he has a car is', self.brand)

    def show(self):
        super().show()  # 调用父类被重写的方法
        print('Professor show', self.name, self.age, self.brand)  # 子类重写父类的方法


# 测试
stu = Student('tom', 20, '100')
teacher = Teacher('jack', 40, 10)
stu.info()
teacher.info()
pro1 = Professor('tony', 50, 16, '宝马X7', '500W', 'yes')
pro1.speak()
pro1.show()


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

    def __str__(self):
        return '我的名字是{0},今年{1}岁了'.format(self.name,self.age)


ha = Haha('jony',20)
print(dir(ha))
print(ha)  # 默认会调用__str__()方法 我的名字是jony,今年20岁了
print(type(ha))#<class '__main__.Haha'>
print(ha.__str__())  # 我的名字是jony,今年20岁了
print(ha is ha.__str__())  # False

'''
['__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__']

Object有一个__str__()方法, 用于返回对于“对象的描述”,对应与内置函数str()用于print()方法
帮我们查看对象的信息,所以我们经常会对__str__()进行重写
'''

(49条消息) Python多继承与super使用详解_python 多继承_涤生大数据的博客-CSDN博客

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aFakeProgramer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值