Members of the class exercises

  1. What is object-oriented private members?

    1.私有类的静态属性
    2.私有对象属性
    3.私有类的方法
  2. How to set an object-oriented class methods, and static methods, static methods class method what's the use?

    1.类方法:@classmethod  得到类名可以实例化一个对象,可以操作类的属性
    2.静态方法:@staticmethod保持代码的一致性,提高代码的维护性
  3. What property is object-oriented? what's the effect?

    1.@property将动态方法伪装成属性,看起来更加合理
  4. What is the role isinstance and issubclass is?

    1.isinstance(a,b)判断a是否是b类的或b类派生类的对象
    2.issubclass(a,b)判断a类是否是b类或b类派生类的子类
  5. Look at the code to write the results:

    class A:
        a = 1
        b = 2
        def __init__(self):
            c = 3
    
    obj1 = A()
    obj2 = A()
    obj1.a = 3
    obj2.b = obj2.b + 3
    print(A.a)
    print(obj1.b)
    print(obj2.c)
    
    结果
    1
    2
    报错
  6. Look at the code to write the results:

    class Person:
        name = 'aaa'
    
    p1 = Person()
    p2 = Person()
    p1.name = 'bbb'
    print(p1.name)
    print(p2.name)
    print(Person.name)
    bbb
    aaa
    aaa
  7. Look at the code to write the results:

    class Person:
        name = []
    
    p1 = Person()
    p2 = Person()
    p1.name.append(1)
    print(p1.name)
    print(p2.name)
    print(Person.name)
    [1]
    [1]
    [1]
  8. Look at the code to write the results:

    class A:
    
        def __init__(self):
            self.__func()
    
        def __func(self):
            print('in A __func')
    
    
    class B(A):
    
        def __func(self):
            print('in B __func')
    
    
    obj = B()
    in A __func
  9. Look at the code to write the results:

    class Init(object):
    
        def __init__(self,value):
            self.val = value
    
    class Add2(Init):
    
        def __init__(self,val):
            super(Add2,self).__init__(val)
            self.val += 2
    
    class Mul5(Init):
    
        def __init__(self,val):
            super(Mul5, self).__init__(val)
            self.val *= 5
    
    class Pro(Mul5,Add2):
        pass
    
    class Iner(Pro):
        csup = super(Pro)
        def __init__(self,val):
            self.csup.__init__(val)  
            self.val += 1
    # 虽然没有见过这样的写法,其实本质是一样的,可以按照你的猜想来。
    p = Iner(5)
    print(p.val)
    36
  10. Follow these requirements, the completion of a commodity.

    • Packaging trade name, original price of goods, as well as a discount.
    • Achieve a real commodity prices get way price.
    • Next complete three methods, the use of a combination of properties complete the following requirements:
      • The method of this property by using the attribute price disguised properties.
      • Use setter decorator decoration and implement the modifications original price of goods.
      • Use deltter decorator decoration and delete the original real property.
    class Goods:
        def __init__(self,name,original_price,discount):
            self.name = name
            self.original_price = original_price
            self.discount =discount
        @property
        def price(self):
            return self.original_price * self.discount
        @price.setter
        def price(self,value):
            self.original_price = value
        @price.deleter
        def price(self):
            del self.original_price
    DY = Goods("大衣",100,0.95)
    print(DY.price)
    DY.price = 1000
    print(DY.price)
    del DY.price
    print(DY.__dict__)
    

Guess you like

Origin www.cnblogs.com/ciquankun/p/11324392.html