浙江天奥建设集团网站,网站布局选择,优创智汇高端网站建设电话怎么样,wordpress修改作者链接今天学习的内容#xff1a;1.面向对象编程的概念1)面向对象的三个基本特征#xff1a;封装、继承、多态2)类和对象是面向对象编程的2个主要方面。类使用class关键字创建。类的域和方法被列在一个缩进块中。2.类[rootreed 0504]# cat simpleclass.py#!/usr/bin/pythonclass Pe…今天学习的内容1.面向对象编程的概念1)面向对象的三个基本特征封装、继承、多态2)类和对象是面向对象编程的2个主要方面。类使用class关键字创建。类的域和方法被列在一个缩进块中。2.类[rootreed 0504]# cat simpleclass.py#!/usr/bin/pythonclass Person:pass #an empty block# print my name is reedpPerson()print p[rootreed 0504]# ./simpleclass.py3.对象的方法[rootreed 0504]# cat method.py#!/usr/bin/pythonclass Person:def sayHi(self):print hello,my name is reedpPerson()p.sayHi()[rootreed 0504]# ./method.pyhello,my name is reed4.__init__方法__init__方法在类的一个对象被建立时马上运行。这个方法可以用来对你的对象做一些你希望的 初始化 。注意这个名称的开始和结尾都是双下划线。[rootreed 0504]# cat class_init.py#!/usr/bin/pythonclass Person:def __init__(self,name):self.namenamedef sayHi(self):print hello,my name is,self.namepPerson(reed)p.sayHi()[rootreed 0504]# ./class_init.pyhello,my name is reed5.类与对象的方法[rootreed 0504]# cat objvar.py#!/usr/bin/python#filename:objvar.pyclass Person:population0def __init__(self,name):self.namenameprint (initializing %s)%self.name#when this person is created,he/she adds to the populationPerson.population1def __del__(self):print %s says bye%self.nameself.__class__.population-1if self.__class__.population0:print i am the last oneelse:print there are still %d people left%Person.populationdef sayHi(self):print hi,my name is %s%self.namedef howMany(self):if Person.population1:print i am the only person hereelse:print we have %d persons here%Person.populationswaroopPerson(swaroop)swaroop.sayHi()swaroop.howMany()print --------------reedPerson(reed)reed.sayHi()reed.howMany()print --------------deerPerson(deer)deer.sayHi()deer.howMany()print --------------swaroop.sayHi()swaroop.howMany()[rootreed 0504]# ./objvar.py(initializing swaroop)hi,my name is swaroopi am the only person here--------------(initializing reed)hi,my name is reedwe have 2 persons here--------------(initializing deer)hi,my name is deerwe have 3 persons here--------------hi,my name is swaroopwe have 3 persons heredeer says byethere are still 2 people leftswaroop says byethere are still 1 people leftreed says byei am the last one6.继承继承完全可以理解成类之间的类型和子类型的关系[rootreed 0504]# cat inherit.py#!/usr/bin/python#filenameinherit.pyclass SchoolMember:def __init__(self,name,age):self.namenameself.ageageprint Initialized SchoolMember:%s%self.namedef tell(self):print Name:%sAge:%s%(self.name,self.age)class Teacher(SchoolMember):def __init__(self,name,age,salary):SchoolMember.__init__(self,name,age)self.salarysalaryprint Initalized Teacher:%s%self.namedef tell(self):SchoolMember.tell(self)print Salary:%d%self.salaryclass Student(SchoolMember):def __init__(self,name,age,marks):SchoolMember.__init__(self,name,age)self.marksmarksprint Initialized Student:%s%self.namedef tell(self):SchoolMember.tell(self)print Marks:%d%self.markstTeacher(Mrs.Reed,40,30000)t.tell()sStudent(Lemon,22,75)s.tell()[rootreed 0504]# ./inherit.pyInitialized SchoolMember:Mrs.ReedInitalized Teacher:Mrs.ReedName:Mrs.ReedAge:40Salary:30000Initialized SchoolMember:LemonInitialized Student:LemonName:LemonAge:22Marks:757__del__方法在对象消逝的时候被调用