温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python面向对象中函数有什么用

发布时间:2021-11-01 17:20:26 来源:亿速云 阅读:187 作者:小新 栏目:编程语言

这篇文章主要介绍python面向对象中函数有什么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

面向对象

面向对象编程——Object Oriented Programming,简称OOP,是一种程序设计思想。OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数

class Car:     #静态字段,类属性     cars='车的种类之一'     def __init__(self,name):         #动态字段,实例属性         self.name=name car1=Car('宝马') print(car1.name) print(Car.cars) print(car1.cars)

定义1个类:class xxx:

初始化1个对象:def __init__(self,value)  

__init__ 是初始化的意思

定义Car(车)这个类,而宝马这个实例对象拥有 name属性

通过字典,一次性取出实例对象所有属性 __dict__

class People:     def __init__(self,name,sex,number,age):         self.name=name         self.sex=sex         self.number=number         self.age=age p1=People('SBharmel','man',1350023,16) print(p1.__dict__) #通过字典,一次性取出,对象所有属性 #输出结果: #{'name': 'SBharmel', 'number': 1350023, 'sex': 'man', 'age': 16}

从属关系

cars 属于 Car 类   类不能访问动态字段

name 属于 car1 对象      对象可以访问动态字段和静态字段

只有是从属关系,才能用 .xxx 方法获取

所以可以写 car.cars #获取静态字段 car1.name #获取动态字段

如果写成 car.name 则会报错

不能写的原因:

1.从属关系不同

2. 车这个类可能拥有多个类型如 宝马, 奔驰 他们两个的属性不一样,如价格,车速不同

#但是对象可以获取 静态字段 car1.cars

因为 car1 也属于 这个类 (car)

class car:     def __init__(self,name,speed,price):         self.name=name         self.speed=speed         self.price=price car1=car('宝马','130km/h','100w') car2=car('奔驰','140km/h','80w') print(car1.name)       #正确 print(car2.price)      #正确 print(car.speed)       #报错  ''' Traceback (most recent call last):   File "E:\workspace\day4\backend\class.py", line 23, in <module>     print(car.speed) AttributeError: type object 'car' has no attribute 'speed' '''

静态字段和动态字段

类创建的字段叫做 静态字段 cars

.sefl 创建的字段叫做 动态字段 name

def () 一般写在类里面的叫做方法,写在类之外的叫做函数

静态方法 使用类或对象名直接访问的方法

@staticmethod  #构造的函数()里面不传参数self,调用写(),类和对象都可以调用

类方法 使用类或对象名直接访问的方法

@classmethod   #构造的函数()里面不传参数self ,但要写cls调用写(),类和对象都可以调用

特性

@property      #构造的函数 ()里面要传参数 self,调用不用写(),仅对象可调用

 #仅为实例对象的特性,类调用无内容,相当于实例化特有的属性

class Student:         # 静态字段,类属性     classtype = "学生"     # 初始化 实例属性,动态字段     def __init__(self,name,age):         self.name = name         self.age = age     # 动态方法,实例方法     def eat(self):         print("学生在吃饭")     # 静态方法     @staticmethod     def staticfun():         print("这是一个静态方法")     # 类方法     @classmethod     def cm(cls):         print("这是一个类方法")     # 特性     @property     def properfun(self):         print("properfun:我的特性,是一个学生,劝你好自为之") print("-----------类可以调用的类容------------------------------------") print(Student.classtype) Student.staticfun() Student.cm() Student.properfun   # 这么写不报错,但也不打印,Student.properfun() 报错 print(Student.__dict__) print("-----------实例化对象后可以调用的类容--------------------------") stu1 = Student("lee",18) print(stu1.classtype) print(stu1.name) print(stu1.age) stu1.eat() Student.eat(stu1) stu1.staticfun() stu1.cm() stu1.properfun  #特性调用不用加(),它相当于实例化的一个属性 print(stu1.__dict__) ''' 打印的结果 -----------类可以调用的类容------------------------------------ 学生 这是一个静态方法 这是一个类方法 {'__module__': '__main__', 'classtype': '学生', '__init__': <function Student.__init__ at 0x000002323C96A1F0>, 'eat': <function Student.eat at 0x000002323C96A280>, 'staticfun': <staticmethod object at 0x000002323C966EB0>, 'cm': <classmethod object at 0x000002323C966EE0>, 'properfun': <property object at 0x000002323C95CD60>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None} -----------实例化对象后可以调用的类容-------------------------- 学生 lee 18 学生在吃饭 这是一个静态方法 这是一个类方法 properfun:我的特性,是一个学生,劝你好自为之 {'name': 'lee', 'age': 18} '''

以上是“python面向对象中函数有什么用”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI