当前位置: 首页 > news >正文

梧州网站建设贝尔利seo推广灰色词

梧州网站建设贝尔利,seo推广灰色词,湖南省政府网站建设,加强 廉政网站建设类属性与方法 类的私有属性 __private_attrs:两个下划线开头,声明该属性为私有,不能在类的外部被使用或直接访问。在类内部的方法中使用时 self.__private_attrs。 类的方法 在类的内部,使用 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 <module>person.__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(age=25, 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, modulo=None):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

http://www.dinnco.com/news/6366.html

相关文章:

  • 广州朝阳网站建设珠海百度推广优化排名
  • 个人做网站需要注意什么百度网站优化
  • 南平做网站百度推广优化排名怎么收费
  • 国家疫情管理seo网站快速排名外包
  • 苏州吴江做网站公司北京seo优化
  • 微信公号嵌入网站开发宁波网站推广专业服务
  • 做美食推广的网站关键词优化是什么意思?
  • 幼儿园教育类网站模板下载口碑营销策略
  • 网站源码分享网络推广学校
  • 跨境电商网站建设流程图色盲色弱测试
  • 网页制作与网站建设填空题体验营销是什么
  • dreamweaver个人网站模板下载营销方案设计思路
  • 山西网站建设免费咨询免费做网站网站
  • 外贸网站制作哪家好廊坊优化外包
  • 电子商务网站建设管理实训报告互联网营销专家
  • 做移动网站首页软seo排名计费系统
  • 全网营销网站大数据获客系统
  • 免费装饰公司网站模板油烟机seo关键词
  • WordPress最强网站无锡百度推广开户
  • 影院网站建设成都推广系统
  • 公司做网站需要几个人宁德市人民医院
  • html5精美网站百度网站大全首页
  • 夏天做哪个网站致富百度不能搜的十大禁词
  • 襄阳做网站哪家好网络广告策划流程有哪些?
  • 网站可以用什么做网站标题seo外包优化
  • wordpress qq邮件订阅百度seo排名原理
  • 网站建设sem怎么做民生热点新闻
  • 网站风格主要包括最全bt搜索引擎
  • 在对方网站做友情链接seo在哪可以学
  • 网站建设dede模板免费share群组链接分享