zoukankan      html  css  js  c++  java
  • python-class(1)

     1 #!/usr/bin/env python
     2 #-*- coding:utf-8 -*-
     3 ############################
     4 #File Name: class1.py
     5 #Author: frank
     6 #Mail: frank0903@aliyun.com
     7 #Created Time:2017-09-04 13:35:27
     8 ############################
     9 
    10 #self 代表的是类的实例,代表当前对象的地址,而 self.class 则指向类。
    11 #self 不是 python 关键字,我们把他换成 self1 也是可以正常执行的
    12 class Testself:
    13     def prt(self1):
    14         print (self1)
    15         print (self1.__class__)
    16 
    17 t = Testself()
    18 t.prt()
    19 
    20 class Employee:
    21     'base class of all employees'
    22     empCount = 0    #类变量
    23     
    24     def __init__(self, name, salary):   #构造函数
    25         self.name = name                #self 代表类的实例,self 在定义类的方法时是必须有的,虽然在调用时不必传入相应的参数。
    26         self.salary = salary
    27         Employee.empCount += 1
    28 
    29     def displayCount(self):
    30         print ("Total Employee %d" % Employee.empCount)
    31 
    32     def displayEmployee(self):
    33         #print ("Name:",self.name, ", Salary:", self.salary)
    34         print ("Name:%s, Salary:%d"%(self.name,self.salary))
    35 
    36 #类的内置属性
    37 print ("Employee.__doc__:", Employee.__doc__)
    38 print ("Employee.__name__:", Employee.__name__)
    39 print ("Employee.__module__:", Employee.__module__)
    40 print ("Employee.__bases__:", Employee.__bases__)
    41 print ("Employee.__dict__:", Employee.__dict__)
    42 
    43 emp1 = Employee("one", 100)
    44 emp2 = Employee("two", 200)
    45 
    46 emp1.displayCount()
    47 print ("Total Employee %d" % Employee.empCount)
    48 emp1.displayEmployee()
    49 emp2.displayEmployee()
    50 
    51 #添加,删除,修改类的属性
    52 emp1.age=7
    53 emp1.age=8
    54 del emp1.age
    55 
    56 #getattr(obj, name[, default]) : 访问对象的属性。
    57 #hasattr(obj,name) : 检查是否存在一个属性。
    58 #setattr(obj,name,value) : 设置一个属性。如果属性不存在,会创建一个新属性。
    59 #delattr(obj, name) : 删除属性。
    60 
    61 print hasattr(emp1, 'age')    # 如果存在 'age' 属性返回 True。
    62 print setattr(emp1, 'age', 1) # 添加属性 'age' 值为 8
    63 print getattr(emp1, 'age', 'not find')    # 返回 'age' 属性的值
    64 print delattr(emp1, 'age')    # 删除属性 'age'
    65 
    66 #类的内置属性
    67 print ("Employee.__doc__:", Employee.__doc__)
    68 print ("Employee.__name__:", Employee.__name__)
    69 print ("Employee.__module__:", Employee.__module__)
    70 print ("Employee.__bases__:", Employee.__bases__)
    71 print ("Employee.__dict__:", Employee.__dict__)
  • 相关阅读:
    LeetCode OJ 107. Binary Tree Level Order Traversal II
    LeetCode OJ 116. Populating Next Right Pointers in Each Node
    LeetCode OJ 108. Convert Sorted Array to Binary Search Tree
    LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode OJ 98. Validate Binary Search Tree
    老程序员解Bug的通用套路
    转载 四年努力,梦归阿里,和大家聊聊成长感悟
    转载面试感悟----一名3年工作经验的程序员应该具备的技能
    Web Service和Servlet的区别
    关于spring xml文件中的xmlns,xsi:schemaLocation
  • 原文地址:https://www.cnblogs.com/black-mamba/p/7476798.html
Copyright © 2011-2022 走看看