(小甲鱼python)类和对象(I)总结 类的定义、self的用法

文章介绍了Python编程中的类和对象概念,包括类的定义(首字母大写,包含属性和方法),对象的实例化,以及`self`参数的使用。示例展示了类的方法调用和对象属性的访问与修改,强调了对象之间的独立性。同时,文章通过课后题探讨了对象的标识性和属性共享。
摘要由CSDN通过智能技术生成

一、类与对象

1.类
定义类时,类名首字母大写。
属性即写在类里面的变量,方法即写在类里面的函数。

例1:

class Turtle:
        head=1
        eyes=2
        legs=4
        shell=True

        def crawl(self):
                print("不积跬步无以至千里")

        def run(self):
                print("积极奔跑")

        def bite(self):
                print("bite,bite,bite")

        def eat(self):
                print("谁知盘中餐,;粒粒皆辛苦")

        def sleep(self):
                print("Zzzzzzz")

>>> t1=Turtle()    
>>> t1.head       #定义的方法,通过t1引用
1
>>> t1.legs
4
>>> t1.crawl()
不积跬步无以至千里
>>> t1.bite()
bite,bite,bite
>>> t1.sleep()
Zzzzzzz

将类Turtle赋值给t2

>>> t2=Turtle()
>>> t1.head
1
>>> t1.legs
4
>>> t2.head
1
>>> t2.legs
4
>>> t2.crawl()
不积跬步无以至千里
>>> 
>>> t2.legs=3    #修改t2变量的值
>>> t2.legs
3
>>> t1.legs     # 修改t2属性值,t1并不会改变
4
>>> 
>>> t1.mouth=1
>>> t1.mouth
1

用dir()函数,可以看到t1 比 t2多了一个mouth属性

>>> dir(t1)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'bite', 'crawl', 'eat', 'eyes', 'head', 'legs', 'mouth', 'run', 'shell', 'sleep']
>>> 
>>> dir(t2)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'bite', 'crawl', 'eat', 'eyes', 'head', 'legs', 'run', 'shell', 'sleep']
>>> 

2.self用法
类里面的参数都必须有一个self参数作为第一个参数。
例2:
没用self报错

>>> class C:
	def hello():
		print("你好")

		
>>> c=C()
>>> c.hello()
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    c.hello()
TypeError: hello() takes 0 positional arguments but 1 was given
>>> 

例3:

>>> class C:
	def get_self(self):
		print(self)

		
>>> c=C()
>>> c.get_self()        # 类c的实例化对象
<__main__.C object at 0x000001F8EF836E80>
>>> c
<__main__.C object at 0x000001F8EF836E80>
>>> 

课后题:
1.判断

>>> class C:
...     x = 250
...        
>>> c1 = C()
>>> c2 = C()
>>> c1 is c2
False
>>> c1 == c2
False
>>> c1.x is c2.x
True
>>> c1.x == c2.x
True

解析:虽然是基于统一个类的两个对象,但它们并不相同,也并不相等。但它们所拥有的 x 属性,确都是来自于同一个类 C 的。
2.请问下面代码中,self 参数的值什么?为什么方法定义的时候有 self 参数,但在调用中却无需传递实参?

>>> class C:
...     def func(self):
...         print("Hi FishC~")
...
>>> c = C()
>>> c.func()
Hi FishC~

答:在上面代码中,self 参数的值是对象 c 自身;因为 self 参数是对象访问方法的时候自动进行传递的,所以不需要我们进行显式传递。
解析:
同一个类可以生成无数多个对象,那么当我们在调用类里面的一个方法的时候,Python 如何知道到底是哪个对象在调用呢?
没错,就是通过这个 self 参数传递的信息。
所以,类中的每一个方法,默认的第一个参数都是 self。

题目来自小甲鱼类与对象

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值