1. 程式人生 > >python_面向物件—程式碼練習

python_面向物件—程式碼練習

"""注意:程式碼切勿照搬,錯誤請留言指出"""

  1 import re
  2 
  3 '''
  4 class Person:
  5     name='xxx'
  6     age=20
  7 
  8 p=Person()  #p為例項物件
  9 print(p.name,p.age)
 10 p.name="yyy"
 11 p.gender='male'
 12 print(p.name,p.gender,p.age)
 13 print(Person.name,Person.age)
 14 #p(例項物件)對值得修改不影響Person類中的內容
 15 Person.age='30'
16 print(Person.age) 17 #class Person (類) 對值得修改將該影響Person類中的內容 18 ''' 19 20 #訪問許可權(類的屬性) 21 ''' 22 class Person: #Person中的name和age公有的 23 name = 'james' 24 age=20 25 #在python中規定在前面加兩個下劃線,就變為私有的 26 ''' 27 ''' 28 class Person: 29 __name = 'james' #私有的__name 30 age = 20
31 32 def show(self): #self >> 自己 33 print(self.__name,self.age) #自己訪問自己 正常返回 34 35 36 37 p=Person () 38 p.show() #需要呼叫除show()函式,否則會拒絕class中的show的執行 39 print(Person.__name) # 訪問類中的私有屬性 異常返回 40 print(p.age) 41 #出現私有的函式呼叫時,結果同樣如此!私有的函式,元素只能在class中被使用
42 #常用的方法就是通過呼叫公有的函式來執行函式內的私有屬性的呼叫 43 ''' 44 45 #案例:編寫個人資訊並建立物件訪問屬性 46 ''' 47 class Person: 48 name = 'xxx' 49 gender = 'x' 50 age = 0 51 52 p=Person() 53 print(p.name,p.gender,p.age) 54 print(Person.name,Person.gender,Person.age) 55 56 p.name = 'A' 57 p.gender = 'Male' 58 p.age = 20 59 60 Person.name = 'B' 61 Person.gender = 'Female' 62 Person.age = 21 63 64 print(p.name,p.gender,p.age) #p例項物件屬性 65 print(Person.name,Person.gender,Person.age) #Person類屬性 66 ''' 67 68 #例項方法 69 ''' 70 class Person: 71 age = 20 72 def show(self): 73 print (self.age) 74 75 p.Person() 76 #例項方法至少有一個引數,一般以名為(self)的變數作為該引數(名稱可自定義) 77 #例項方法通過例項物件呼叫,例如:p.show() 78 #如果使用類名稱呼叫,需要人為傳遞例項引數,例如:Person.show(p) 79 #例項方法被呼叫是要向他的第一個引數傳遞例項物件 80 ''' 81 ''' 82 class Person: 83 name = 'james' 84 age = 12 85 86 def getName(self): 87 return self.name 88 89 def getAge(self): 90 return self.age 91 92 p=Person() 93 print(p.getName(),p.getAge()) #例項屬性物件呼叫 94 print(Person.getAge(p)) #類名稱呼叫 95 ''' 96 ''' 97 def show(self,s): #引數傳遞 98 print(self,s) 99 100 p.show('hi') #'hi'值傳遞給s 101 Person.show(p,'hello') #hello 給 s''' 102 103 #類方法 104 ''' 105 class Person: 106 age = 20 107 @classmethod 108 def show(cls): 109 print(cls.age) 110 111 p=Person() 112 ''' 113 #類方法要使用 @classmethod 來修飾,而且第一個引數一般命名為cls(可另定名) 114 #類方法通常使用類的名稱呼叫,例如:Person.show() 115 #類方法也可例項方法呼叫,例如:p.show() 116 #類方法呼叫時會向它的第一個引數傳遞類的名稱 117 ''' 118 class Person: 119 __name = 'james' 120 __age = 12 121 122 @classmethod 123 def show(self): 124 print(self) 125 print(self.__name,self.__age) 126 127 Person.show() 128 p=Person() 129 p.show()''' 130 131 #靜態方法 132 ''' 133 定義: 134 class Person: 135 age = 20 136 @staticmethod 137 def show(): 138 print(Person.age) 139 140 p = Person() 141 142 #靜態變數函式通過@staticmethod修飾 143 #通常採用類的名稱或者例項來呼叫靜態方法,例如:Person.show(),p.show() 144 #在呼叫靜態函式時不會向函式傳遞任何引數 145 ''' 146 ''' 147 class Person: 148 __name = 'james' 149 __age = 12 150 151 @staticmethod 152 def show(): #靜態不帶引數 153 print(Person.__name) 154 155 p = Person() 156 Person.show() 157 p.show() 158 ''' 159 160 #類、例項、靜態方法 不同——Person 161 ''' 162 class Person: 163 name = 'xxx' 164 gender = 'x' 165 age = 0 166 #例項方法 167 def instanceShow(self): 168 print(self.name,self.gender,self.age) 169 170 @classmethod #類方法 171 def classShow(cls): 172 print(cls.name,cls.gender,cls.age) 173 174 @staticmethod #靜態方法 175 def staticShow(): 176 print(Person.name,Person.gender,Person.age) 177 178 179 p=Person() 180 p.instanceShow() #例項方法呼叫 181 Person.classShow() #類方法呼叫 182 Person.staticShow() #靜態方法呼叫 183 ''' 184 ''' 185 例項方法一般用例項物件呼叫 186 類方法和靜態方法一般用類名稱呼叫 187 ''' 188 189 #物件初始化 190 ''' 191 構造方法(函式)...>完成函式初始化 192 @ 構造方法__init__(self, ... )在生成物件時呼叫, 193 可以用來進行一些初始化操作,不需要顯示去呼叫,系統預設執行 194 如果使用者自己沒有定義構造方法,系統就自己執行預設的額構造方法 195 196 class Person: 197 def __init__(self,n,a): 198 self.name=n 199 aelf.age=a 200 201 p=Person('xxx',20) 202 print(p.name,p.age) 203 204 @ 析構方法__del__(self)在釋放物件時呼叫,可以在裡面進行一些釋放資源的操作 205 不需要顯示呼叫 206 207 class Person: 208 def __init__(self,n,a): 209 self.name = n 210 self.age = a 211 def __del__(self): 212 print('__del__',self.name,self.age) 213 214 p=Person('xxx',20) 215 print(p.name,p.age) 216 '''''' 217 class Person: 218 def __init__(self): 219 print("__init__",self) 220 def __del__(self): 221 print("__del__",self) 222 223 p=Person()''' 224 225 #__init__完成物件初始化 226 ''' 227 class Person: 228 def __init__(self,n,g,a): #n,g,a 傳參單位 229 self.name = n 230 self.gender = g 231 self.age = a 232 233 def show(self): 234 print(self.name,self.gender,self.age) 235 236 p=Person('james','male',20) #p物件確定後,p物件的初始化(n,a,m)就完成了 237 p.show() 238 ''' 239 #python規定:類中只能有一個建構函式 240 #...__init__中設定預設引數 241 ''' 242 class Person: 243 def __init__(self,n='',g='male',a=0): 244 self.name = n 245 self.gender = g 246 self.age = a 247 248 def show(self): 249 print(self.name,self.gender,self.age) 250 251 a = Person('james') 252 b = Person('james','fenmale') 253 c = Person('james','male',20) 254 a.show() #結果:james male 0 255 b.show() #結果:james fenmale 0 256 c.show() #結果:james male 20 257 # __init__引數中如果設定了預設值,得到新的內容傳參後則覆蓋原值''' 258 259 #案例:日期類 MyDate 260 ''' 261 class MyDate: 262 __months = [0,31,28,31,30,31,30,31,31,30,31,30,31] 263 def __init__(self,y,m,d): 264 if (y<0): 265 print("Error:無效年份") 266 if (m<1 or m>12): 267 print("Error:無效月份") 268 if (y%400 == 0 or y%4 == 0 and y%100!=0): 269 MyDate.__months[2] = 29 270 else: 271 MyDate.__months[2] = 28 272 273 if d<1 or d>MyDate.__months[m]: 274 print("Error:無效日期") 275 #日期,月份,年份均合法後,建立類的屬性(內容) 276 self.year = y 277 self.months = m 278 self.day = d 279 def show(self,end='\n'): 280 print("%d-%d-%d"%(self.year,self.months,self.day),end = end) 281 282 p = MyDate(2018,11,19) 283 p.show() 284 ''' 285 286 #類的繼承 287 #派生與繼承:Student:name gender age 288 ''' 289 class Person: 290 def __init__(self,name,gender,age): 291 self.name = name 292 self.gender = gender 293 self.age = age 294 295 def show(self,end='\n'): 296 print(self.name,self.gender,self.age) 297 298 class Student(Person): 299 def __init__(self,name,gender,age,major,dept): 300 Person.__init__(self,name,gender,age) 301 self.major = major 302 self.dept = dept 303 304 def show(self): 305 Person.show(self,'') 306 print(self.major,self.dept) 307 308 s = Student('james','male',20,'softwart','computer') 309 s.show() 310 ''' 311 ''' 312 結果: 313 james male 20 314 softwart computer 315 ''' 316 #繼承類建構函式: 317 ''' 318 從上面的Strdent類的定義可以看除派生類的建構函式除了要完成自己的新增加的 319 major,dept屬性的初始化外,還要呼叫基類的Person的建構函式,而且還要顯示呼叫 320 即: Person.__init__(self,neme,gender,age) 321 通過類名稱Person直接呼叫Person的__init__函式,並提供所要的4個函式 322 繼承類時不會自動呼叫基類的建構函式的,必須顯示呼叫 323 ''' 324 325 #屬性方法的繼承: 326 ''' 327 如果一個基類中有一個例項方法,在繼承類中也可以重新定義完全一樣的例項方法。 328 例如 Person有show方法,在Student中也有一樣的show方法,它們是不會混淆的 329 我們稱Student類的show重寫了Person的show 330 —當然,一個基類的例項方法也可以不被重寫,派生類會繼承這個基類的例項方法 331 -派生類也可以增加自己的新例項方法 332 ''' 333 ''' 334 class Person: 335 className = 'Person' #自己的例項屬性 336 def __init__(self,name,gender,age): 337 self.name = name 338 self.gender = gender 339 self.age = age 340 341 def show(self,end='\n'): 342 print(self.name,self.gender,self.age) 343 344 @classmethod #類方法修飾 345 def classClassName(cls): 346 print(cls.className) 347 348 @staticmethod #靜態方法修飾 349 def staticClassName(): 350 print(Person.className) 351 352 def display(self): 353 print('display') 354 355 class Student(Person): 356 #className = 'Student' 357 def __init__(self,name,gender,age,major,dept): 358 Person.__init__(self,name,gender,age) 359 self.major = major 360 self.dept = dept 361 362 def show(self): 363 Person.show(self,'') 364 print(self.major,self.dept) 365 366 s = Student('A','male',20,'software','computer') 367 s.show() 368 print(Student.className) #Student本身沒有,則顯示上一級(Person) 369 Student.classClassName() 370 Student.staticClassName() 371 372 s.display() #例項呼叫 373 ''' 374 #結果說明:Student繼承了Person的例項、靜態、類(屬性)方法 375 #結果: 376 ''' 377 A male 20 378 software computer 379 Person 380 Person 381 Person 382 display 383 ''' 384 385 #案例 :年月日 + 時分秒 386 387 class MyDate: 388 __months = [0,31,28,31,30,31,30,31,31,30,31,30,31] 389 def __init__(self,y,m,d): 390 if (y<0): 391 print("Error:無效年份") 392 if (m<1 or m>12): 393 print("Error:無效月份") 394 if (y%400 == 0 or y%4 == 0 and y%100!=0): 395 MyDate.__months[2] = 29 396 else: 397 MyDate.__months[2] = 28 398 399 if d<1 or d>MyDate.__months[m]: 400 print("Error:無效日期") 401 #日期,月份,年份均合法後,建立類的屬性(內容) 402 self.year = y 403 self.months = m 404 self.day = d 405 def show(self,end='\n'): 406 print("%d-%d-%d"%(self.year,self.months,self.day),end = end) 407 408 class MyDateTime(MyDate): 409 def __init__(self,y,mo,d,h,mi,s): 410 MyDate.__init__(self,y,mo,d) 411 if (h<0 or h>23 or mi<0 or mi>59 or s<0 or s>59): 412 print("Error:無效時間") 413 self.hour = h 414 self.minute = mi 415 self.second = s 416 417 def show(self): 418 MyDate.show(self,'\t') 419 print("%d:%d:%d"%(self.hour,self.minute,self.second)) 420 421 422 423 424 p = MyDateTime(2018,11,19,21,21,54) 425 p.show() 426 427 #結果: 2018-11-19 21:21:54