-----学习视频来源于马士兵教育-----
内容主要为了自己复习用,也可以让大家梳洗思路
##类的创建 #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