|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +'练习面向对象高级特性的@property' |
| 5 | + |
| 6 | +__author__ = 'sergiojune' |
| 7 | + |
| 8 | + |
| 9 | +class Student(object): |
| 10 | + def __init__(self, name, score): |
| 11 | + self.__name = name |
| 12 | + self.__score = score |
| 13 | + |
| 14 | + def get_name(self): |
| 15 | + return self.__name |
| 16 | + |
| 17 | + def get_score(self): |
| 18 | + return self.__score |
| 19 | + |
| 20 | + def set_name(self, name): |
| 21 | + if not isinstance(name, str): |
| 22 | + raise ValueError('请输入正确的名字') |
| 23 | + self.__name = name |
| 24 | + |
| 25 | + def set_score(self, score): |
| 26 | + if score < 0 or score > 100: |
| 27 | + raise ValueError('请输入正确的成绩') |
| 28 | + elif not isinstance(score, int): |
| 29 | + raise ValueError('请输入正确的成绩') |
| 30 | + else: |
| 31 | + self.__score = score |
| 32 | + |
| 33 | + |
| 34 | +stu = Student('bob', 86) |
| 35 | +print(stu.get_name()) |
| 36 | +# 这个就会报错 |
| 37 | +# stu.set_score(999) |
| 38 | + |
| 39 | + |
| 40 | +# 使用@property装饰器 |
| 41 | +class People(object): |
| 42 | + def __init__(self, name, age): |
| 43 | + self.__name = name |
| 44 | + self.__age = age |
| 45 | + |
| 46 | + @property # 添加装饰器,让这个方法变成一个属性 |
| 47 | + def age(self): |
| 48 | + return self.__age |
| 49 | + |
| 50 | + @property |
| 51 | + def name(self): |
| 52 | + return self.__name |
| 53 | + |
| 54 | + @name.setter # 这个前缀名字要和property装饰器的方法名字一致 |
| 55 | + def name(self, name): |
| 56 | + if not isinstance(name, str): |
| 57 | + raise ValueError('请输入正确名字') |
| 58 | + self.__name = name |
| 59 | + |
| 60 | + |
| 61 | +p = People('bart', 20) |
| 62 | +# 加了装饰器之后这样直接调用属性 |
| 63 | +print(p.name) # 这个就是直接获取name属性 |
| 64 | +p.name = 'bat' # 直接修改属性 |
| 65 | +print(p.name) |
| 66 | +print(p.age) |
| 67 | +# 由于age只是只读,不予许写,所以这个会报错 |
| 68 | +# p.age = 52 |
| 69 | + |
| 70 | + |
| 71 | +# 作业:请利用@property给一个Screen对象加上width和height属性,以及一个只读属性resolution |
| 72 | +class Screen(object): |
| 73 | + def __init__(self): |
| 74 | + self.__width = None |
| 75 | + self.__height = None |
| 76 | + |
| 77 | + @property |
| 78 | + def width(self): |
| 79 | + return self.__width |
| 80 | + |
| 81 | + @property |
| 82 | + def height(self): |
| 83 | + return self.__height |
| 84 | + |
| 85 | + @width.setter |
| 86 | + def width(self,width): |
| 87 | + if width < 0 or width > 1000: |
| 88 | + raise ValueError('请输入正确的宽') |
| 89 | + self.__width = width |
| 90 | + |
| 91 | + @height.setter |
| 92 | + def height(self, height): |
| 93 | + if height < 0 or height > 1000: |
| 94 | + raise ValueError('请输入正确的高') |
| 95 | + self.__height = height |
| 96 | + |
| 97 | + @property |
| 98 | + def resolution(self): |
| 99 | + self.__resolution = (self.__height, self.__width) |
| 100 | + return self.__resolution |
| 101 | + |
| 102 | + |
| 103 | +screen = Screen() |
| 104 | +screen.width = 25 |
| 105 | +screen.height = 65 |
| 106 | +print(screen.width) |
| 107 | +print(screen.height) |
| 108 | +print(screen.resolution) |
0 commit comments