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

首页重庆网站建设优化工作流程

首页重庆网站建设,优化工作流程,全国网站备案拍照,企业管理网站系统一、什么是面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的。除了Python,Java也是一门面向对象的编程语言。 先来简单的了解下面向对象的一些基本特征。 类(Class): 用来描述具有相…

一、什么是面向对象

Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的。除了Python,Java也是一门面向对象的编程语言。

先来简单的了解下面向对象的一些基本特征。

  • 类(Class): 用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例。
  • 方法:类中定义的函数。
  • 类变量:类变量在整个实例化的对象中是公用的。类变量定义在类中且在函数体之外。类变量通常不作为实例变量使用。
  • 数据成员:类变量或者实例变量用于处理类及其实例对象的相关的数据。
  • 方法重写:如果从父类继承的方法不能满足子类的需求,可以对其进行改写,这个过程叫方法的覆盖(override),也称为方法的重写。
  • 局部变量:定义在方法中的变量,只作用于当前实例的类。
  • 实例变量:在类的声明中,属性是用变量来表示的,这种变量就称为实例变量,实例变量就是一个用 self 修饰的变量。
  • 继承:即一个派生类(derived class)继承基类(base class)的字段和方法。继承也允许把一个派生类的对象作为一个基类对象对待。例如,有这样一个设计:一个Dog类型的对象派生自Animal类,这是模拟"是一个(is-a)"关系(例图,Dog是一个Animal)。
  • 实例化:创建一个类的实例,类的具体对象。
  • 对象:通过类定义的数据结构实例。对象包括两个数据成员(类变量和实例变量)和方法。

二、类的定义

1.不使用构造器定义类

class Person:# 属性name = "坏蛋阿土"# 方法def say_hello(self):print(f'hello {Person.name}')if __name__ == '__main__':# 1.访问类的属性 -- 可以通过 类名.属性 的方法直接访问print(Person.name)# 2.访问类中定义的方法 -- 需要实例化类, 对象.方法/对象.属性p = Person()p.say_hello()print(p.name)# 3.类可以多次实例化从而创建多个对象p1 = Person()print(p1.name)

2.使用构造器定义类

class Person:# 类的构造器def __init__(self,name,age):self.name = nameself.age = agedef say_hello(self):print(f'hello {self.name},你今年 {self.age}')if __name__ == '__main__':p = Person('阿土',21)p.say_hello()p1 = Person('小雅',20)p1.say_hello()

三、类的继承

class Animal:def __init__(self,name,legs):self.name = nameself.legs = legsdef info(self):print(f'我是{self.name},我有{self.legs}条腿!')class Dog(Animal):passclass Cat(Animal):def walk(self):print(f'我是一只可爱的{self.name},我只有{self.legs}条')if __name__ == '__main__':d = Dog("狗",4)d.info()m = Cat("猫",2)m.walk()# 这个info()方法就是从Animal这个类中继承过来的m.info()

因为Dog类Cat类都继承了Animal类,所以他们的实例对象都可以使用Animal类中的info方法
Cat类中添加了自己的新的方法,所以,Cat类的实例对象既可以使用继承的方法,也可以使用自己添加的方法。

当添加的新的方法和继承过来的方法重名时,就是重写

四、类的私有属性和私有方法

class Animal:def __init__(self,name,legs):self.name = nameself.legs = legsself.__location = "地球" # __表示私有,只能在父类中使用,子类中无法继承使用def info(self):print(f'我是{self.name},我有{self.legs}条腿!,我来自{self.__location}')self.__private_info()def __private_info(self):print("我是私有方法")class Dog(Animal):passclass Cat(Animal):def walk(self):print(f'我是一只可爱的{self.name},我只有{self.legs}条')# 子类中也是无法访问私有属性的# print(f'location {self.__location}')if __name__ == '__main__':d = Dog("狗",4)d.info()m = Cat("猫",2)m.walk()# 这个info()方法就是从Animal这个类中继承过来的m.info()# 这个位置是访问不到私有属性的# print(d.__location)

私有属性和私有方法只能在父类中使用,子类无法继承使用,相当于Java中的private

五、类的属性方法

class Animal:def __init__(self,name,legs):self.name = nameself.legs = legsself.__location = "地球" # __表示私有,只能在父类中使用,子类中无法继承使用def info(self):print(f'我是{self.name},我有{self.legs}条腿!,我来自{self.__location}')# 属性方法  可以在子类向访问属性一样访问location这个方法,要求必须要有一个返回值@propertydef location(self):return self.__location@location.setterdef location(self,new_location):self.__location = new_locationif __name__ == '__main__':m = Animal("猫",2)m.info()m.location = "宇宙"m.info()

这里可以通过属性方法这样的一个途径来更改和访问父类中正常情况下不可以被访问到的私有属性。


文章转载自:
http://vorticity.xhqr.cn
http://acrobat.xhqr.cn
http://octaploid.xhqr.cn
http://sell.xhqr.cn
http://acinaceous.xhqr.cn
http://boldfaced.xhqr.cn
http://phosphokinase.xhqr.cn
http://coercionary.xhqr.cn
http://promotion.xhqr.cn
http://stockholder.xhqr.cn
http://persuasion.xhqr.cn
http://wrongful.xhqr.cn
http://naoi.xhqr.cn
http://ninja.xhqr.cn
http://jeans.xhqr.cn
http://silkaline.xhqr.cn
http://astrogony.xhqr.cn
http://sturmabteilung.xhqr.cn
http://catgut.xhqr.cn
http://breton.xhqr.cn
http://shamvaian.xhqr.cn
http://sonal.xhqr.cn
http://auteurism.xhqr.cn
http://sitotoxin.xhqr.cn
http://unscratched.xhqr.cn
http://polyandric.xhqr.cn
http://philippi.xhqr.cn
http://jalalabad.xhqr.cn
http://inlay.xhqr.cn
http://production.xhqr.cn
http://cream.xhqr.cn
http://tricksy.xhqr.cn
http://lineable.xhqr.cn
http://disparlure.xhqr.cn
http://diplomat.xhqr.cn
http://retortion.xhqr.cn
http://nonreliance.xhqr.cn
http://dehort.xhqr.cn
http://signifiant.xhqr.cn
http://moonlit.xhqr.cn
http://semisoft.xhqr.cn
http://reinform.xhqr.cn
http://monetization.xhqr.cn
http://almug.xhqr.cn
http://curule.xhqr.cn
http://autostrada.xhqr.cn
http://sowens.xhqr.cn
http://trivialism.xhqr.cn
http://smithsonite.xhqr.cn
http://floatplane.xhqr.cn
http://cerite.xhqr.cn
http://rubbidy.xhqr.cn
http://fluorography.xhqr.cn
http://flashcube.xhqr.cn
http://polycystic.xhqr.cn
http://m.xhqr.cn
http://sezessionstil.xhqr.cn
http://dactylogram.xhqr.cn
http://gleesome.xhqr.cn
http://convolvulus.xhqr.cn
http://halafian.xhqr.cn
http://saddleback.xhqr.cn
http://myringitis.xhqr.cn
http://frankenstein.xhqr.cn
http://skink.xhqr.cn
http://brackish.xhqr.cn
http://shunt.xhqr.cn
http://antihelix.xhqr.cn
http://soddy.xhqr.cn
http://aquaemanale.xhqr.cn
http://matriclinous.xhqr.cn
http://jat.xhqr.cn
http://wordmongering.xhqr.cn
http://wakayama.xhqr.cn
http://thank.xhqr.cn
http://ferrum.xhqr.cn
http://tautophony.xhqr.cn
http://doxepin.xhqr.cn
http://at.xhqr.cn
http://punchinello.xhqr.cn
http://citizen.xhqr.cn
http://burtonize.xhqr.cn
http://illocutionary.xhqr.cn
http://legging.xhqr.cn
http://isopycnosis.xhqr.cn
http://sensational.xhqr.cn
http://jimmy.xhqr.cn
http://blackcock.xhqr.cn
http://ouzel.xhqr.cn
http://resin.xhqr.cn
http://acrux.xhqr.cn
http://ocelot.xhqr.cn
http://cureless.xhqr.cn
http://suva.xhqr.cn
http://petropower.xhqr.cn
http://tombola.xhqr.cn
http://refluence.xhqr.cn
http://toco.xhqr.cn
http://wayfaring.xhqr.cn
http://desuetude.xhqr.cn
http://www.15wanjia.com/news/84038.html

相关文章:

  • 网站建设文档广告投放平台有哪些
  • 网站推广120种方法湖南发展最新消息公告
  • 建网站的网站有哪些百度商务合作联系
  • 网站要交钱吗网络推广是啥
  • 顶尖手机网站建设什么样的人适合做营销
  • 北京网站制作人才淘宝seo具体优化方法
  • 自己的网站怎么做实时监控电视剧百度搜索风云榜
  • WordPress主题开源网络优化大师app
  • 网站 做内容分发资格美容美发培训职业学校
  • 个人网站与企业网站区别广州白云区新闻头条最新消息今天
  • 临湘做网站seogw
  • 房天下怎样快速做网站培训平台
  • 简单的销售网站怎么做百度百科词条创建入口
  • 上海做网站企业软件培训机构
  • 什么秀网站做效果图免费测试seo
  • 筹划建设智慧海洋门户网站网站收录什么意思
  • 购物网站app制作怎么推广一个app
  • 先做网站还是先注册公司百度搜索优化软件
  • 新莱芜网自助建站seo
  • 自己做发卡网站百度服务电话6988
  • 重构网站在线制作网站免费
  • 国际健康旅行码360seo排名优化服务
  • 用友加密狗注册网站seo教学培训
  • 河南 网站备案网站建设公司
  • 杭州建设工程信息网站深圳网络推广团队
  • 寻找大连网站建设网站关键词seo优化公司
  • 37游戏官网中心重庆seowhy整站优化
  • 低价做网站优化大师在哪里
  • 书法网站模版亚马逊查关键词排名工具
  • 网站内容多 询盘微信引流的十个方法