# 开发人员: hanhan丶
# 开发时间: 2020/11/6 10:14
# class Student: # Student为类的名称(类名),由一个多个单词组成,每个单词的首字母大写,其余小写
# native_pace = '吉林' # 直接写在类里的变量,称为类属性
# # 初始化方法
# def __init__(self,name,age):
# self.name = name
# self.age = age
# # 实例方法
# def fun(self): # 实例方法 在类里定义的称为方法,在类外定义的称为函数
# print('hello,实例方法')
# # 静态方法
# @staticmethod
# def fun1():
# print('hello,静态方法')
# # 类方法
# @classmethod
# def fun2(cls):
# print('hello,类方法')
# 创建student类的对象
# stu = Student('徐晗',18) # stu就是实例对象
# print(stu)
# stu.fun() # 调用类对象中的方法 hello,实例方法
# print(stu.name) # 徐晗
# Student.fun(stu) # hello,实例方法 与上面23行代码功能相同,都是去调fun方法,不过这个要传实参
# 类属性的使用方式
# print(Student.native_pace) # 吉林
# stu = Student('张三',18)
# stu1 = Student('李四',50)
# print(stu.native_pace) # 吉林
# print(stu1.native_pace) # 吉林
# stu.native_pace = '珲春'
# stu1.native_pace = '珲春'
# print(stu.native_pace) # 珲春 打出来是更改后的值
# print(stu1.native_pace) # 珲春 打出来是更改后的值
# 类方法的使用方式
# Student.fun2() # hello,类方法 不需要传入实参
# 静态方法的使用方式
# Student.fun1() # hello,静态方法
# 动态绑定属性和方法
# class Student:
# def __init__(self, name, age):
# self.name = name
# self.age = age
#
# def eat(self):
# print(self.name, '在吃饭')
#
#
# stu = Student('张宇航', 28)
# Student.eat(stu) # 张宇航 在吃饭
# stu.gender = '女'
# # print(stu.gender)
# def fun():
# print('我是动态绑定的方法')
# stu.fun = fun
# stu.fun() # 我是动态绑定的方法
# 面向对象中的封装 提高代码的安全性 “__”的用法
# class Car:
# def __init__(self,brand,price):
# self.brand = brand
# self.__price = price # __price 不希望在class外部访问时,加__
# def strat(self):
# print(self.brand) # 开车
# print(self.__price) # 50000
# ve = Car('开车',50000)
# ve.strat()
# print(ve.brand) # 开车
# print(ve.__price) # AttributeError: 'Car' object has no attribute '__price' 在外部访问不到
# print(dir(ve)) # ve中的所有实例方法对象['_Car__price', '__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__', 'brand', 'strat']
# print(ve._Car__price) # 50000 这样就可以在外部访问到了 但是使用__的意思就是不想让你在外部访问,所以不推荐你使用这个去访问
# 面向对象中的继承 提高代码的复用性
# 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,grade):
# super().__init__(name,age)
# self.grade = grade
# class teacher(person):
# def __init__(self,name,age,teacherage):
# super().__init__(name,age)
# self.teacherage = teacherage
# stu = student('张宇航',23,'jquery')
# tea = teacher('徐晗',23,1)
# stu.info()
# tea.info()
#面向对象中的多继承 不能使用super()方法
# class a(object):
# def __init__(self,name):
# self.name = name
# def ainfo(self):
# print(self.name)
# class b(object):
# def __init__(self,age):
# self.age = age
# def binfo(self):
# print(self.age)
# class c(a,b):
# def __init__(self,name,age,gender):
# a.__init__(self,name)
# b.__init__(self,age)
# self.gender = gender
# def cinfo(self):
# print(self.gender)
# d = c('徐晗',20,'男')
# d.ainfo()
# d.binfo()
# d.cinfo()
# 面向对象中的方法重写
# 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,grade):
# super().__init__(name,age)
# self.grade = grade
# def info(self): # 方法重写
# super().info() # 调用父class中的info() 张宇航23
# print(self.grade) # jquery
# class teacher(person):
# def __init__(self,name,age,teacherage):
# super().__init__(name,age)
# self.teacherage = teacherage
# def info(self): # 方法重写
# super().info() # 徐晗23
# print(self.teacherage) # 1
# stu = student('张宇航',23,'jquery')
# tea = teacher('徐晗',23,1)
# stu.info()
# tea.info()
# 面向对象中的object类
# class student(object):
# def __init__(self,name,age):
# self.name = name
# self.age = age
# def __str__(self):
# return '我的名字是{0},年龄是{1}'.format(self.name,self.age)
# stu = student('xuhan',20)
# print(dir(stu)) # ['__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__', 'age', 'name']
# print(stu) # 默认调用__str__()这个方法 我的名字是xuhan,年龄是20