0% found this document useful (0 votes)
38 views13 pages

FC07 Object

The document discusses Python object design including defining classes, constructors, properties, class variables, printing and adding objects, object representation, and inheritance.

Uploaded by

CHENG YU LO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views13 pages

FC07 Object

The document discusses Python object design including defining classes, constructors, properties, class variables, printing and adding objects, object representation, and inheritance.

Uploaded by

CHENG YU LO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Python Object Design

物件設計

薛念林 , 逢甲資工
定義一個類別

class class_name():
...
定義類別的建構子

class class_name():
def __init()__(self, arg1, arg2, ...)
定義類別的物件屬性

class class_name():
def __init()__(self, arg1, arg2, ...):
self.attr1 = arg1
self.attr2 = arg2
定義私有物件屬性

class class_name():
def __init()__(self, arg1, arg2, ...):
self.__attr1 = arg1
self.attr2 = arg2
定義 property

class class_name():
def setXXX():

def getXXX():

property XXX = (getXXX, setXXX)
用 @ 裝飾品 定義 property

@property
def XXX(self):

@XXX.setter
def XXX(self, ...):

類別屬性

class class_name():

class_var = ...

def __init()__(self, arg1, arg2, ...):


self.__attr1 = arg1
self.attr2 = arg2
print 物件
class class_name():
def __str__(self):
# print 此物件時會呼叫
...

obj = class_name()
print (obj)
物件相加
class class_name():
def __add__(self):
...

> obj1 = class_name()


> obj1 = class_name()
>a+b
物件資料
class class_name():
def __repr__(self):
# 在命令列直接打物件時名稱時會呼叫
...

> obj = class_name()


> obj
繼承

class child_class(parent_class):
Thanks for your watching

You might also like