實例
python10_class.py
'''
類class
'''
# 定義類
class Student(): #括號內爲空,默認繼承Object類
def __init__(self,name,city): #構造函數
self.name=name
self.city=city
print("My name is %s and come from %s" %(name,city))
def talk(self):
print("Hello world,"+self.name)
python11_module.py
'''
模塊module
'''
import time # 導入時間模塊
import random # 導入隨機數模塊
from time import sleep # 導入模塊中的方法
from python10_class import Student # 導入模塊中的類
# 導入時間模塊 顯示當前系統時間
now=time.ctime()
print(now)
# 導入隨機數模塊 顯示隨機整數
number=random.randint(1,10) #顯示[1,10]之間的整數
print(number)
# 休眠5秒
print(time.ctime())
sleep(5)
print(time.ctime())
#生成實例對象
stu1=Student("小明","Beijing")
stu1.talk()
stu2=Student("小王","Shanghai")
stu2.talk()