python3-类
来源:互联网 发布:经济大数据分析书籍 编辑:程序博客网 时间:2024/04/30 07:23
1、概述
类是用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例。
程序举例:
class test(): def __init__(self,one,two,three): self.one=one self.two=two self.three=three def add(self): SUM=self.one+self.two+self.three return SUMif __name__ == '__main__': print(test(1,2,3).add())>>>6
要点:
1、类的方法必须包含参数self,且为第一个参数。
2、_init_()可以包含参数,之后在类的实例化时设定参数值。
3、类的方法即定义一个函数(def),在之后的类调用时直接引用即可。
2、类的继承
2.1、单继承
程序举例:
class father(): def __init__(self,index_1,index_2,index_3): self.index_1=index_1 self.index_2=index_2 self.index_3=index_3 def output(self): passclass son_1(father): def __init__(self,index_1,index_2,index_3,index_4): father.__init__(self,index_1,index_2,index_3) self.index_4=index_4 def outPUT(self): print('the first number is %d,the forth number is %d'%(self.index_1,self.index_4))if __name__ == '__main__': son_1(1,2,3,4).outPUT()>>>the first number is 1,the forth number is 4
要点:
子类有_init_()函数,父类的_init_()未调用,则默认调用子类的_init_()函数。
2.2、多继承
程序举例:
class father(): def __init__(self,index_1,index_2,index_3): self.index_1=index_1 self.index_2=index_2 self.index_3=index_3 def output(self): print('the first number is %d \nthe twice number is %d\nthe third number is %d'%(self.index_1,self.index_2,self.index_3))class son_1(father): def __init__(self,index_1,index_2,index_3,index_4): father.__init__(self,index_1,index_2,index_3) self.index_4=index_4 def outPUT(self): print('the first number is %d \nthe forth number is %d'%(self.index_1,self.index_4))class son_2(son_1,father): def __init__(self,index_1,index_2,index_3,index_4,index_5): self.index_5=index_5 father.__init__(self, index_1, index_2, index_3) son_1.__init__(self, index_1, index_2, index_3,index_4) def OUTput(self): print('the third number is %d\nthe forth number is %d\nthe fifth number is %d'%(self.index_3,self.index_4,self.index_5))if __name__ == '__main__': print('子类调用自身函数:') son_2(8,6,4,5,2).OUTput() print('子类调用一个父类的函数:') son_2(4,6,7,9,2).outPUT() print('子类调用另一个父类的函数:') son_2(7,9,5,7,6).output()>>>子类调用自身函数:the third number is 4the forth number is 5the fifth number is 2子类调用一个父类的函数:the first number is 4 the forth number is 9子类调用另一个父类的函数:the first number is 7 the twice number is 9the third number is 5
要点:
若子类未定义_init_方法时,程序会执行调用的首个基类的_init_方法。
2.3、方法重写
class father(): def __init__(self,index_1): self.index_1=index_1 def way(self): print ("father's number is %d"%self.index_1)class son_1(father): def __init__(self,index_1,index_2): self.index_2=index_2 father.__init__(self, index_1) def way(self): print ("father's number is %d\nson_1's number is %d"%(self.index_1,self.index_2))class son_2(son_1): def __init__(self,index_1,index_2,index_3): self.index_3=index_3 son_1.__init__(self, index_1, index_2) def way(self): print ("father's number is %d\nson_1's number is %d\nson_2's number is %d"%(self.index_1,self.index_2,self.index_3))if __name__ == '__main__': print('用子类son_1的函数重写父类father:') example_2=son_1(8,6) example_2.way() print('用子类son_2的函数重写父类son_1:') example_1=son_2(8,6,4) example_1.way()>>>用子类son_1的函数重写父类father:father's number is 8son_1's number is 6用子类son_2的函数重写父类son_1:father's number is 8son_1's number is 6son_2's number is 4
要点:
当父类的函数不能满足需要时,可以在子类定义一个相同的函数对父类的函数进行重写。
阅读全文
0 0
- python3-类
- Python3 类
- python3类
- Python3 - 类和对象
- python3-类与对象
- Python3 (入门5) 类
- python3 类的学习
- Python3 4类函数
- Python3之元类
- python3-类的继承
- Python3 (入门5) 类
- python3 类的继承
- Python3.x 类
- python3
- Python3
- python3
- Python3
- Python3
- Tomcat7目录结构详解(非常详细)
- Maven的oracle的jar包导入
- 动态内存管理总结(malloc、calloc、realloc的区别),以及内存泄漏常见问题
- Python3使用csv模块csv.writer().writerow()保存csv文件,产生空行的问题
- WebSocket实现android消息推送
- python3-类
- hp主机系统主控台Console的说明与设置
- 程序员面试金典:魔术索引II、集合的子集
- 网络课堂工具
- HotSpot的热点代码探测技术
- 数据库最大连接池Max Pool Size
- git命令
- 网络与分布式计算复习 名词解释与简答
- tensorflow LSTM + CTC实现端到端OCR