python笔记(十一):类class的创建、继承

-----学习视频来源于马士兵教育-----

内容主要为了自己复习用,也可以让大家梳洗思路

##类的创建
#Python中一切皆对象
class Student:  #Student类的名称自定义,首字母必须大写
    pass
print(id(Student))  #2292897707344
print(type(Student)) #print(type(Student))
print(Student)  #<class '__main__.Student'>

class Student:  #Student类的名称自定义,首字母必须大写
    native_pace='山东'  #直接写在类里的变量,称为类属性
    def __init__(self,name,age):   #初始化方法
        self.name=name
        self.age=age ##实体属性
    #实例方法
    def eat(self):
        print('eating')  ##类方法
    #静态方法
    @staticmethod
    def method():
        print('eating1')
    #类方法
    @classmethod
    def cm(cls):
        print('eating2')

stu1=Student('宋',20)
#stu1=method()
Student.eat(stu1)  #eating 实例方法使用
print(stu1.native_pace) #山东
Student.native_pace='陕西'
print(stu1.native_pace) #陕西
Student.method() #eating1 静态方法使用
Student.cm()   #eating2类方法使用


class Student:  #Student类的名称自定义,首字母必须大写
    def __init__(self,name,age):   #初始化方法
        self.name=name
        self.age=age
    def eat(self):
        print('eating')
Student1=Student('MARK',18)
Student2=Student('SARK',18)

Student1.gender='女'
print(Student1.name,Student1.age,Student1.gender) #MARK 18 女  增加类的对象

def show():
    print('函数')
Student1.show=show()  #函数

class Student4:  #Student类的名称自定义,首字母必须大写
    def eat(self):
        print('eating')

eat1=Student4()
Student4.eat(eat1) ##eating

class Information():
    def __init__(self,name,age):
        self.name=name
        self.__age=age #age年龄不希望被外部调用,加了两个__
    def show(self):
        print(self.name,self.__age)
infor=Information('mark',18)
infor.show()  #mark 18
print(dir(infor))  #打印出内部实例名['_Information__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']
print(infor._Information__age) ##18

##继承
class Person():
    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,num):
        super().__init__(name,age)
        self.num=num
class Teacher(Person):
    def __init__(self,name,age,teacherage):
        super().__init__(name,age)
        self.teacherage=teacherage
stu=Student('mark',19,'101')
teacher=Teacher('SONG',20,40)

stu.info()  #mark 19
teacher.info()  #SONG 20

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

三颗草丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值