1. 程式人生 > >初識面向對象 -類和對象命名空間、組合用法(學習筆記二)

初識面向對象 -類和對象命名空間、組合用法(學習筆記二)

多態 數據 math att class 動態 ini 數據類型 rom

類和對象命名空間

# 類裏 可以定義兩種屬性
# 靜態屬性
# 動態屬性
class Course:
language = [‘Chinese‘]
def __init__(self,teacher,course_name,period,price):
self.teacher = teacher
self.name = course_name
self.period = period
self.price = price
def func(self):
pass

# Course.language = ‘English‘
# Course.__dict__[‘language‘] = ‘Chinese‘
# print(Course.language)

python = Course(‘egon‘,‘python‘,‘6 months‘,20000)
linux = Course(‘oldboy‘,‘linux‘,‘6 months‘,20000)
#[‘chinese‘]
python.language = ‘‘
# print(python.language)
# print(linux.language)
# Course.language = ‘Chinese‘
# print(python.language)
# print(linux.language)

# del python.language
# print(python.language)
# print(python.__dict__)

# print(Course.language)
# print(linux.language)
# print(linux.__dict__)
# 類中的靜態變量 可以被對象和類調用
# 對於不可變數據類型來說,類變量最好用類名操作
# 對於可變數據類型來說,對象名的修改是共享的,重新賦值是獨立的

# 模擬人生
# class Person:
# money = 0
# def work(self):
# Person.money += 1000
#
# mother = Person()
# father = Person()
# Person.money += 1000
# Person.money += 1000
# print(Person.money)

# mother.work()
# father.work()


# 創建一個類,每實例化一個對象就計數
# 最終所有的對象共享這個數據
# class Foo:
# count = 0
# def __init__(self):
# Foo.count += 1
#
# f1 = Foo()
# f2 = Foo()
# print(f1.count)
# print(f2.count)
# f3 = Foo()
# print(f1.count)


# 認識綁定方法
# def func():pass
# print(func)
#
# class Foo:
# def func(self):
# print(‘func‘)
# def fun1(self):
# pass
# f1 = Foo()
# print(Foo.func)
# print(f1.func)
# print(f1.fun1)
#<bound method Foo.func of f1>

# 包 —— __init__
# import package —— 類的實例化的過程
# import time
# time.time()


# 類裏的名字有 類變量(靜態屬性量)+ 方法名(動態屬性)
# 對象裏的名字 對象屬性
# 對象 —— > 類
# 對象找名字 : 先找自己的 找類的 再找不到就報錯
# 對象修改靜態屬性的值
# 對於不可變數據類型來說,類變量最好用類名操作
# 對於可變數據類型來說,對象名的修改是共享的,重新賦值是獨立的

組合的用法

# 面向對象的三大特性 : 繼承 多態 封裝
# 組合
# 人狗大戰
class Dog:
def __init__(self,name,aggr,hp,kind):
self.name = name
self.aggr = aggr
self.hp = hp
self.kind = kind

def bite(self,person):
person.hp -= self.aggr

class Person:
def __init__(self,name,aggr,hp,sex):
self.name = name
self.aggr = aggr
self.hp = hp
self.sex = sex
self.money = 0

def attack(self,dog):
dog.hp -= self.aggr

def get_weapon(self,weapon):
if self.money >= weapon.price:
self.money -= weapon.price
self.weapon = weapon
self.aggr += weapon.aggr
else:
print("余額不足,請先充值")

class Weapon:
def __init__(self,name,aggr,njd,price):
self.name = name
self.aggr = aggr
self.njd = njd
self.price = price

def hand18(self,person):
if self.njd > 0:
person.hp -= self.aggr * 2
self.njd -= 1

alex = Person(‘alex‘,0.5,100,‘不詳‘)
jin = Dog(‘金老板‘,100,500,‘teddy‘)
w = Weapon(‘打狗棒‘,100,3,998)
# alex裝備打狗棒
alex.money += 1000
alex.get_weapon(w)
print(alex.weapon)
print(alex.aggr)
alex.attack(jin)
print(jin.hp)
alex.weapon.hand18(jin)
print(jin.hp)

# 組合 :一個對象的屬性值是另外一個類的對象
# alex.weapon 是 Weapon類的對象

組合練習

# 圓形類
# 圓環類
from math import pi
class Circle:
def __init__(self,r):
self.r = r
def area(self):
return self.r**2 * pi
def perimeter(self):
return 2*pi*self.r

class Ring:
def __init__(self,outside_r,inside_r):
self.outside_c = Circle(outside_r)
self.inside_c = Circle(inside_r)
def area(self):
return self.outside_c.area() - self.inside_c.area()
def perimeter(self):
return self.outside_c.perimeter()+self.inside_c.perimeter()

# ring = Ring(20,10)
# print(ring.area())
# print(ring.perimeter())

# 創建一個老師類
# 老師有生日
# 生日也可以是一個類
# 組合
class Birthday:
def __init__(self,year,month,day):
self.year = year
self.month = month
self.day = day

class Course:
def __init__(self,course_name,period,price):
self.name = course_name
self.period = period
self.price = price

class Teacher:
def __init__(self,name,age,sex,birthday):
self.name = name
self.age = age
self.sex = sex
self.birthday =birthday
self.course = Course(‘python‘,‘6 month‘,2000)

b = Birthday(2018,1,16)
egg = Teacher(‘egon‘,0,‘女‘,b)
print(egg.name)
print(egg.birthday.year)
print(egg.birthday.month)
print(egg.course.price)

初識面向對象 -類和對象命名空間、組合用法(學習筆記二)