1. 程式人生 > >初識面向對象-封裝、property、class_static、反射(四)

初識面向對象-封裝、property、class_static、反射(四)

rop lex 行為 bsp 密碼 The 開發規範 setter per

封裝

# class Room:
# def __init__(self,name,length,width):
# self.__name = name
# self.__length = length
# self.__width = width
# def get_name(self):
# return self.__name
# def set_name(self,newName):
# if type(newName) is str and newName.isdigit() == False:
# self.__name = newName
# else:
# print(‘不合法的姓名‘)
# def area(self):
# return self.__length * self.__width
#
# jin = Room(‘金老板‘,2,1)
# print(jin.area())
# jin.set_name(‘2‘)
# print(jin.get_name())

# 假設父類的私有屬性 能被 子類調用麽
# class Foo:
# __key = ‘123‘ # _Foo__key
#
# class Son(Foo):
# print(Foo.__key) # _Son__key


# 會用到私有的這個概念de場景
#1.隱藏起一個屬性 不想讓類的外部調用
#2.我想保護這個屬性,不想讓屬性隨意被改變
#3.我想保護這個屬性,不被子類繼承

property

# property
# 內置裝飾器函數 只在面向對象中使用
from math import pi
class Circle:
def __init__(self,r):
self.r = r
@property
def perimeter(self):
return 2*pi*self.r
@property
def area(self):
return self.r**2*pi

# c1 = Circle(5)
# print(c1.area) # 圓的面積
# print(c1.perimeter) # 圓的周長

# class Person:
# def __init__(self,name,high,weight):
# self.name = name
# self.high = high
# self.weight = weight
# @property
# def bmi(self):
# return self.weight / self.high**2

# jin = Person(‘金老板‘,1.6,90)
# jin.bmi = 18
# classmethod
# staticmethod

# class Person:
# def __init__(self,name):
# self.__name = name
# @property
# def name(self):
# return self.__name + ‘sb‘
# @name.setter
# def name(self,new_name):
# self.__name = new_name
#
# tiger = Person(‘泰哥‘)
# print(tiger.name)
# tiger.name = ‘全班‘
# print(tiger.name)

# class Goods:
# discount = 0.8
# def __init__(self,name,price):
# self.name = name
# self.__price = price
# @property
# def price(self):
# return self.__price * Goods.discount
# apple = Goods(‘蘋果‘,5)
# print(apple.price)

# 屬性 查看 修改 刪除
# class Person:
# def __init__(self,name):
# self.__name = name
# self.price = 20
# @property
# def name(self):
# return self.__name
# @name.deleter
# def name(self):
# del self.__name
# @name.setter
# def name(self,new_name):
# self.__name = new_name
# brother2 = Person(‘二哥‘)
# del Person.price
# brother2.name = ‘newName‘
# brother2
# del brother2.name
# print(brother2.name)

class_static

# method 方法
# staticmathod 靜態的方法 ***
# classmethod 類方法 ****
# 類的操作行為
# class Goods:
# __discount = 0.8
# def __init__(self,name,price):
# self.name = name
# self.__price = price
# @property
# def price(self):
# return self.__price * Goods.__discount
# @classmethod # 把一個方法 變成一個類中的方法,這個方法就直接可以被類調用,不需要依托任何對象
# def change_discount(cls,new_discount): # 修改折扣
# cls.__discount = new_discount
# apple = Goods(‘蘋果‘,5)
# print(apple.price)
# Goods.change_discount(0.5) # Goods.change_discount(Goods)
# print(apple.price)
# 當這個方法的操作只涉及靜態屬性的時候 就應該使用classmethod來裝飾這個方法

# java
class Login:
def __init__(self,name,password):
self.name = name
self.pwd = password
def login(self):pass

@staticmethod
def get_usr_pwd(): # 靜態方法
usr = input(‘用戶名 :‘)
pwd = input(‘密碼 :‘)
Login(usr,pwd)

Login.get_usr_pwd()
# 在完全面向對象的程序中,
# 如果一個函數 既和對象沒有關系 也和類沒有關系 那麽就用staticmethod將這個函數變成一個靜態方法

# 類方法和靜態方法 都是類調用的
# 對象可以調用類方法和靜態方法麽? 可以 一般情況下 推薦用類名調用
# 類方法 有一個默認參數 cls 代表這個類 cls
# 靜態方法 沒有默認的參數 就象函數一樣


# 面向對象的進階
# 網絡編程

反射

#反射 *****
# name = ‘alex‘
# ‘name‘

class Teacher:
dic = {‘查看學生信息‘:‘show_student‘,‘查看講師信息‘:‘show_teacher‘}
def show_student(self):
print(‘show_student‘)

def show_teacher(self):
print(‘show_teacher‘)

@classmethod
def func(cls):
print(‘hahaha‘)
alex = Teacher()
for k in Teacher.dic:
print(k)
key = input(‘輸入需求 :‘)
# print(Teacher.dic[key])
if hasattr(alex,Teacher.dic[key]):
func = getattr(alex,Teacher.dic[key])
func()
# # alex.show_student() ‘show_student‘
# func = getattr(alex,‘show_student‘)
# func()

# hasattr getattr delattr
# if hasattr(Teacher,‘dic‘):
# ret = getattr(Teacher,‘dic‘) # Teacher.dic # 類也是對象
# # ret2 = getattr(Teacher,‘func‘) # 類.方法 teacher.func
# # ret2()
# print(ret)


# menu = Teacher.dic
# for k in menu:
# print(k)

# 通過反射
# 對象名 獲取對象屬性 和 普通方法
# 類名 獲取靜態屬性 和類方法 和 靜態方法

# 普通方法 self
# 靜態方法 @staticmethod
# 類方法 @classmethod
# 屬性方法 @property

# 繼承
# 封裝的


# 學員管理系統 : 開發規範
# 整理面向對象的所有知識點
# 時間計劃表 :周末幹什麽 中等偏下的 每周來一天自習

初識面向對象-封裝、property、class_static、反射(四)