python类实例化及练习

#!/usr/bin/python
# coding=utf-8

#class employee:
#  'all employee base'
#  empcount = 0
#  name = "default"
#  salary = 20000
#
#  def __init__(self,name,salary):
#    self.name = name
#    self.salary = salary
#    employee.empcount += 1
#
#  def displaycount(self):
#    print "Total employee %d" % employee.empcount
#
#  def displayemployee(self):
#    print "Name:",self.name, ",Salary: ",self.salary
#
##use class
#emp1 = employee("Zara",2000)
#
#emp2 = employee("Jone",3000)
#
#
#'''
#emp1.displayemployee()
#emp2.displayemployee()
#
#print hasattr(employee,'name')
#print hasattr(employee,'salary')
#print hasattr(employee,'empcount')
#'''
#
#print "employee.__doc__:", employee.__doc__
#print "employee.__name__:", employee.__name__
#print "employee.__module__:", employee.__module__
#print "employee.__bases__:", employee.__bases__
#print "employee.__dict__:", employee.__dict__

#class Point:
#  def __init__(self, x=0, y=0):
#    self.x = x
#    self.y = y
#  def __del__(self):
#    class_name = self.__class__.__name__
#    print class_name,"destroy"
#
#pt1 = Point()
#pt2 = pt1
#pt3 = pt1
#print id(pt1),id(pt2),id(pt3)
#del pt1
#del pt2
#del pt3

class parent:#define parent
  parentattr = 100
  _protectedattr = 1

  def __init__(self):
    print "calling the parent class constructor"

  def parentmethod(self):
    print "calling parent class methods"

  def setattr(self,attr):
    parent.parentattr = attr

  def getattr(self):
    print "parent attributes", parent.parentattr

class child(parent):#define son
  def __init__(self):
    print "calling the son class constructor"

  def childmethod(self):
    print "calling the son method"

#c = child()
#c.childmethod()
#c.parentmethod()
#c.setattr(200)
#c.getattr()
#
#print issubclass(parent,child)
#print issubclass (child,parent)

#print isinstance(c,child)

c = parent()
print c._protectedattr

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值