Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!
Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount
คลาสและออบเจกต์
วัตถุ (Objects) คือการรวมกันของตัวแปรและฟังก์ชันไว้ในหน่วยเดียวกัน วัตถุจะได้รับตัวแปรและฟังก์ชันจากคลาส (Classes) ซึ่งคลาสนั้นเป็นแม่แบบขั้นพื้นฐานในการสร้างวัตถุของคุณ
คลาสขั้นพื้นฐานที่สุดจะมีลักษณะดังนี้:
เราจะอธิบายว่าทำไมถึงต้องใส่ "self" เป็นพารามิเตอร์ในภายหลัง แต่ก่อนอื่น การมอบคลาส (แม่แบบ) ด้านบนให้กับวัตถุ คุณจะทำดังนี้:
ตอนนี้ตัวแปร "myobjectx" เก็บวัตถุของคลาส "MyClass" ที่มีตัวแปรและฟังก์ชันที่ได้กำหนดไว้ในคลาสที่เรียกว่า "MyClass"
การเข้าถึงตัวแปรของวัตถุ
เพื่อเข้าถึงตัวแปรภายในวัตถุที่สร้างใหม่ "myobjectx" คุณจะทำดังนี้:
ดังเช่นตัวอย่างด้านล่างจะส่งออกสตริง "blah":
คุณสามารถสร้างวัตถุหลายวัตถุที่อยู่ในคลาสเดียวกัน (มีตัวแปรและฟังก์ชันเดียวกันที่ได้กำหนดไว้) อย่างไรก็ตาม วัตถุแต่ละตัวจะมีสำเนาของตัวแปรที่ได้กำหนดไว้ในคลาสแยกกัน ตัวอย่างเช่น หากเราจะกำหนดวัตถุอื่นโดยใช้คลาส "MyClass" และเปลี่ยนสตริงในตัวแปรดังกล่าว:
การเข้าถึงฟังก์ชันของวัตถุ
เพื่อเข้าถึงฟังก์ชันภายในวัตถุ คุณใช้สัญลักษณ์คล้ายกับการเข้าถึงตัวแปร:
ด้านบนจะพิมพ์ข้อความ "This is a message inside the class."
init()
ฟังก์ชัน __init__()
เป็นฟังก์ชันพิเศษที่ถูกเรียกเมื่อกำลังเริ่มต้นคลาส
มันถูกใช้สำหรับกำหนดค่าในคลาส
Exercise
เรามีคลาสที่กำหนดสำหรับยานพาหนะ สร้างยานพาหนะใหม่สองคันชื่อ car1 และ car2 ตั้งค่า car1 ให้เป็นรถเปิดประทุนสีแดงมูลค่า $60,000.00 ชื่อ Fer และ car2 ให้เป็นรถตู้สีน้ำเงินชื่อ Jump มูลค่า $10,000.00
# define the Vehicle class
class Vehicle:
name = ""
kind = "car"
color = ""
value = 100.00
def description(self):
desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
return desc_str
# your code goes here
# test code
print(car1.description())
print(car2.description())
# define the Vehicle class
class Vehicle:
name = ""
kind = "car"
color = ""
value = 100.00
def description(self):
desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
return desc_str
# your code goes here
car1 = Vehicle()
car1.name = "Fer"
car1.color = "red"
car1.kind = "convertible"
car1.value = 60000.00
car2 = Vehicle()
car2.name = "Jump"
car2.color = "blue"
car2.kind = "van"
car2.value = 10000.00
# test code
print(car1.description())
print(car2.description())
#test_output_contains('Fer is a red convertible worth $60000.00.')
#test_output_contains('Jump is a blue van worth $10000.00.')
success_msg("Great job!")
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!