Python006——类和对象

1.函数和方法,类和对象

  • 方法跟函数其实几乎完全一样,但有一点区别是方法默认有一个 self 参数。
  • 类名要大写,函数名小写

2.调用类属性

  • 方法中对属性的引用形式需加上 self,如 self.name
  • 方法中对类的静态属性的引用形式为”类名.属性名”
  • 使用self则需要实例化对象
class Person:    
   name = '鱼'        
   def printName(self):       
       print(self.name)
p=Person()
p.printName()

3.如果属性的名字和方法相同时,属性会把方法覆盖掉

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

4.类的静态方法

  • 静态方法是类的特殊方法,静态方法只需要在普通方法的前边加上 @staticmethod 修饰符即可。
  • 静态方法最大的优点是:不会绑定到实例对象上,换而言之就是节省开销。
  • 静态方法并不需要 self 参数,因此即使是使用对象去访问,self 参数也不会传进去。
class C:
        @staticmethod  # 该修饰符表示 static() 是静态方法
        def static(arg1, arg2, arg3):
            print(arg1, arg2, arg3, arg1 + arg2 + arg3)
        def nostatic(self):
                print("I'm the f**king normal method!")


>>> c1 = C()
>>> c2 = C()
# 静态方法只在内存中生成一个,节省开销
>>> c1.static is C.static
True
>>> c1.nostatic is C.nostatic
False
>>> c1.static
<function C.static at 0x03001420>
>>> c2.static
<function C.static at 0x03001420>
>>> C.static
<function C.static at 0x03001420>
# 普通方法每个实例对象都拥有独立的一个,开销较大
>>> c1.nostatic
<bound method C.nostatic of <__main__.C object at 0x03010590>>
>>> c2.nostatic
<bound method C.nostatic of <__main__.C object at 0x032809D0>>
>>> C.nostatic
<function C.nostatic at 0x0328D2B8>

5 . 如何屏蔽父类中的方法?

答:如果子类中定义与父类同名的方法或属性,则会自动覆盖父类对应的方法和属性。 覆盖父类方法,例如将函数体内容写 pass,这样调用 fly 方法就没有任何反应了。

6.使用 super() 函数解决继承问题

import random as r
class Fish:        
   def __init__(self):                
       self.x = r.randint(0, 10)                
       self.y = r.randint(0, 10)        
   def move(self):                
       self.x -= 1                
       print("我的位置是:", self.x, self.y)
class Shark(Fish):        
   def __init__(self):                
      #使用super函数 
      #(或者:Fish.__init__(self) ,调用未绑定的父类方法)                 
      super().__init__()                
      self.hungry = True        
   def eat(self):                
      if self.hungry:                        
         print("吃货的梦想就是天天有吃的^_^")
         self.hungry = False                
      else:                        
         print("太撑了,吃不下了")
>>> shark = Shark()
>>> shark.move()
我的位置是: 6 2

7.hasattr、getattr、setattr、delattr、property

class C:	
  def __init__(self, x = 0):
     self.x = x 		
c1 = C()
hasattr(c1, "x")
True
getattr(c1, 'x')
0
setattr(c1, 'y', '来自江南的你')
'来自江南的你'

>>> class C:
	def __init__(self, size = 10):
		self.size = size
	def getSize(self):
		return self.size
	def setSize(self, value):
		self.size = value
	def delSize(self):
		del self.size
	x = property(getSize, setSize, delSize)
>>> c1 = C()
>>> c1.x
10
>>> c1.getSize()
10
>>> c1.x = 18
>>> c1.getSize()
18
>>> c1.setSize(20)
>>> c1.x
20
>>> del c1.x
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值