0
点赞
收藏
分享

微信扫一扫

DevOps(9)

九点韶留学 2024-01-09 阅读 8
class Student():
	def __init__(self, name, age):
		self.name = name
		self.age = age
		self.__score = 0

	def marking(self, score):
		if score < 0:
			return '分数不能为0'
		self.__score = score
		print(self.name + '同学本次得分是:' + str(self.__score))	

	def __talk(self): # 私有的类可通过在前面加__即可
		print('私聊我')	

  
    
student1 = Student('喜小乐', 18)    
student1.marking(70)
# student1.talk() # 会报错
student1._Student__talk()
student1.__score = 20
print(student1.__score)
print(student1.__dict__)

student2 = Student('石敢当', 18)    
print(student2.__dict__)

print('***********************')
print(Student.__dict__)

打印结果如下:
在这里插入图片描述

举报

相关推荐

0 条评论