1. 程式人生 > 實用技巧 >面向物件基礎 相關內容-24

面向物件基礎 相關內容-24

day24

1.面向物件

# 1、如何基於面向物件的思想寫程式
# 例1
# 學生的資料
# stu_name = "egon"
# stu_age = 18
# stu_gender = "male"

# 學生的功能
# def choose(name, age, gender):
# print('%s:%s:%s 正在選課' % (name, age, gender))

# choose(stu_name,stu_age,stu_gender)

# 例2:基於物件式的思想進行改寫
# def choose(stu_self):
# print('%s:%s:%s 正在選課' % (stu_self["stu_name"], stu_self["stu_age"],stu_self["stu_gender"],))
#
# stu_obj = {
# "stu_name": "egon",
# "stu_age": 18,
# "stu_gender": "male",
# "choose":choose
# }

# print(stu_obj["stu_name"])
# stu_obj["choose"](stu_obj)

# python提供專門的語法來更漂亮地實現面向物件程式設計,什麼語法???

'''
學生物件1
資料:

名字 = "馮瘋子"
年齡 = 18
性別 = "female"


學生物件2
資料:

名字 = "郭靖"
年齡 = 19
性別 = "male"


學生物件3
資料:
名字 = "大雕"
年齡 = 200
性別 = "male"


學生的類
相同的資料
學校 = "oldboy"
相同的功能
選課


'''
# 類體程式碼會在類定義階段立刻執行,然後將產生的名字都丟到類的名稱空間中
class Student:
# 相同的資料
school = "oldboy"

# 相同的功能
def choose(self):
print("正在選課")


# print('====>')

stu_obj1=Student()
stu_obj2=Student()
stu_obj3=Student()

stu_obj1.name = "馮瘋子" # stu1_obj1.__dict__["name"] = "馮瘋子"
stu_obj1.age = 18
stu_obj1.gender = "female"

stu_obj2.name = "郭靖"
stu_obj2.age = 19
stu_obj2.gender = "male"

stu_obj3.name = "大雕"
stu_obj3.age = 200
stu_obj3.gender = "male"

# print(stu_obj1.name)
# stu_obj1.school = "xxx"
# print(stu_obj1.school)

# print(Student.__dict__)
print(stu_obj1.__dict__)
print(stu_obj2.__dict__)
print(stu_obj3.__dict__)

2.初始化方法

# =============================>例1
# class Student:
# school = "oldboy"
#
# def choose(self):
# print("正在選課")
#
# stu_obj1=Student()
# stu_obj2=Student()
# stu_obj3=Student()

# stu_obj1.name = "馮瘋子"
# stu_obj1.age = 18
# stu_obj1.gender = "female"
#
# stu_obj2.name = "郭靖"
# stu_obj2.age = 19
# stu_obj2.gender = "male"
#
# stu_obj3.name = "大雕"
# stu_obj3.age = 200
# stu_obj3.gender = "male"

# # ============================>例2:
# class Student:
# school = "oldboy"
#
# def choose(self):
# print("正在選課")
#
# stu_obj1 = Student()
# stu_obj2 = Student()
# stu_obj3 = Student()
#
# def init(obj, x, y, z):
# obj.name = x
# obj.age = y
# obj.gender = z
#
# init(stu_obj1, "馮瘋子", 18, "female")
# init(stu_obj2, "郭靖", 19, "male")
# init(stu_obj3, "大雕", 200, "male")

# ============================>例3:
class Student:
school = "oldboy"

# 空物件
def __init__(obj, x, y, z):
obj.name = x
obj.age = y
obj.gender = z
# return None # 只能返回None

def choose(self):
print("正在選課")

# 呼叫類:
# 1、建立一個空物件與類相關
# 2、把空物件、"馮瘋子", 18, "female"一起傳給__init__方法,完成物件的初始化
# 3、賦值符號把初始化好的物件的記憶體地址繫結變數名stu_obj1
stu_obj1 = Student("馮瘋子", 18, "female")
stu_obj2 = Student("郭靖", 19, "male")
stu_obj3 = Student("大雕", 200, "male")


print(Student.__dict__)
print(stu_obj1.__dict__)

3.屬性查詢

# 優先順序
# 先從物件的字典裡找,沒有,再去類的字典中找
class Student:
school = "oldboy"

def __init__(obj, x, y, z):
obj.name = x
obj.age = y
obj.gender = z

def choose(self):
print("%s 正在選課" %self.name)

stu_obj1 = Student("馮瘋子", 18, "female")
stu_obj2 = Student("郭靖", 19, "male")
stu_obj3 = Student("大雕", 200, "male")

# 1、類中定義的資料是直接共享給所有物件使用的
# print(id(stu_obj1.school))
# print(id(stu_obj2.school))
# print(id(stu_obj3.school))
# print(id(Student.school))

# Student.school="xxx"
# print(stu_obj1.school)
# print(stu_obj2.school)
# print(stu_obj3.school)

# print(Student.choose)
# print(stu_obj1.choose)
# print(stu_obj2.choose)
# print(stu_obj3.choose)

# 2、類中定義的函式是繫結給所有物件用的,繫結給誰就應該由哪個物件來呼叫
# 物件.繫結方法()會把物件當作第一個引數傳入
# 類.函式()就是一個函式的玩法,沒有自動傳參的效果

# Student.choose(123123123)
stu_obj3.choose()
stu_obj2.choose()
stu_obj1.choose()