代码拉取完成,页面将自动刷新
#!/usr/bin/env python3
# _*_ coding:utf-8 _*_
'面向对象高级编程'
from types import MethodType
__author__ = 'chenguangyue'
# 数据封装、继承、多态是面向对象编程设计中最基础的概念,多重继承、定制类、元类等
class Student(object):
# __slots__ = ('name', 'age', 'score') # 用tuple定义允许绑定的属性名称,只对当前类起作用,对子类不起作用
@property
def score(self):
return self._score
@score.setter
def score(self, score):
if not isinstance(score, int):
raise ValueError('score must be an integer!')
if score < 0 or score > 100:
raise ValueError('score must between 0~ 100!')
self._score = score
s = Student()
s.score = 90
print(s.score)
class Screen(object):
@property
def width(self):
return self._width
@width.setter
def width(self, width):
self._width = width
@property
def height(self):
return self._height
@height.setter
def height(self, height):
self._height = height
@property
def resolution(self):
return self._width * self._height
s = Screen()
s.width = 1024
s.height = 768
print('resolution = ',s.resolution)
# def set_score(self, score):
# if not isinstance(score, int):
# raise ValueError('score must be an integer!')
# if score < 0 or score > 100:
# raise ValueError('score must between 0 ~ 100!')
# self.score = score
#
# def get_score(self):
# return self.score
# def set_age(self, age):
# self.age = age
#
#
# def set_score(self, score):
# self.score = score
#
#
# s = Student()
# s.name = 'Michael'
# # s.set_age = MethodType(set_age, s)
# # s.set_age(25)
# print(s.name)
#
# # print(s.age)
#
# Student.set_score = set_score
# s.set_score(88)
# print(s.score)
#
# s2 = Student()
# s2.set_score(99)
# print(s2.score)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。