封装

# class Room:
# def __init__(self,name,length,width):
# self.__name = name
# self.__length = length
# self.__width = width
# def get_name(self):#C++的编码习惯,一个get和一个set
# return self.__name
# def set_name(self,newName):
# if type(newName) is str and newName.isdigit() == False:
# self.__name = newName
# else:
# print('不合法的姓名')
# def area(self):
# return self.__length * self.__width
#
# jin = Room('金老板',2,1)
# print(jin.area())
# jin.set_name('2')
# print(jin.get_name()) # 假设父类的私有属性 能被 子类调用么
# class Foo:
# __key = '123' # _Foo__key
#
# class Son(Foo):
# print(Foo.__key) # _Son__key # 会用到私有的这个概念de场景
#1.隐藏起一个属性 不想让类的外部调用
#2.我想保护这个属性,不想让属性随意被改变
#3.我想保护这个属性,不被子类继承

property

# property
# 内置装饰器函数 只在面向对象中使用
from math import pi
class Circle:
def __init__(self,r):
self.r = r
@property(加装饰伪装成一个属性,后面直接连方法名即可,类似查看属性)
def perimeter(self):
return 2*pi*self.r
@property
def area(self):
return self.r**2*pi # c1 = Circle(5)
# print(c1.area) # 圆的面积,面积和周长是一个名字,相当一个属性,所以我把他伪装成一个属性,看上去更合理,但不能像其他属性那样去外部更改,因为它是计算而得的
# print(c1.perimeter) # 圆的周长 # class Person:
# def __init__(self,name,high,weight):
# self.name = name
# self.high = high
# self.weight = weight
# @property
# def bmi(self):
# return self.weight / self.high**2 # jin = Person('金老板',1.6,90)
# jin.bmi = 18
# classmethod
# staticmethod # class Person:
# def __init__(self,name):
# self.__name = name
# @property
# def name(self):
# return self.__name + 'sb'
# @name.setter
# def name(self,new_name):
# self.__name = new_name
#
# tiger = Person('泰哥')
# print(tiger.name)
# tiger.name = '全班'#像要修改伪属性,要在上面加 @name.setter一段代码
,三个标红name必须一样 # print(tiger.name) # class Goods:
# discount = 0.8
# def __init__(self,name,price):
# self.name = name
# self.__price = price
# @property
# def price(self):
# return self.__price * Goods.discount
# apple = Goods('苹果',5)
# print(apple.price) # 属性 查看 修改 删除
# class Person:
# def __init__(self,name):
# self.__name = name
# self.price = 20
# @property
# def name(self):
# return self.__name
# @name.deleter
# def name(self):
# del self.__name
# @name.setter
# def name(self,new_name):
# self.__name = new_name
# brother2 = Person('二哥')
# del Person.price
# #brother2.name = 'newName'
# #brother2
# del brother2.name #deleter以及下方内容不会有删除的作用,当外部用del Person.price就会触发找到@name.deleter,然后执行下方函数的删除私有属性 动作 # print(brother2.name)#这里的不是删除name方法名,而是去执行删除属性的方法,对象是没权删除类里的方法的

class_static

# method 方法
# staticmathod  静态的方法 ***
# classmethod   类方法    ****
# 类的操作行为
# class Goods:
#     __discount = 0.8
#     def __init__(self,name,price):
#         self.name = name
#         self.__price = price
#     @property
#     def price(self):
#         return self.__price * Goods.__discount
#     @classmethod   # 把一个方法 变成一个类中的方法,这个方法就直接可以被类调用,不需要依托任何对象
#     def change_discount(cls,new_discount):  # 修改折扣
#         cls.__discount = new_discount
# apple = Goods('苹果',5)
# print(apple.price)
# Goods.change_discount(0.5)   # Goods.change_discount(Goods)
# print(apple.price)
# 当这个方法的操作只涉及静态属性的时候 就应该使用classmethod来装饰这个方法

# java
class Login:
    def __init__(self,name,password):
        self.name = name
        self.pwd = password
    def login(self):pass

@staticmethod
    def get_usr_pwd():   # 静态方法
        usr = input('用户名 :')
        pwd = input('密码 :')
        Login(usr,pwd)

Login.get_usr_pwd()
# 在完全面向对象的程序中,
# 如果一个函数 既和对象没有关系 也和类没有关系 那么就用staticmethod将这个函数变成一个静态方法

# 类方法和静态方法 都是类调用的
# 对象可以调用类方法和静态方法么?   可以   一般情况下 推荐用类名调用
# 类方法 有一个默认参数 cls 代表这个类  cls
# 静态方法 没有默认的参数 就象函数一样

下周内容
 # 面向对象的进阶
 # 网络编程

初识面向对象-封装、property装饰器、staticmathod(静态的方法)、classmethod(类方法) (五)的更多相关文章

  1. 第7.26节 Python中的@property装饰器定义属性访问方法getter、setter、deleter 详解

    第7.26节 Python中的@property装饰器定义属性访问方法getter.setter.deleter 详解 一.    引言 Python中的装饰器在前面接触过,老猿还没有深入展开介绍装饰 ...

  2. Python使用property函数和使用@property装饰器定义属性访问方法的异同点分析

    Python使用property函数和使用@property装饰器都能定义属性的get.set及delete的访问方法,他们的相同点主要如下三点: 1.定义这些方法后,代码中对相关属性的访问实际上都会 ...

  3. Python10/24--组合/封装/property装饰器/多态

    组合的应用: 1. 什么是组合 一个对象的属性是来自于另外一个类的对象,称之为组合 2. 为何用组合 组合也是用来解决类与类代码冗余的问题 3. 如何用组合 '''class Foo: aaa=111 ...

  4. [转载]Python使用@property装饰器--getter和setter方法变成属性

    原贴:为什么Python不需要getter和setter getter 和 setter在java中被广泛使用.一个好的java编程准则为:将所有属性设置为私有的,同时为属性写getter和sette ...

  5. 第7.27节 Python案例详解: @property装饰器定义属性访问方法getter、setter、deleter

    上节详细介绍了利用@property装饰器定义属性的语法,本节通过具体案例来进一步说明. 一.    案例说明 本节的案例是定义Rectangle(长方形)类,为了说明问题,除构造函数外,其他方法都只 ...

  6. @property 装饰器

    property() 函数作用于新式类,返回属性值. class C(object): def __init__(self): self._x = None def getx(self): print ...

  7. 第8.27节 Python中__getattribute__与property的fget、@property装饰器getter关系深入解析

    一. 引言 在<第7.23节 Python使用property函数定义属性简化属性访问的代码实现>和<第7.26节 Python中的@property装饰器定义属性访问方法gette ...

  8. 绑定方法和隐藏属性之property装饰器

    目录 一:绑定方法 1.绑定给对象的方法 2.绑定给类的方法 3.非绑定方法之静态方法 二,隐藏属性 1.如何隐藏属性 三,property 装饰器 一:绑定方法 1.绑定给对象的方法 class P ...

  9. 面向对象之封装 及@property装饰器使用

    目录 封装 1.封装的定义 2.封装的目的: 3.封装的三种方式 4.封装的优点 5.访问限制(封装) @property 装饰器 属性property底层实现 封装 1.封装的定义 将复杂的丑陋的, ...

随机推荐

  1. 20172325 2017-2018-2 《Java程序设计》第六周学习总结

    20172325 2017-2018-2 <Java程序设计>第六周学习总结 教材学习内容总结 1.利用[ ]建立一个数组,整列数据可以通过数组名引用,数组中的每个元素则可以通过其在数组中 ...

  2. python panda库自动去重

    http://blog.csdn.net/xinxing__8185/article/details/48022401

  3. pthread_once 和 pthread_key

    http://blog.csdn.net/rickyguo/article/details/6259410 一次性初始化 有时候我们需要对一些posix变量只进行一次初始化,如线程键(我下面会讲到). ...

  4. CSS 关键的基础知识

    今晚看了 百度传课 一门关于CSS的课程, 感觉不错, 随手记了点儿笔记, 供以后查阅. =================================================== pos ...

  5. 让kbmmw 4.8 支持ios 64

    随着xe8 的出来,其开始支持IOS 64 的编译了(不支持也没办法,从今年2月开始不支持ios 64 的应用 就不允许入住apple appstore,霸气呀).相信不少同学迫不及待的开始了ios6 ...

  6. javabean为什么要实现序列化?

    javabean为什么要实现序列化? 所谓的Serializable,就是java提供的通用数据保存和读取的接口.至于从什么地方读出来和保存到哪里去都被隐藏在函数参数的背后了.这样子,任何类型只要实现 ...

  7. 19. Fight over Fox-hunting 猎狐引发的冲突

    . Fight over Fox-hunting 猎狐引发的冲突 ① Foxes and farmers have never got on well.These small dog-like ani ...

  8. 设定Word段落的背景色

    段落背景不同于文字区别.很多新接触word的朋友都找不到怎么弄. 先把光标停留在需要设置的段落文字上,或者选择需要设置的段落文字. 点击段落里的边框和底纹,如图 在弹出框中选择底纹. 选择需要填充的颜 ...

  9. UVa 11039 Building designing (贪心+排序+模拟)

    题意:给定n个非0绝对值不相同的数,让他们排成一列,符号交替但绝对值递增,求最长的序列长度. 析:我个去简单啊,也就是个水题.首先先把他们的绝对值按递增的顺序排序,然后呢,挨着扫一遍,只有符号不同才计 ...

  10. VBA替换函数

    Sub test() On Error Resume Next Dim arr1, arr2, i, j arr1 = Range("T1:EI3") arr2 = Range(& ...