温馨提示:本文最后更新于
2024-12-21 04:56:05
,某些文章具有时效性,若有错误或已失效,请在下方留言。自定义类
类定义的语法,如下
# 类的定义, ClassName必须首字母大写
class ClassName:
# 类的对象
class_name_1 = ClassName()
常见的几种命名方式
– PascalCase
– camelCase
– snake_case
构造器(初始化器)
Python 的初始化器为__init__
方法
class ClassName:
def __init__(self):
# 将属性进行初始化
类的属性
属性的初始化操作在构造器中进行
# 属性的定义
class ClassName:
def __init__(self, attribute_1, attribute_2, ...)
self.attribute_a = attribute1
self.attribute_b = attribute2
...
# 对象属性的使用
class_name_1 = ClassName(attribute_1, attribute_2, ...)
类的方法
类的方法定义,如下所示
class ClassName:
# 初始化器(构造器)
def __init__(self, attribute_1, attribute_2, ...)
self.attribute_a = attribute1
self.attribute_b = attribute2
...
# 方法定义
def function_a(self, class_name):
# 方法的内部实现
# 对象方法的使用
class_name_1 = ClassName(attribute_1, attribute_2, ...)
class_name_2 = ClassName(attribute_1, attribute_2, ...)
class_name_1.function_a(class_name_2)
Quiz 游戏
main.py
文件内容
from question_model import Question
from data import question_data
from quiz_brain import QuizBrain
question_bank = []
for question in question_data:
question_bank.append(Question(question["text"], question["answer"]))
quiz = QuizBrain(question_bank)
while quiz.still_has_question():
quiz.next_question()
print("You've completed the quiz.")
print(f"Your final score was: {quiz.score}/{quiz.question_number}")
quiz_brain.py
文件内容
class QuizBrain:
def __init__(self, q_list):
"""类初始化"""
self.question_number = 0
self.question_list = q_list
self.score = 0
def next_question(self):
"""下一个问题"""
current_question = self.question_list[self.question_number]
self.question_number += 1
user_answer = input(f"Q.{self.question_number}: {current_question.text} (True/False)?: ")
self.check_answer(user_answer, current_question.answer)
def still_has_question(self):
"""是否还有问题,有返回 True, 否则返回 False"""
return self.question_number < len(self.question_list)
def check_answer(self, user_answer, current_answer):
"""检测用户答案"""
if user_answer == current_answer:
self.score += 1
print("You got it right!")
else:
print("That's wrong.")
print(f"The correct answer was {current_answer}.")
print(f"Your current score was {self.score}/{self.question_number}")
question_model.py
文件内容
class Question:
def __init__(self, q_text, q_answer):
"""属性初始化"""
self.text = q_text
self.answer = q_answer
data.py
文件内容
question_data = [
{"text": "A slug's blood is green.", "answer": "True"},
{"text": "The loudest animal is the African Elephant.", "answer": "False"},
{"text": "Approximately one quarter of human bones are in the feet.", "answer": "True"},
{"text": "The total surface area of a human lungs is the size of a football pitch.", "answer": "True"},
{"text": "In West Virginia, USA, if you accidentally hit an animal with your car, you are free to take it home to eat.", "answer": "True"},
{"text": "In London, UK, if you happen to die in the House of Parliament, you are entitled to a state funeral.", "answer": "False"},
{"text": "It is illegal to pee in the Ocean in Portugal.", "answer": "True"},
{"text": "You can lead a cow down stairs but not up stairs.", "answer": "False"},
{"text": "Google was originally called 'Backrub'.", "answer": "True"},
{"text": "Buzz Aldrin's mother's maiden name was 'Moon'.", "answer": "True"},
{"text": "No piece of square dry paper can be folded in half more than 7 times.", "answer": "False"},
{"text": "A few ounces of chocolate can to kill a small dog.", "answer": "True"}
]
© 版权声明
THE END
暂无评论内容