zoukankan      html  css  js  c++  java
  • hausaufgabe--python 39 -- objects and class

    00-- 

    class, class object, instance object

    As shown below,  Tracking is a class, and itself it's own class oject. in the object, __dict__ will have all the original data;  d is the instance object of Tracking class with value (2,3), and __dict__ data for d is unique for d. the class object's data in __dict__ still the same for the next newly created instance object. 

    001-- to add a attribute in the Class to trace how many related objects have been created :

    class Tracking:
        count = 0
    
        def __init__(self,x,y):
            self.x = x
            self.y = y
            Tracking.count += 1
        def __del__(self):
            Tracking.count -= 1
        
       
        
           
      
    
        
            

    Running result:

    002-- define a Stack class with below functions

    a-- isEmpty() to check if the Stack is empty (return True or False)

    b-- push() add a data from top 

    c -- pop() pop a data from top

    d -- top() to show a data from top

    d -- bottom() to show a data of bottom 

    class Stack(list):
        count = 0
        def __init__(self):
            Stack.count +=1
            self = []
        def __del__(self):
            Stack.count -=1
    
        def isEmpty(self):
            return not len(self) # to check if any data in it
    
        def push(self,x):
            self.append(x) #to add a data from top
            print('data have been added')
            
        def top(self):
            return self[len(self)-1]#to show the data of top
    
        def bottom(self):
            return self[0]#to show the data in the bottom
            

    Standar:

    class Stack:
        def __init__(self, start=[]):
            self.stack = []
            for x in start:
                self.push(x)
    
        def isEmpty(self):
            return not self.stack
        
        def push(self, obj):
            self.stack.append(obj)
     
        def pop(self):
            if not self.stack:
                print('Warming: Stack is empty!')
            else:
                return self.stack.pop()
     
        def top(self):
            if not self.stack:
                print('Warming: Stack is empty!')
            else:
                return self.stack[-1]
     
        def bottom(self):
            if not self.stack:
                print('Warming: Stack is empty!')
            else:
                return self.stack[0]

    003-- class combination

    // turtle class
    class Turtle:
        def __init__(self, x):
            self.num = x
    // Fish class
    class Fish:
        def __init__(self, x):
            self.num = x
    // Pool class
    class Pool:
        def __init__(self, x, y):
            self.turtle = Turtle(x)        // combine with class Turtle
            self.fish = Fish(y)        //  combine with class Fish
         
        def print_num(self):
            print("There are Turle %d ,Fish %d in the Pool!" % (self.turtle.num, self.fish.num))
    
    >>> pool = Pool(1, 10)
    >>> pool.print_num()

    004-- please note that if the attribute's name is the name as function's, then it will overwrite function. 

    class C:
            def x(self):
                    print('Xman')
    
    >>> c = C()
    >>> c.x()
    Xman
    >>> c.x = 1
    >>> c.x
    1
    >>> c.x()
    Traceback (most recent call last):
      File "<pyshell#20>", line 1, in <module>
        c.x()
    TypeError: 'int' object is not callable

  • 相关阅读:
    分布式缓存负载均衡的规则处理:虚拟节点对一致性哈希的改进
    GoldenGate实时投递数据到大数据平台(6)– HDFS
    OGG 12.3中支持系统procedure复制的几点说明
    使用GoldenGate初始化的两种方式
    传统OGG与Microservice Architecture OGG的通信
    利用Oracle GoldenGate记录源系统所有表的操作
    GoldenGate实时投递数据到大数据平台(5)
    GoldenGate 12.2抽取Oracle 12c多租户配置过程
    GoldenGate实时投递数据到大数据平台(4)- ElasticSearch 2.x
    GoldenGate实时投递数据到大数据平台(3)- Apache Flume
  • 原文地址:https://www.cnblogs.com/Shareishappy/p/7492305.html
Copyright © 2011-2022 走看看