先對各類被匯入檔案進行類的分配
class
restaurant()
:def
__init__
(self, restaurant_name, cuisine_type)
:"""初始化屬性"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
defdescribe_restaurant
(self)
:"""列印名稱和型別"""
print
("the restaurant's name is "
+ self.restaurant_name.title()+
" which specializes in "
+ self.cuisine_type +
".")
defopen_restaurant
(self)
:"""列印開業訊息"""
print
("the restaurant is opening!"
)
class
user()
:def
__init__
(self,first_name,last_name,age,location)
:"""設定初始屬性"""
self.first_name = first_name
self.last_name = last_name
self.age = age
self.location = location
defdescribe_user
(self)
:"""列印使用者資訊摘要"""
print
("\nfirst name: "
+ self.first_name.title())
print
("last name: "
+ self.last_name.title())
print
("age: "
+str
(self.age)
)print
("location: "
+ self.location)
class
admin
(user)
:def
__init__
(self,first_name,last_name,age,location)
:"""初始化父類屬性"""
super()
.__init__(first_name,last_name,age,location)
self.privileges =
['can add post'
,'can delete post'
,'can ban user'
]def
show_privileges
(self)
:for privileges in self.privileges:
print
(self.first_name.title()+
" "+ self.last_name.title()+
" "+ privileges +
".")
class
privileges()
:def
__init__
(self,privileges =
['can add post'
,'can delete post'
,'can ban user'])
:"""初始化屬性"""
self.privileges = privileges
defshow_privileges
(self)
:for privileges in self.privileges:
print
(privileges)
#coding:gbk
class
user()
:def
__init__
(self,first_name,last_name,age,location)
:"""設定初始屬性"""
self.first_name = first_name
self.last_name = last_name
self.age = age
self.location = location
defdescribe_user
(self)
:"""列印使用者資訊摘要"""
print
("\nfirst name: "
+ self.first_name.title())
print
("last name: "
+ self.last_name.title())
print
("age: "
+str
(self.age)
)print
("location: "
+ self.location)
from user import user
class
admin
(user)
:def
__init__
(self,first_name,last_name,age,location)
:"""初始化父類屬性"""
super()
.__init__(first_name,last_name,age,location)
self.privileges =
['can add post'
,'can delete post'
,'can ban user'
]def
show_privileges
(self)
:for privileges in self.privileges:
print
(self.first_name.title()+
" "+ self.last_name.title()+
" "+ privileges +
".")
class
privileges()
:def
__init__
(self,privileges =
['can add post'
,'can delete post'
,'can ban user'])
:"""初始化屬性"""
self.privileges = privileges
defshow_privileges
(self)
:for privileges in self.privileges:
print
(privileges)
#coding:gbk
#9-10匯入restaurant類
from restaurant import restaurant
new_restaurant = restaurant(
'seine'
,'french cuisine'
)new_restaurant.describe_restaurant(
)#9-11admin類
from admin import user,admin,privileges
case = admin(
'john'
,'su',20
,'amoy'
)case.show_privileges(
)#9-12多個模組
from user import user
from admin_privileges import admin,privileges
case = admin(
'john'
,'su',20
,'amoy'
)case.show_privileges(
)
Python 程式設計 從入門到實踐
1.官網安裝 3.環境配置 務必選中核取方塊add python to path 4.檢視 啟動python版本的命令 python 執行 print hello python world 5.終端執行x.py檔案 python x.py 7.檢視當前目錄中的所有檔案的命令 dir windows系...
python程式設計 從入門到實踐第3章
第三章 列表簡介 1.列表一般用 表示。2.索引從0而不是1開始。通過將索引指定為 1 可讓python返回最後乙個列表元素。4.可使用方法insert 向列表中插入新元素,insert 索引,元素 5.使用del語句根據索引刪除元素 6.方法pop 可刪除列表末尾的元素,並能再使用先前的列表 7....
python程式設計 從入門到實踐 第4章
第四章 操作列表 1.函式range 生成一系列的數字。2.可使用函式list 將range 的結果直接轉換為列表。如果將range 作為list 的引數,輸出將為乙個數字列表。例 numbers list range 1,6 3.列表解析將for迴圈和建立新元素的 合併成一行,並自動新增新元素。例...