# 開發(fā)人員: hanhan丶
# 開發(fā)時間: 2020/11/6 10:14
# class Student:? # Student為類的名稱(類名),由一個多個單詞組成玄柠,每個單詞的首字母大寫休涤,其余小寫
#? ? native_pace = '吉林'? ? # 直接寫在類里的變量咱圆,稱為類屬性
#? ? #? 初始化方法
#? ? def __init__(self,name,age):
#? ? ? ? self.name = name
#? ? ? ? self.age = age
#? ? #? 實例方法
#? ? def fun(self):? ? # 實例方法? 在類里定義的稱為方法,在類外定義的稱為函數(shù)
#? ? ? ? print('hello,實例方法')
#? ? #? 靜態(tài)方法
#? ? @staticmethod
#? ? def fun1():
#? ? ? ? print('hello,靜態(tài)方法')
#? ? #? 類方法
#? ? @classmethod
#? ? def fun2(cls):
#? ? ? ? print('hello,類方法')
#? 創(chuàng)建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,類方法? 不需要傳入實參
# 靜態(tài)方法的使用方式
# Student.fun1()? ? # hello,靜態(tài)方法
# 動態(tài)綁定屬性和方法
# 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('我是動態(tài)綁定的方法')
# stu.fun = fun
# stu.fun()? ? # 我是動態(tài)綁定的方法
# 面向對象中的封裝? 提高代碼的安全性? ? “__”的用法
# 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