python oop

/usr/bin/python3.6

@# Study notes for personal reference only

'''
class A(object):
    hobby = 'cpath'
    def __init__(self , name , age ,weight):
        self.name = name;
        self._age = age;
        self.__weight = weight;

    def get_w(self):
        return self.__weight;


    # 直接通过 A.get+hobby 条用 ,不需要实例化
    @classmethod

    def get_hobby(self):
        return self.hobby;

    # 可以把方法当成属性调用, A.get_weight
    @property
    def get_weight(self):
        return self.__weight();

    def self_intrudection(self):
        print ('my name is %s \n %s years old\n' % (self.name , self._age));




class B(A):
    def __init__(self, name ,age ,weigh , language):
        super(B , self).__init__(name ,age ,weigh);# 重新父的构造方法
        self.language  =  language


    def self_intrudection(self):
        print(' b my name is %s \n %s years old\n' % (self.name , self._age));


def introduce(object):
    if isinstance(object , A):
        object.self_intrudection();


if __name__ == '__main__':'''

'''test = A('cpath ', 18 , 175)
    print(test);
    print(dir(test)); # 获取对象属性
    print(test.__dict__); #获取类中的属性
    print(test.get_w());  # 调用类中的方法
    print(test._Test__weight); # 调用"私有属性"

    print(A.get_weight); # 把方法当成属性调用
    print(A.get_hobby()); # 不需要实例化对象直接调用
    print(test.self_intrudection());'''

'''b = B('cpath ', 18 , 175 , 'zh_en');
    print(dir(b));
    print(b.__dict__);
    print(type(b)); # 查看对象属性类型
    print(isinstance(b , A));# b 从属于A  类'''

    # 多态 重写
'''a = A('cpath ', 18 , 175);
    b = B('lu ', 19 , 1799 , 'zh_en');
    #print(b);
    introduce(a);
    introduce(b);'''
@# 创建类对象 def __new__(cls)
@# 回收对象   def __del__();
'''
class Programer(object):

    def __new__(cls , *args , **kwargs):
        print('call __new__ method');
        print(args);
        #return super(Programer , cls).__new__(cls , *args ,**kwargs)
        return super(Programer , cls).__new__(cls) #3.5 +

    def __init__(self , name , age):
        print('call __init__ method');
        self.name = name
        self.age = age;

if __name__ == '__main__':
    programer = Programer('cpath', 18);
    print(programer.__dict__);
'''
@# 类和Magin Method中运算符
@# __cmp__ 包含
@# isinstance 判断对象是否是已知类型
'''
class Programer(object):

    def __init__(self , name ,age):
        self.name = name;
        if isinstance(age , int):
            self.age = age;
        else:
            raise Exception('age must be int');


    def __eq__(self , other):
        if isinstance(other , Programer):
            if self.age == other.age:
                return True;
            else:
                return False;
        else:
            raise Exception('the type of object must be Programe'); 


    def __add__(self , other):
        if isinstance(other , Programer):
            return self.age + other.age;
        else:
            raise Exception('The type of object must be Programer');



if __name__ == '__main__':
    a = Programer('cpath', 18);
    b = Programer('cpathu' , 18);
    print(a==b);
    print(a+b)

'''
@# __str__   __repr__ __unicode__ 将对象装换成字符串
'''
class Programer(object):

    def __init__(self , name , age):
        self.name = name;
        if isinstance(age ,int):
            self.age = age;
        else:
            raise Exception('age must be int');

    def __str__(self):
        return '%s is %s years old' % (self.name , self.age)

    # 返回当前属性的键名
    def __dir__(self):
        return self.__dict__.keys();


if __name__ == '__main__':
    p = Programer('cpath', 18);
    print(p);
    print(dir(p));
'''    
@# 设置类的属性控制控制
'''
class Programer():

    def __init__(self , name , age):
        self.name = name;
        self.age = age;

    def __getattribute__(self , name):

        return super(Programer,self).__getattribute__(name);


    def __setattr__(self , name , value):
        #setattr(self , name ,value);#x
        self.__dict__[name] = value;



if __name__ == '__main__':
    a = Programer('cpath' , 18);
    print(dir(a));
    print(a.name);
'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值