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

  • 相关阅读:
    获取各种屏幕宽度、高度
    java中获取本地文件的编码
    线程通信与进程通信的区别
    女性长期没有性生活有什么危害?
    面试中你必须要知道的语言陷阱
    当项目出现空闲时候,如何开展软件测试工作?
    同样是做鸭的 绝味与周黑鸭的区别咋那么大?!
    javaIO(二)
    (原)代码块
    this的使用
  • 原文地址:https://www.cnblogs.com/Shareishappy/p/7492305.html
Copyright © 2011-2022 走看看