python-类和对象

一:类和对象

python是一门纯粹的面向对象编程的语言(OO)

特征:封装、继承、多态

1:封装 举例

class Tuth:
    clolor = 'green'
    weight = 10
    legs = 4
    shell = True
    mouth = '大嘴'

    def climb(self):
        print('我在努力向前爬')

    def run(self):
        print('跑 跑 跑 飞奔而去')

    def bite(self):
        print('咬一口 试试')

    def eat(self):
        print('吃点东西吧')
        
调用:
>>> tt = Tuth()
>>> tt.eat()
吃点东西吧
>>> tt.bite()
咬一口 试试
>>> 

2:继承

>>> list2 = Mylists()
>>> list2,append(23)
>>> list2.append(233)
>>> list2.append(34)
>>> list2
[233, 34]

3:多态:不同对象对同一方法响应不同的行动

举例:老虎、兔子、人 跑的方式各不相同

>>> class A:
	def fun(self):
		print('我是A')

		
>>> class B:
	def fun(self):
		print('我是B')

		
>>> a = A()
>>> b = B()
>>> a.fun()
我是A
>>> b.fun()
我是B
>>> 

4:self:

4.1: 默认init版本

>>> class Ball:
	def setName(self, name):
		self.name = name
	def kick(self):
		print('我叫%s, 谁踢我,....' % self.name)

		
>>> a = Ball()
>>> a.setName('球Aa')
>>> b = Ball()
>>> b.setName('球BB')
>>> c = Ball()
>>> c.setName('球C')
>>> a.kick
<bound method Ball.kick of <__main__.Ball object at 0x1057edb00>>
>>> a.kick()
我叫球Aa, 谁踢我,....
>>> b.kick()
我叫球BB, 谁踢我,....
>>> c.kick()
我叫球C, 谁踢我,....
4.2:自定义init版本:

>>> class Ball:  #重写方法,初始化这个类
	def __init__(self, name):
		self.name = name
	def kick(self):
		print('我叫%s 谁 谁 谁 踢我' % self.name)

		
>>> b = Ball('牛肉')
>>> b.kick()
我叫牛肉 谁 谁 谁 踢我


5:公有 私有

默认上来说,对象的属性和方法都是公开公有的,可以通过.操作符进行访问

5.1:name mangling:名字改编  名字重造

在python中定义私有变量只需要在变量名或函数名前加上'__' 两个下划线,那么这个函数或变量就变成私有的了

>>> class Person:
	__name = "哈哈"
	def getNAme(self):
		return self.__name

	
>>> p = Person()
>>> p.__name
Traceback (most recent call last):
  File "<pyshell#91>", line 1, in <module>
    p.__name
AttributeError: 'Person' object has no attribute '__name'
>>> p.getName()
Traceback (most recent call last):
  File "<pyshell#92>", line 1, in <module>
    p.getName()
AttributeError: 'Person' object has no attribute 'getName'
>>> p.getNAme()       #虽然是私有,但是可以通过这种方式被外部调用
'哈哈'
>>> p._Person__name   #此方法一样
'哈哈'
>>> 

6:继承

class DerivedClassName(BaseClassName)  =》  DerivedClassName:子类     BaseClassName:父类、基类或者超类

>>> class Parent:
	def hello(self):
		print('正在调用父类的方法...')

		
>>> class Child(Parent):
	pass

>>> p = Parent()
>>> p.hello()
正在调用父类的方法...
>>> c = Child()
>>> c.hello
<bound method Parent.hello of <__main__.Child object at 0x1057edef0>>
>>> c.hello()
正在调用父类的方法...
>>> 
注意:如果子类定义与父类重名的方法或者属性,那么会自动覆盖对应父类的属性或者方法
看如下代码
正在调用父类的方法...
>>> class Child(Parent):
	def hello(self):
		print('正在调用子类的方法')

		
>>> c = Child()
>>> c.hello()
正在调用子类的方法
>>> c = Parent()
>>> c.hello()
正在调用父类的方法...
>>> 

比如。举例:覆盖父类的方法

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 Glodfish(Fish):
    pass
class Garp(Fish):
    pass

class Salmon(Fish):
    pass

class Shark(Fish):
    def __init__(self):
        self.hungry = True

    def eat(self):
        if self.hungry:
            print('吃货的梦想就是天天有的吃')
            self.hungry = False
        else:
            print('太撑了,吃不下了')
            
运行结果
>>> fish = Fish()
>>> fish.move()
我的位置是: 4 0
>>> glodfish = Glodfish()
>>> glodfish.move()
我的位置是: 8 7
>>> glodfish.move()
我的位置是: 7 7
>>> shark = Shark()
>>> shark.eat()
吃货的梦想就是天天有的吃
>>> shark.move()
Traceback (most recent call last):
  File "<pyshell#128>", line 1, in <module>
    shark.move()
  File "/Users/hongbaodai/Desktop/Untitled.py", line 9, in move
    self.x -= 1
AttributeError: 'Shark' object has no attribute 'x'  #可以看出这里是没找到方法,其实父类的init方法被覆盖了,所以这里找不到x了
>> 
解决方式

1:加入       Fish.__init__(self)方法

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 Glodfish(Fish):
    pass
class Garp(Fish):
    pass

class Salmon(Fish):
    pass

class Shark(Fish):
    def __init__(self):
        Fish.__init__(self)  #这里相当于重新初始化父类
        self.hungry = True

    def eat(self):
        if self.hungry:
            print('吃货的梦想就是天天有的吃')
            self.hungry = False
        else:
            print('太撑了,吃不下了')
            
结果:
>>> fish = Fish()
>>> shark = Shark()
>>> shark.move()
我的位置是: 9 7
>>> 
2:super函数  super().__init__()

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 Glodfish(Fish):
    pass
class Garp(Fish):
    pass

class Salmon(Fish):
    pass

class Shark(Fish):
    def __init__(self):

        super().__init__()   # 这里使用super方法,就是调用父类 优势:会给你自动找到父类的名字,
        self.hungry = True

    def eat(self):
        if self.hungry:
            print('吃货的梦想就是天天有的吃')
            self.hungry = False
        else:
            print('太撑了,吃不下了')
            
结果:
>>> shark = Shark()
>>> shark.move()
我的位置是: 8 9
6.1:多重继承

>>> class Base1:
	def fool1(self):
		print('我是base basem')

		
>>> class Base2:
	def fool2(self):
		print('我是base basem 2222')

		
>>> class C(Base1,Base2):
	pass

>>> c = C()
>>> c.fool1()
我是base basem
>>> c.fool2()
我是base basem 2222
>>> 










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值