1 Star 0 Fork 0

chengy/python_demo01

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
oop.py 1.87 KB
一键复制 编辑 原始数据 按行查看 历史
chengy 提交于 2019-02-15 11:47 . init commit
#!/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)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/nocoding/python_demo01.git
[email protected]:nocoding/python_demo01.git
nocoding
python_demo01
python_demo01
master

搜索帮助