简答题(一)

抽象类为什么不能创建对象 1、什么是抽象类?抽象类往往用来表征对问题领域进行分析、设计中得出的抽象概念,是对一系列看上去不同,但是本质上相同的具体概念的抽象。通常在编程语句中用 abstract 修饰的类是抽象类。在C++中,含有纯虚拟函数的类称为抽象类,它不能生成对象;在java中,含有抽象方法的类称为抽象类,同样不能生成对象。抽象类是不完整的,它只能用作基类。在面向对象方法中,抽象类主要用来进行类型隐藏和充当全局变量的角色。2、为什么抽象类不能实例化(不能创建对象)?如果...
摘要由CSDN通过智能技术生成
  • 抽象类为什么不能创建对象

1、什么是抽象类?

抽象类往往用来表征对问题领域进行分析、设计中得出的抽象概念,是对一系列看上去不同,但是本质上相同的具体概念的抽象。通常在编程语句中用 abstract 修饰的类是抽象类。在C++中,含有纯虚拟函数的类称为抽象类,它不能生成对象;在java中,含有抽象方法的类称为抽象类,同样不能生成对象。抽象类是不完整的,它只能用作基类。在面向对象方法中,抽象类主要用来进行类型隐藏和充当全局变量的角色。

 

2、为什么抽象类不能实例化(不能创建对象)?

如果自上而下在类的继承层次结构中上移,位于上层的类更具有通用性,甚至可能更加抽象。从某种角度看,祖先类更加通用,人们只将它作为派生其他类的基类,而不会作为想使用的特定的实例类。例如有一个水果类 fruit(),它是一个广泛的、抽象的概念,而它的一些子类,如apple()类、banana()类,才是具体存在的类,所以水果类本身不能够实例化。

总的来说就是抽象类是一个抽象的、宏观的一个概念,所以它不能直接被实例

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。这是一个比较复杂的问题,需要涉及到多个方面,包括GUI编程、文件读写、计时器和逻辑判断等。下面是一个基本的框架,你可以根据需要进行修改和完善。 首先,我们需要导入必要的模块。 ```python import tkinter as tk import random import time ``` 然后,定义一些全局变量,包括题目数量、每种题目的分值、答案列表等。 ```python # 题目数量 NUM_CHOICES = 20 NUM_JUDGEMENTS = 20 NUM_SHORT_ANSWERS = 20 # 题目分值 CHOICE_SCORE = 4 JUDGEMENT_SCORE = 3 SHORT_ANSWER_SCORE = 15 # 答案列表 ANSWER_CHOICES = [] ANSWER_JUDGEMENTS = [] ANSWER_SHORT_ANSWERS = [] ``` 接着,我们需要定义一个函数来读取题库中的题目和答案。 ```python def read_questions(): # 读取选择题 with open('choices.txt', 'r') as f: for line in f: ANSWER_CHOICES.append(line.strip()) # 读取判断题 with open('judgements.txt', 'r') as f: for line in f: ANSWER_JUDGEMENTS.append(line.strip()) # 读取简答题 with open('short_answers.txt', 'r') as f: for line in f: ANSWER_SHORT_ANSWERS.append(line.strip()) ``` 然后,我们需要定义一个生成试卷的函数。 ```python def generate_paper(): # 随机抽取题目 choices = random.sample(range(NUM_CHOICES), 10) judgements = random.sample(range(NUM_JUDGEMENTS), 10) short_answers = random.sample(range(NUM_SHORT_ANSWERS), 2) # 生成试卷 paper = '' for i in range(10): paper += f'{i+1}. {ANSWER_CHOICES[choices[i]]}\n' for i in range(10): paper += f'{i+11}. {ANSWER_JUDGEMENTS[judgements[i]]}\n' for i in range(2): paper += f'{i+21}. {ANSWER_SHORT_ANSWERS[short_answers[i]]}\n' # 返回试卷 return paper ``` 然后,我们需要定义一个计时器,用来计算考试时间。 ```python def start_timer(): global start_time start_time = time.time() def stop_timer(): global elapsed_time elapsed_time = time.time() - start_time ``` 接着,我们需要定义一个函数来核对答案。 ```python def check_answers(): # 获取答案 answers = [] for i in range(10): answer = choice_vars[i].get() if answer == 1: answers.append('A') elif answer == 2: answers.append('B') elif answer == 3: answers.append('C') elif answer == 4: answers.append('D') for i in range(10): answer = judgement_vars[i].get() if answer == 1: answers.append('T') elif answer == 2: answers.append('F') for i in range(2): answer = short_answer_vars[i].get() answers.append(answer) # 核对答案 score = 0 for i in range(10): if answers[i] == ANSWER_CHOICES[i]: score += CHOICE_SCORE for i in range(10): if answers[i+10] == ANSWER_JUDGEMENTS[i]: score += JUDGEMENT_SCORE for i in range(2): if answers[i+20] == ANSWER_SHORT_ANSWERS[i]: score += SHORT_ANSWER_SCORE # 显示分数和用时 result_label.config(text=f'分数:{score}\n用时:{int(elapsed_time)}秒') ``` 最后,我们需要定义一个GUI界面。 ```python # 初始化窗口 window = tk.Tk() window.geometry('800x600') window.title('自动组卷考试系统') # 读取题库 read_questions() # 生成试卷 paper = generate_paper() # 显示试卷 paper_label = tk.Label(window, text=paper, font=('Arial', 12)) paper_label.pack() # 显示计时器 timer_label = tk.Label(window, text='00:00:00', font=('Arial', 12)) timer_label.pack() # 显示选择题 choice_vars = [] for i in range(10): choice_var = tk.IntVar() choice_vars.append(choice_var) choice_frame = tk.Frame(window) choice_frame.pack() choice_label = tk.Label(choice_frame, text=f'{i+1}. ') choice_label.pack(side=tk.LEFT) choice_options = ['A', 'B', 'C', 'D'] for j in range(4): choice_button = tk.Radiobutton(choice_frame, text=choice_options[j], variable=choice_var, value=j+1) choice_button.pack(side=tk.LEFT) # 显示判断题 judgement_vars = [] for i in range(10): judgement_var = tk.IntVar() judgement_vars.append(judgement_var) judgement_frame = tk.Frame(window) judgement_frame.pack() judgement_label = tk.Label(judgement_frame, text=f'{i+11}. ') judgement_label.pack(side=tk.LEFT) judgement_button1 = tk.Radiobutton(judgement_frame, text='T', variable=judgement_var, value=1) judgement_button1.pack(side=tk.LEFT) judgement_button2 = tk.Radiobutton(judgement_frame, text='F', variable=judgement_var, value=2) judgement_button2.pack(side=tk.LEFT) # 显示简答题 short_answer_vars = [] for i in range(2): short_answer_var = tk.StringVar() short_answer_vars.append(short_answer_var) short_answer_frame = tk.Frame(window) short_answer_frame.pack() short_answer_label = tk.Label(short_answer_frame, text=f'{i+21}. ') short_answer_label.pack(side=tk.LEFT) short_answer_entry = tk.Entry(short_answer_frame, textvariable=short_answer_var, width=50) short_answer_entry.pack(side=tk.LEFT) # 显示提交按钮 submit_button = tk.Button(window, text='提交', command=check_answers) submit_button.pack() # 显示结果 result_label = tk.Label(window, text='') result_label.pack() # 显示开始考试按钮 start_button = tk.Button(window, text='开始考试', command=start_timer) start_button.pack() # 显示结束考试按钮 stop_button = tk.Button(window, text='结束考试', command=stop_timer) stop_button.pack() # 运行窗口 window.mainloop() ``` 这个程序还有很多可以改进的地方,比如添加更多的题目类型、优化界面布局等。但是,以上代码应该可以满足你的需求了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值