クラス演習のメンバー

  1. オブジェクト指向のプライベートメンバーとは何ですか?

    1.私有类的静态属性
    2.私有对象属性
    3.私有类的方法
  2. オブジェクト指向のクラスのメソッドを設定し、静的メソッド、静的メソッドのクラスメソッド方法使用は何ですか?

    1.类方法:@classmethod  得到类名可以实例化一个对象,可以操作类的属性
    2.静态方法:@staticmethod保持代码的一致性,提高代码的维护性
  3. オブジェクト指向とは何な特性でありますか?役割とは何ですか?

    1.@property将动态方法伪装成属性,看起来更加合理
  4. 役割でisinstanceとissubclassは何ですか?

    1.isinstance(a,b)判断a是否是b类的或b类派生类的对象
    2.issubclass(a,b)判断a类是否是b类或b类派生类的子类
  5. 結果を書き込むためのコードを見てください:

    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. 結果を書き込むためのコードを見てください:

    class Person:
        name = 'aaa'
    
    p1 = Person()
    p2 = Person()
    p1.name = 'bbb'
    print(p1.name)
    print(p2.name)
    print(Person.name)
    bbb
    aaa
    aaa
  7. 結果を書き込むためのコードを見てください:

    class Person:
        name = []
    
    p1 = Person()
    p2 = Person()
    p1.name.append(1)
    print(p1.name)
    print(p2.name)
    print(Person.name)
    [1]
    [1]
    [1]
  8. 結果を書き込むためのコードを見てください:

    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. 結果を書き込むためのコードを見てください:

    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. これらの要件、商品の完成に従ってください。

    • 商品名、商品の元の価格だけでなく、割引をパッケージング。
    • 実際の商品価格は、道の価格を取得達成。
    • 次の完全な3つの方法は、特性の組合せを使用するには、以下の要件を満たすことになります。
      • 属性価格装っプロパティを使用して、このプロパティの方法。
      • セッターデコレータの装飾を使用して、商品の変更元の価格を実装します。
      • deltterデコレータの装飾を使用して、元の不動産を削除します。
    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__)
    

おすすめ

転載: www.cnblogs.com/ciquankun/p/11324392.html