Python学习笔记12—类

时间:2023-12-10 09:42:20

典型的类和调用方法:

#!/usr/bin/env Python
# coding=utf-8
__metaclass__ = type #新式类
class Person: #创建类
def __init__(self, name): #初始化函数
self.name = name
def getName(self): #类中的方法(函数)
return self.name
def color(self, color):
print "%s is %s" % (self.name, color)
girl = Person('wangguniang') #实例化
name = girl.getName() #调用方法(函数)
print "the person's name is: ", name
girl.color("white") #调用方法(函数)
print "------"
print girl.name #实例的属性

运行结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAa0AAABqCAIAAACArpnYAAAMIUlEQVR4nO2dyZniMBBGiWAi6DMXIugIuPhKJE5g0nAKnYSS6BSIY+bgTUtJVcLG2PDe14cGZKlKQj9a7NLplPD1c7v9u93+fX/J7yd/vxc/2Z+/1zDB9RJ8ric4nb6+C0Xcvvs3v29ebtkEY1Y/X5nSk1LGy6O/vjify2+Q4Pr3T95HyVOhoLjOs1wu19/r9fdSsGGuzZ9cPeerOrLt5+t0uVylFsm2ZpT+5ysqKzF1+DR1IS7iV3DDRNPd7/d714hvC3SN/6Frz+NL7980O6X4OevWxRlElrj2HL2t2hDkIJp2HorNpvCyGA0Q02Su7Rq9iKkY12Zr6+XkdHBHxAqyY1OfxKiDlrSDjvzsvJKyOjh8/CP/GsGO6HXQRusqfkNewAF0ECp0sNcXcTC4KxQdhANg18Gm2/Vg8BTPf/fffz4Ssw4eYDAYju7RwQNj1sG9DwZh/0RrguJsUVrM5ScNnkdmbbOUZt/rgwAAAAAAAAAAAAA7RrhHtwI2xFW8W5epH4XxruHsTcevYdiT2JlVYOfye73+5rvfKIL9JumwefrQfSFKQR9AuQaon5lz+U6Pptul4rRuj1aBBeW+31745rvMBll85K7vj+/nalXvqH5e/DwJOvhpvHraWO6cyaev1cHwYWfJ1Acn75uwDx1U76OOlkFeooboYHXR+YeLLY8wv5zsc3X+1/H3Enx9vQcblsRZSO8Qjp8g9p+jCLrH91d0uRwIIDCm0M9FS6Lu5/1gCJn0OXzfPAMiK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wEgN0S9F9vt2TCuxnk3s+e5xtLkjtNvFtHYqULbygJBaIqwd21Tf/SakOy9CfYuIWbAAADqkC8h4K8hxcA8BTSm/NqExyC9/ACAJ6ANPeuS3AI3sMLAFiNcIEus1FRTHAI3sMLAAAAeHf+A/cpDJjZLItqAAAAAElFTkSuQmCC" alt="" />

self 的属性数据,也不一定非得是由参数传入的,也可以在构造函数中自己设定

#/bin/env/python
#coding:utf-8
__metaclass__ = type
class Person:
def __init__(self, name,general="male"):
self.name = name
self.email = "zy5724@163.com"
self.general = general info = Person("keven")
print "info.name=",info.name
print "info.email=",info.email
print "info.general=",info.general

运行结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbAAAABSCAIAAAC61xZAAAANZElEQVR4nO2dS5bbxhWGsQKtQGMeJbTd6syY2DmxWgonmDKPk8SWnSVwA9oGtiDJj2b32HAcU63Me2Q5idUSvQ15gFc97q178SBRYP/f0aBFFKpuVaF+1At1k4urt7/54z8ThrtPV6t39b/lfM4FBOMxny+vl8truW7uPFmu3q1WT+8ewKiOrBbG87ZaPrkztkGgK2m2y1JNwHWuDHgQLq52AUEEkTO/Nt9Yq9W71WLlhrHfani3gX2TZjuTfD0Tw+x2+frwhhKEe4gAAHCLuIQgAgBAAXqIAABQcnH19hSCCAAASSGIj6YsiPP50lwuaLmEimV0kXJtGuWjYLbO+WWE8SgXMCKzKk4urt6ePvp8bCtY5tfL5TXfDis1LJZWyyXXTttKhIRuAeESQPk0zMIbRdIsSulZ5zFaFR0XL96cPvxsbCs47i6CHZNCAZvdaqU+Ljoo4q1v8GJRR1Q+Rb/e32B0ICCIR8zFizf3eUEce0QZbqXe1XEFsdpUTDXUwtSO4/qDEIcgihuznRmSUWQRgtg66d2OLbFmQ2IUpbbZ3tw/e8xdrQTRkxjzubyeW8+x8cmEMf1ES2oggL/luPpXGWN+oWG1k8Vd5/bKJDs5y5hAgyctcdqh8eYgIiliWKwMAxxNtIVA/wZqbLueO3bWamL9XqRrJbekknb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jrz/669hWAABABDzPf/zVR38JBIBPlf2j3DM9LpMwEoB+PM9f3ftwv4I4hKLeDqZw5vY0jASgG8+++eHe7/68zxTkM2lAySS0ZhJGAtCNZ9/8cO+3f9pnChBENZPQmkkYCUA3eEEMzhnF41PF+Zif8D1k71s2s6LJRTgG82J9+imVT9UM3F60Jh3It4zGyGBBARA/8pA5ep8qdezF4VeWtZLTFTkXcgxVqCwzsmOdptLLbcsQR8wewreMrqAAiJpn+asegji+TxUnaqdVik5XxFwoYqjTbXH8accjF7tyAN8yyoICIGqe56/C224m5kLAspY9zNA+wy6QC00MXrKkpZqu3biC2MtIbUEBEDVffPtfYWP2tAVR7KCIgqjq4gSkrK/bloGGzGFB7O1bBktn4Cj48l/Sp3sxDJmZgbQ4ZFYMwIVcKIfwAUEc0m1LVw7gW6bVXAcAkSIf7hCzTxVxUUVyuqLIhRhDlTQjB0O6benKQXzLqAoKgKj56t8/vf/xP/zfJ+NThdh2E0zCbOeKXAgxuF8l0hnp5rZloCHofn3LBHYXDZcFAA7F19+//uCYzkPEtmEAQGfOtzdHdWI2prIAAJ05PyqfKmmGYRoAoDOb7ZuJCyI+FwMADMMvvibsS8/X2O4AAAAASUVORK5CYII=" alt="" />

继承

#!/usr/bin/env python
# coding=utf-8
__metaclass__ = type
class Person:
def speak(self):
print "I love you."
def setHeight(self, n):
self.length = n
def breast(self, n):
print "My breast is: ",n class Girl(Person):
def setHeight(self):
print "The height is:1.70m ." #方法重写 if __name__ == "__main__":
cang = Girl()
cang.setHeight()
cang.speak()
cang.breast(90)

运行结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbUAAABUCAIAAACKpz4ZAAALP0lEQVR4nO2dy52jMAzGqWAr2DOXVJAKuHBNJTSwbdDCNuEmpoXUsXvgJcmSLYPzIPn+vznMEGMLGH+RH0hNY/D77+32b/3pLherIHgdl0v303U/+Wfz6093+3e7/f39BKN2cruS/7db9+fXqw0Ce+nH+9h7Cg7BWRCAQi4/9Avsdv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qRFXYOEKt0RF35J+JTHQEHXu6Pr4G5hIq0d5aHrbNcDEB+Hw2GZmdvKlXyCA3qcwtzkYSTeeq9eWfUd6xblP1xCY9XB8dNqxlHqejVP80/1EMuqGP4GvhM490UBX3GSNzi7ORVNNrE6Y+ZlqMt9Rkz1FE6Bn6mLOBbK96uEIaXyTwHwGYMGREUTQzc4urEef4OhE5PKkWufzZeuVygP5gfXTZ8Fz0KQ3MPwLQNE2JPiYyt3gaYZss22lhomDNJ5N/Js6mLXyeeKFW28WtVbNDGRL6mLfhQLvFRpqOIdavAWhCPMZKZWBJZW4xIO8XskFdtPScnluMTYs6Latk7MWLjdvieHrurxXV7LhU80KcNjx+fO156RP7HwEo5Hv2TwMAQAnI3AIAACpwHgEAgILMLQAAAAAAAAAAgOA/wD6U8OJBuL4AAAAASUVORK5CYII=" alt="" />

多重继承:

#/usr/bin/env python
__metaclass__ = type class Person:
def speak(self):
print "Hello,world!!!"
def name(self,n):
print "My name is: ",n class Man:
def role(self,r):
print "I'm a ",r class Boy(Person,Man):
def food(self):
print "I like drink milk." if __name__ == "__main__":
keven = Boy()
keven.speak()
keven.name("keven")
keven.role("student")
keven.food()

运行结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUcAAABfCAIAAAD02ZJ+AAAJvElEQVR4nO2d24GEIAxFpxqrsRursRqqsY/98EUgLx6OjnvP166OGCARVHL9fA7GeVnm8cMzhRDCwvxgnMnWYQrxv/rez2eYQpgG9ozjvP5yN2v/n+zkDrC2bWcVf0WNzFtFsbkTXE8MU1hSxN7KyyP15ZtFaL/l7Ib1r2EKvjPnDRnbnPrCOC/L0a7WeS2/YqvgIbWSmpXX3d0Yd2FENb+TqVQUM/rejyeqD1+jTnf8dxjm9lQrqq0i6qN6c5nl9BPeHcWe2Nozi1IT0hFp9c+zKlfFqNZqc5Fjk3aK66WfzTiv6Vf0nAVxxzV9Op5EtXI2xY1YYzXnyXoo2YFmR/VxMcwG1DANn3FeQlh/wJZUF9X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I/uoDRj77dBVIP/gvkFvCVdMLzY2gY6vGpCDyTlAzaHKVmEGiU/MTMXqkqQNlqdPoFqFQD1WNnPebQa2gYGmmpCG5bygTYqqtkOuclx9DXqE2CsBv3RoprfZ2VWdTt9IdaVpTaq80tbfC1q1SdAVIP+tKiJ1EW1rJrQjKp8UBnV3GJ40gT5LHopyHlEVIP+fDmqVdWErmTKB/3G6lhaqFWfAFEN+vPdqNZVE5qwlQ+ofF+qIhD9ODEruXNeR2ei9dCiT6BYBUAZle+rfdoG3vPGqgn9K8Rrngi76YP5TB4wOjJMQ/rErE2fQLEKAAAAAAAAAAAAAAAAAAAAfIGeS7y/xTNtfqZV4G0wcvTsb6Q3xtevpqrTNqiJn+trJFml9cK32hk8ih6qCXXLPb+2RvJrnyi4d9WnVE2sRf2/tLg+onoFUQ2exQVRbczP6XfYsx+2rJ+UtQ32BaFhGqLFoXQxN39KXfnAUyPNVl1/wlWcHdUFVoFXcN1Y7fO2NIFJUyBQ0bUNVqYQgppQwtvsVz4ouzH36k90b2fwcm6K6ix9QjrCa5+ubbBhfuFTjGo7m5KtkWmzK/utdzuDt3NLVGtz3RxPZqKubbBj3mlWR3XtDcO1UY1p9z/lvhl4Pr1utEXRNji2XBXVQo1Mm68eqyusAr/PnffV65Q4DhhdgUDF0DbYNl4a1ft5vbIH34lqzSqINLyLhvfVzKGyggC79/A25umspEBQaFmibZBP7uP41my2lA/sGhm2ivoT17bz55yjY4oOAAAAAAAAAACAf80f7WttQZLT9UwAAAAASUVORK5CYII=" alt="" />

继承的特点,即将父类的方法和属性全部承接到子类中;如果子类重写了父类的方法,就使用子类的该方法,父类的被遮盖。

super 函数

super 函数的参数,第一个是当前子类的类名字,第二个是 self,然后是点号,点号后面是所要调用的父类的方法。

#/usr/bin/env python
__metaclass__ = type class Person:
def speak(self):
print "Hello,world!!!"
def name(self,n):
print "My name is: ",n class Man:
def role(self,r):
print "I'm a ",r class Boy(Person,Man):
def food(self):
print "I like drink milk."
def speak(self):
print "I'm a boy."
super(Boy,self).speak() if __name__ == "__main__":
keven = Boy()
keven.speak()
keven.name("keven")
keven.role("student")
keven.food()

其中“super(Boy,self).speak() ”也可以通过”Person.speak(self)”调用speak方法。

执行结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAToAAABpCAIAAAAlTXjmAAAJuElEQVR4nO2d26GsIAxFpxqrsRursRqqsY/7MaPyCEnw7blr/ZwzPjAIAUS2+XxK+nH6MfbCbriCIVAGd9KPjjvfDSEMV1gDADWWtnKapjB0xf61Q6seAgAAAAAAAABPJnq4b5sqZZr7dLrvzMz99/dX1vcb8ufphqDMrw1hnX2L//fjmpdvRLf5XPpxCsP89yIbtPyecX838RhDLuOODsmoCvHbrU0F8jp31d+7dkP47pj/XgHumrG+77kz7/0oXv835pnC0C3/pwd26vuq2t5ku5D/tZYsR857599ral25ac5TlqVfozQ3BHMblR4V2xY3GZbNB1EpibVTbehcfyPWIbE8OnlppH8XTO6kmd9+nMY+uVlNpR8G7Vyd9DkpL+jkFemy57t1DHN9GsRqo9bn9db3o9/oXaVQz3+16g0hhCDVzX5MtnZDiH/qe9WWey6A2aykQKTG1LutXJWSHpUaWd6V8wfDUklInuNsKIpnu37Mm7okpWyD0btGKXdDyAdESul3Q4jMyM9VKYsovlBey5Lsrjat/8UZtmpsnnl3Y72zFMT0FHeVdwppRs6g7/143HVxpdSnll+LYaJnbnFXK4nt7ro2+XMCcm2olsTvfmaPCZ7L5snljV/a0uf1xj0YTn4bpd9UCvlVy1FU7HBKs73+F+Vr3WjW2PSaDUOrnaXgSm+hUmS6j9geZLvr0sgUXWAYuk8/TiF8DxBT2uaueUd2iLvGnhl1lbXFbpnZOzpX+SakmRAr8XrpTe5qlf5md5X8J8lAcae+N7qe04YaG21sG77vLAUpvWZ3PbF3XXbJdbsbQhi+DVI/TuMgN4Eb3DWfgj6wd/WizCIkLdiO5IQutHzkWHY9vnf9pMNZJZ0jetdvjWycs9hZCq70FqpFtu/ZNRvXfZsscZd84WX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iyIi/2HG06u6qy2CE3Nk2uzSNh19n+G13OKu2rCzxCMo00XkM+bT3GZ33Tp2P9ddGQH/Ne4bDJcj3Z22KCLyZctZ7lrJkWnz2b3rBqvgwdz57LotXngFQ0T+23iqu87X9erLr3FXzSrU8C9hx3tX4VQ1Enm5d2e8cI9lmYi8HGfHjqvZbEnM7RwZtlaF/ufe5886XGa0DAAAAAAAAADN/AM4A/uJVSs4qwAAAABJRU5ErkJggg==" alt="" />

静态方法和类方法

  • @staticmethod 表示下面的方法是静态方法
  • @classmethod 表示下面的方法是类方法
  • 静态方法中的参数没有self,
  • 类方法的参数也没有 self,但是必须有 cls 这个参数
# coding=utf-8
__metaclass__ = type
class StaticMethod:
@staticmethod
def foo():
print "This is static method foo()." class ClassMethod:
@classmethod
def bar(cls):
print "This is class method bar()."
print "bar() is part of class:",cls.__name__ if __name__ == "__main__":
static_foo = StaticMethod() #实例化
static_foo.foo() #实例调用静态方法
StaticMethod.foo() #通过类来调用静态方法
print "****************"
class_bar = ClassMethod() #实例化
class_bar.bar()
ClassMethod.bar()

执行结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAakAAAClCAIAAABp+5XDAAATVklEQVR4nO2dy5mjOhBGHcFE0GtvHEFH4A3bjsQJ3DRIYZIgiU7BccxdYECPkkoCBAjO+WYxbQupJKHfekDV7QYAAAAAAAAAR+PxeP77+Rn//f3Kuvrrr3Htv+fjUcjKivnz35P2SeT+6t7v9/vdve57m2LStEe0CuI8fp/P3/CQG4Tv+6dPPEf+kgq6APEWoH0m7q/u3TbBr5v2kCrz6o5oFYT4+o5ON3qxe/73Z/i7l8LvGeJ3+bGtNvWB2qefrfc/eDuA9l2BvZeE8QHpfbuv9v18//wLjcne1JkL8004hvYNbdj/m37VJjvsLY5dFBDtS2fYBXi/A02mJtiLQfs8NTFvwd+Hdcv+TuPD2CqS1TOS4LOAFf4Nxjwez9/nsy/OGhLfX87lg0l2cZYxkbEtWuIMOeNHQsikz+H7xzDAkT97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Yd7+Jb/NmF1BzYJIkMPdutmr0qGdrUS7Mr0PZ95Tns0e6GZv/wLKVozhX6oFxAj20aaukkwvUpEmjQRT/m8mIwR/u0hlI7a3FvKuS0z27V/Bg5lv/529S+c4RnKYXmBlK84rihD4oG9NigoZZpn+o3NGpaGtK6r7rAJgkWJWe/WzWH/HoD7sOOkdFBoW6pLDxLKcQtDMdgb2p83NAH5WKGbNNQy7Qv6FVVWEnNbOSMK/cPbDJ7GbHOvK9wNScjP2uS0aHmycKzlEIb0pWFPigXM2SbhrqA9m0W2GTclJzRoMv3+8pX097pG7/QdCfR/qDVp9K+6FLOr1t57Vt/3rdOQI8NGqrONW+GNccIbKIjLjfFgnerZqATjQ5K2hq49pq3ktAHKZQL6CE3lPUrvEZDSdlkmOluPAfac9WzjpCV+wc2WXR05gyEYGa7VVPXPv9+uNUYnqUE9YU+iF0WM2OFgB799vcGDRWIlTGzNfwzvbTeyijiLQxyn+0DmyxZ80qV8U8396um1ZHiU1KWeoUslHKbrjxAeJazUHfog5yjPwAAi1e973WgfQCwhJDAHVr4bmgfAFwOfcMEAAAAAAAAAAAAAAAgzuPx/PfzM/77+5V19ddf49p/z8ejkJUV8+e/5wXbZ7sgB58DMw7KwObx+3z+hofcIHzfP33iOfKXVNAFiLfA1dpn4wAvx3iH/4r8D5JK74nO8bQjAAAAAElFTkSuQmCC" alt="" />

封装和私有化

python 中私有化的方法也比较简单,就是在准备私有化的属性(包括方法、数据)名字前面加双下划线。

用了@property 之后就可以调用被封装的私有属性了。

#!/usr/bin/env python
#coding=utf-8
__metaclass__ = type class ProtectMe:
def __init__(self):
self.me = "Keven"
self.__name = "Rock" #封装 @property #调用私有属性
def name(self):
return self.__name if __name__ == "__main__":
p = ProtectMe()
print p.name