zoukankan      html  css  js  c++  java
  • python 属性

    #! /usr/bin/env python
    #! -*- cording:utf-8 -*-
    #
    # class C:
    #     def __getattribute__(self, name):
    #         print("getattribute")
    #         return super().__getattribute__(name)#super默认找父类
    #     def __getattr__(self, name):
    #         print("getattr")
    #     def __setattr__(self, name, value):
    #         print("setatter")
    #         super().__setattr__(name,value)
    #     def __delattr__(self, name):
    #         print("delattr")
    #         super().__delattr__(name)
    # c=C()
    # c.x
    #
    # c.x=1
    
    class rectangle:
        def __init__(self,width=0,height=0):#构造函数
            self.width=width
            self.height=height
        def __setattr__(self, name, value):
            if name=="square":
                self.width=value
                self.height=value
            else:
                super().__setattr__(name,value)
        def getArea(self):
            return  self.width*self.height
    
    r1=rectangle(4,5)#实例化类
    print(r1.getArea())
    r1.square=10#说明赋予了一个square属性,被设置了,则直接把square赋值给value,执行操作,就是正方形
    print(r1.getArea())
    print(r1.__dict__)#直接打印出了字典属性
  • 相关阅读:
    mysql执行sql脚本
    Eclipse Memory Analyzer 进行堆转储文件分析
    JAVA字符串格式化-String.format()
    rpm 使用
    md5sum 使用
    Spring Security 初探
    java工厂模式
    Linux 定时任务
    Java Map 知识
    【转】MVC 比较
  • 原文地址:https://www.cnblogs.com/aqiuarcadia/p/7392255.html
Copyright © 2011-2022 走看看