wordpress 多站点共享,电子商务网站建设与管理的论文,网站认证要钱,网站设计和美工一样吗类属性与方法
类的私有属性
__private_attrs#xff1a;两个下划线开头#xff0c;声明该属性为私有#xff0c;不能在类的外部被使用或直接访问。在类内部的方法中使用时 self.__private_attrs。 类的方法
在类的内部#xff0c;使用 def 关键字来定义一个方法#xf…类属性与方法
类的私有属性
__private_attrs两个下划线开头声明该属性为私有不能在类的外部被使用或直接访问。在类内部的方法中使用时 self.__private_attrs。 类的方法
在类的内部使用 def 关键字来定义一个方法与一般函数定义不同类方法必须包含参数 self且为第一个参数self 代表的是类的实例。 self 的名字并不是规定死的也可以使用 this但是最好还是按照约定是用 self。 类的私有方法
__private_method两个下划线开头声明该方法为私有方法只能在类的内部调用 不能在类的外部调用。self.__private_methods。 实例
类的私有属性实例如下
class Person:name __nickname def __init__(self, name, nickname):self.name nameself.__nickname nicknamedef say_hello(self):print(你好我叫 self.name 我的外号是 self.__nickname)person Person(张三, 狗剩子)
person.say_hello()
print(大名叫 person.name)
# 报错私有属性不能在类外部使用
print(外号叫 person.__nickname)
执行以上程序输出结果为
Traceback (most recent call last):
你好我叫张三我的外号是狗剩子File F:/Python教程/project2/com/bjsxt/mypy/面向对象-对象.py, line 191, in module
大名叫张三print(外号叫 person.__nickname)
AttributeError: Person object has no attribute __nickname
类的私有方法实例如下
class Person:name __nickname def __init__(self, name, nickname):self.name nameself.__nickname nicknamedef say_hello(self):print(你好我叫 self.name 我的外号是 self.__nickname)def __say_hello(self):return self.__nicknamedef say_hello2(self):print(self.__nickname)person Person(张三, 狗剩子)
person.say_hello()
print(大名叫 person.name)
# 报错私有属性不能在类外部使用
# print(外号叫 person.__nickname)
person.say_hello2()
# 报错不允许在类外部调用私有方法
person.__say_hello()
以上实例执行结果
Traceback (most recent call last):File F:/Python教程/project2/com/bjsxt/mypy/面向对象-对象.py, line 219, in moduleperson.__say_hello()
你好我叫张三我的外号是狗剩子
AttributeError: Person object has no attribute __say_hello
大名叫张三
狗剩子
类的专有方法
__init__ : 构造函数在生成对象时调用
__del__ : 析构函数释放对象时使用
__add__: 加运算
__sub__: 减运算
__mul__: 乘运算
__truediv__: 除运算
__mod__: 求余运算
__pow__: 乘方
class User:def __new__(cls, *args, **kwargs):如果 __new__方法不返回值或者说返回 None__init__ 将不会得到调用因为实例对象都没创建出来调用 init 也没什么意义。:param args::param kwargs::return:print(调用了__new__方法)# 返回一个实例对象,这个实例对象会传递给 __init__ 方法中定义的 self 参数# 以便实例对象可以被正确地初始化。return super(User, cls).__new__(cls)def __init__(self, age, name):python 规定__init__只能返回 None 值__init__方法中除了self之外定义的参数都将与 __new__方法中除cls参数之外的参数是必须保持一致或者等效。self.name nameself.age ageprint(生成对象时调用)def __del__(self):print(析构函数释放对象时调用)def showparams(self):print(self.__dict__)user User(age25, name张三)
输出结果
调用了__new__方法
生成对象时调用
析构函数释放对象时调用
运算符重载
Python同样支持运算符重载我们可以对类的专有方法进行重载实例如下
class Vector:xpos 0ypos 0def __init__(self, xpos, ypos):self.xpos xposself.ypos yposdef __add__(self, other):return Vector(self.xpos other.xpos, self.ypos other.ypos)def __str__(self):return 横坐标 str(self.xpos) 纵坐标 str(self.ypos)def __sub__(self, other):return Vector(self.xpos - other.xpos, self.ypos - other.ypos)def __mul__(self, other):return Vector(self.xpos * other.xpos, self.ypos * other.ypos)def __truediv__(self, other):return Vector(self.xpos / other.xpos, self.ypos / other.ypos)def __mod__(self, other):return Vector(self.xpos % other.xpos, self.ypos % other.ypos)def __pow__(self, power, moduloNone):return Vector(self.xpos ** power, self.ypos ** power)v1 Vector(1, 3)
v2 Vector(4, 5)v v1 v2
print(v)v v1 - v2
print(v)v v1 * v2
print(v)v v1 / v2
print(v)v v1 % v2
print(v)v v1 ** 2
print(v)
以上代码执行结果如下所示:
横坐标5纵坐标8
横坐标-3纵坐标-2
横坐标4纵坐标15
横坐标0.25纵坐标0.6
横坐标1纵坐标3
横坐标1纵坐标9