ทำความรู้จักกับภาษา python (43) : คุณสมบัติของการสร้าง Class (คลาส)
Moderator: mindphp, ผู้ดูแลกระดาน
-
- PHP Super Hero Member
- โพสต์: 604
- ลงทะเบียนเมื่อ: 07/12/2016 10:55 am
ทำความรู้จักกับภาษา python (43) : คุณสมบัติของการสร้าง Class (คลาส)
คุณสมบัติของการสร้าง Class (คลาส)
ในการสร้างคลาสแต่ละครั้ง python (ไพทอน) จะทำการสร้าง Attributes (แอททริบิวต์) ของ Class (คลาส) ให้อัตโนมัติ เพื่อบอกคุณสมบัติของ class โดยสามารถเรียกดูได้ด้วยฟังก์ชัน Built-in (บิวต์-อิน)
ฟังก์ชัน Built-in ของ class
__dict__ เป็น Dictionary (ดิกชันนารี) สำหรับเก็บ Namespace (เนมสเปช) ของ class
__doc__ เป็น Desination String (เดซทิเนชั่น สตริง) ของ Class หรือคำอธิบายของ Class
__name__ เก็บชื่อ Class
__module__ เก็บ Module (โมดูล) ต่างๆ
__bases___ เก็บเป็นตัวแปรชนิด Tuple (ทูเปิล) ที่ใช้สำหรับเก็บ Base Class (เบซ คลาส)
ตัวอย่างวิธีการใช้ฟังก์ชัน dict (ดิก) ตัวอย่างวิธีการใช้ฟังก์ชัน doc (ด๊อก) จะนำคำอธิบายของคลาสมาแสดง ตัวอย่างวิธีการใช้ฟังก์ชัน name (เนม) จะเรียกดูชื่อของ class ตัวอย่างวิธีการใช้ฟังก์ชัน module (โมดูล) ที่เก็บชื่อ Module ตัวอย่างวิธีการใช้ฟังก์ชัน bases (เบซ) ศึกษาข้อมูลมาจาก https://www.youtube.com/watch?v=ryPb7kB ... lzdKrpxsMM
ในการสร้างคลาสแต่ละครั้ง python (ไพทอน) จะทำการสร้าง Attributes (แอททริบิวต์) ของ Class (คลาส) ให้อัตโนมัติ เพื่อบอกคุณสมบัติของ class โดยสามารถเรียกดูได้ด้วยฟังก์ชัน Built-in (บิวต์-อิน)
ฟังก์ชัน Built-in ของ class
__dict__ เป็น Dictionary (ดิกชันนารี) สำหรับเก็บ Namespace (เนมสเปช) ของ class
__doc__ เป็น Desination String (เดซทิเนชั่น สตริง) ของ Class หรือคำอธิบายของ Class
__name__ เก็บชื่อ Class
__module__ เก็บ Module (โมดูล) ต่างๆ
__bases___ เก็บเป็นตัวแปรชนิด Tuple (ทูเปิล) ที่ใช้สำหรับเก็บ Base Class (เบซ คลาส)
ตัวอย่างวิธีการใช้ฟังก์ชัน dict (ดิก) ตัวอย่างวิธีการใช้ฟังก์ชัน doc (ด๊อก) จะนำคำอธิบายของคลาสมาแสดง ตัวอย่างวิธีการใช้ฟังก์ชัน name (เนม) จะเรียกดูชื่อของ class ตัวอย่างวิธีการใช้ฟังก์ชัน module (โมดูล) ที่เก็บชื่อ Module ตัวอย่างวิธีการใช้ฟังก์ชัน bases (เบซ) ศึกษาข้อมูลมาจาก https://www.youtube.com/watch?v=ryPb7kB ... lzdKrpxsMM
-
- PHP Super Hero Member
- โพสต์: 604
- ลงทะเบียนเมื่อ: 07/12/2016 10:55 am
-
- PHP Super Hero Member
- โพสต์: 813
- ลงทะเบียนเมื่อ: 08/01/2018 9:55 am
Re: ทำความรู้จักกับภาษา python (43) : คุณสมบัติของการสร้าง Class (คลาส)
โค้ด: เลือกทั้งหมด
class employee:
count = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
employee.count += 1
def discount(self):
print("total %d" % employee.count)
def disemployee(self):
print("name:", self.name, "salary:", self.salary)
print(employee.__bases__)
print(employee.__name__)
print(employee.__dict__)
print(employee.__doc__)
print(employee.__module__)
I am slow walker, but I never walk back. (Abraham Lincoln)
- Jom07
- PHP Super Hero Member
- โพสต์: 514
- ลงทะเบียนเมื่อ: 08/01/2018 9:56 am
Re: ทำความรู้จักกับภาษา python (43) : คุณสมบัติของการสร้าง Class (คลาส)
โค้ด: เลือกทั้งหมด
def __init__(self, name, salary):
self.name = name
self.salary = salary
employee.count += 1
def discount(self):
print("total %d" % employee.count)
def disemployee(self):
print("name:", self.name, "salary:", self.salary)
print("ฟังก์ชัน ของ _dict_:", employee.__dict__)
ศึกษาข้อมูลจาก :https://www.youtube.com/watch?v=ryPb7kB ... lzdKrpxsMM
-
- PHP Hero Member
- โพสต์: 199
- ลงทะเบียนเมื่อ: 30/04/2018 9:44 am
Re: ทำความรู้จักกับภาษา python (43) : คุณสมบัติของการสร้าง Class (คลาส)
คุณสมบัติของการสร้าง Class (คลาส)
ผลการรันโค้ด
ศึกษาจาก : https://www.youtube.com/watch?v=ryPb7kB ... lzdKrpxsMM
โค้ด: เลือกทั้งหมด
class workker:
'Example class with 3 method'
empCount = 0 #Class Variable
def __init__(self,name,salary):
self.name = name
self.salary = salary
workker.empCount += 1
def displayCount(self):
print "Tottal Workker %d : " % workker.empCount
def displayWorkker(self):
print "Name : ",self.name, ", Salary : ",self.salary
print "Function Built-in of __dict__: \n",workker.__dict__
print "------------------------------------------------------"
print "Function Built-in of __doc__: \n",workker.__doc__
print "------------------------------------------------------"
print "Function Built-in of __name__: \n",workker.__name__
print "------------------------------------------------------"
print "Function Built-in of __module__: \n",workker.__module__
print "------------------------------------------------------"
print "Function Built-in of __bases__: \n",workker.__bases__
It’s never too late to start again.
-
- PHP Super Hero Member
- โพสต์: 561
- ลงทะเบียนเมื่อ: 02/07/2018 10:45 am
Re: ทำความรู้จักกับภาษา python (43) : คุณสมบัติของการสร้าง Class (คลาส)
ในการที่เราสร้าง class หลายๆ class ฟังก์ชั่น built in เหล่านี้เหมาะแก่การเรียกใช้มากครับ เพราะสามารถดูรายละเอียดต่างๆของ class
โค้ด: เลือกทั้งหมด
class Employee:
"ง่วงจัง"
#ตัวอย่างการสร้าง class โดยภายในมี 3 method
empCount=0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def displayCount(self):
print("Total Employee %d"%Employee.empCount)
def displayEmployee(self):
print("Name :", self.name, ",Salary",self.salary)
print("ฟังก์ชั่น built in ของ Dict:",Employee.__dict__) # dict เก็บ namespace ของ คลาส
print("ฟังก์ชั่น built in ของ Doc:",Employee.__doc__)
print("ฟังก์ชั่น built in ของ name:",Employee.__name__)
print("ฟังก์ชั่น built in ของ module:",Employee.__module__)
- MBMoo
- PHP VIP Members
- โพสต์: 25635
- ลงทะเบียนเมื่อ: 04/06/2020 10:05 am
Re: ทำความรู้จักกับภาษา python (43) : คุณสมบัติของการสร้าง Class (คลาส)
โค้ด: เลือกทั้งหมด
'oop __dict__ คุณสมบัติ'
'oop __doc__ คำอธิบายclass'
'oop __nane__ ดูชื่อclass'
'oop __module__ เก็บชื่อของ module'
'oop __bases__ เก็บ bases ของ class'
class Employee:
'นี่คือคำอธิบายเมื่อใช้ doc จะเห็นนะ'
empcount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empcount += 1
def disempcount(self):
print("Total %d" % Employee.empcount)
def disEmployee(self):
print("Name:", self.name, "salary:", self.salary)
print("ฟังก์ชั่น Built-in ของ dict : " , Employee.__dict__)
print("ฟังก์ชั่น Built-in ของ doc: " , Employee.__doc__)
print("ฟังก์ชั่น Built-in ของ name: " , Employee.__name__)
print("ฟังก์ชั่น Built-in ของ module: " , Employee.__module__)
print("ฟังก์ชั่น Built-in ของ bases: " , Employee.__bases__)
-
- Similar Topics
- ตอบกลับ
- แสดง
- โพสต์ล่าสุด
-
-
โพสต์ใหม่ ทำความรู้จักกับภาษา python (45) : การสืบทอด Class (คลาส)
โดย dawthana » 19/01/2017 12:29 pm » ใน Python Knowledge - 7 ตอบกลับ
- 2579 แสดง
-
โพสต์ล่าสุด โดย MBMoo
09/06/2020 11:18 am
-
-
- 0 ตอบกลับ
- 2714 แสดง
-
โพสต์ล่าสุด โดย ธวัชชัย แสนหาญ
04/01/2019 10:15 am
-
- 0 ตอบกลับ
- 886 แสดง
-
โพสต์ล่าสุด โดย tatiya
14/08/2018 7:16 pm
-
-
โพสต์ใหม่ การ return ค่า ออกมาใช้นอก class ใน python
โดย tatiya » 16/08/2018 4:36 pm » ใน Python Knowledge - 0 ตอบกลับ
- 1430 แสดง
-
โพสต์ล่าสุด โดย tatiya
16/08/2018 4:36 pm
-
-
-
โพสต์ใหม่ Python เบื้องต้น (3) เขียนการสืบทอด class เป็นการสืบทอดลักษณะจากพ่อแม่
โดย Aussadawut » 24/11/2018 6:19 pm » ใน Python Knowledge - 0 ตอบกลับ
- 1192 แสดง
-
โพสต์ล่าสุด โดย Aussadawut
24/11/2018 6:19 pm
-
-
-
โพสต์ใหม่ การเรียกใช้ ฟังก์ชั่น ใน class เดียวกัน พร้อมส่งค่า โดย python
โดย MBMoo » 13/06/2020 3:18 pm » ใน Python Knowledge - 0 ตอบกลับ
- 1535 แสดง
-
โพสต์ล่าสุด โดย MBMoo
13/06/2020 3:18 pm
-
ผู้ใช้งานขณะนี้
สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 5