首页 > 编程语言 >python-面向对象-类的继承-单继承

python-面向对象-类的继承-单继承

时间:2022-12-02 14:59:19浏览次数:31  
标签:... classmethod grandson 面向对象 继承 father son python id

1.继承是python面向对象的三大特性之一,是一种创建新类的方式,python中的继承,可以继承一个或者继承多个父类,新建的类被称之为派生类或者子类,被继承的类是父类,可以称之为基类,超类,继承是实现代码重用的重要方式。

#coding=utf-8;
class father(object):
    #父类初始化方法
    def __init__(self,id):
        self.id=id;
    #父类类方法
    @classmethod
    def classmethod_father(cls,id,where):
        print(id,"father working...",where);
    #父类静态方法
    @staticmethod
    def staticmethod_father(a,b,c):
        print(a,b,c,"father static method...");
    #父类普通方法
    def publicmethod_father(self,id,name):
        #id=self.id;
        #name=self.name;
        print("id:",id,"name:",name);

class son(father):
    def __init__(self,sonid):
        super().__init__(id);
        self.sonid=sonid;
    @classmethod
    def classmethod_son(cls,sonid,sondo):
        print("调用者:",sonid,"son classmethod_son")
    @staticmethod
    def staticmethod_son(who):
        print("调用者:",who,"son static method ...");
    def public_son(self,who):
        print("调用者:",who,"son public method...");
class grandson(son):
    def __init__(self,grandson_id):
        self.grandson_id=grandson_id;
    @classmethod
    def classmethod_grandson(cls,who):
        print("调用者",who,"grandson classmethod ...");
    @staticmethod
    def staticgrandson(who):
        print("调用者","grandson staticmethod...");
    def public_grandson(self,who):
        print("调用者",who,"grandson public...");
#设置一个变量
fathername="tom";
fatherid=123;
#father实例化
f=father(fatherid);
f.classmethod_father(fatherid,"北京");
f.staticmethod_father(111,222,333);
f.publicmethod_father(fatherid,fathername);
#father类调用
father.classmethod_father(888,"father类调用");
father.staticmethod_father("aaa","bbb","ccc");
father.publicmethod_father(1,100,"tom")
#son实例调用
s = son(12345);
s.classmethod_father(fatherid,"北京");
s.staticmethod_father(111,222,333);
s.publicmethod_father(fatherid,fathername);
s.classmethod_son(12345,"上海");
s.staticmethod_son("son")
s.public_son("son");
#grandson实例调用
gs=grandson("1234578");
gs.classmethod_grandson("grandson");
gs.staticgrandson("grandson");
gs.public_grandson("grandson");

运行结果:

123 father working... 北京
111 222 333 father static method...
id: 123 name: tom
888 father working... father类调用
aaa bbb ccc father static method...
id: 100 name: tom
123 father working... 北京
111 222 333 father static method...
id: 123 name: tom
调用者: 12345 son classmethod_son
调用者: son son static method ...
调用者: son son public method...
调用者 grandson grandson classmethod ...
调用者 grandson staticmethod...
调用者 grandson grandson public...

 

标签:...,classmethod,grandson,面向对象,继承,father,son,python,id
From: https://www.cnblogs.com/joyware/p/16944458.html

相关文章

  • Python NameError: name ‘xxx‘ is not defined
    错误:NameError:name'getopt'isnotdefined原因:没有引入模块,脚本里添加下面内容importgetopt错误:NameError:name'argv'isnotdefined原因:没有给main(......
  • python取整和取余
    python中求取余数用的是%,也就是百分号。a%b=c参考:https://zhuanlan.zhihu.com/p/412338505向下取整:int(a)参考:https://blog.csdn.net/u010087338/article/details......
  • Python基础之推导式
    ⼀、列表推导式作⽤:⽤⼀个表达式创建⼀个有规律的列表或控制⼀个有规律列表。列表推导式⼜叫列表⽣成式。1、示例创建⼀个0-10的列表1、while循环实现#1.准备⼀个空列表li......
  • python爬虫为什么很多公司都需要?
    python爬虫在如今大数据时代是越来越重要,却发现,都没有人总结Python爬虫可以用来做什么,从而导致学习Python爬虫的小伙伴略有点迷茫。1、学习爬虫,可以私人订制一个搜索引擎,并......
  • Python高级-mini-web框架、添加路由、MySQL功能-笔记
    此篇文章是接着上一篇文章写的​​《Python高级-WSGI、mini-web框架-笔记》​​下一篇​​《Python高级-mini-web框架、添加log日志、路由支持正则-笔记》​​1.miniweb框......
  • Python高级-mini-web框架、添加log日志、路由支持正则-笔记
    接着上一篇介绍mini-web框架​​《Python高级-mini-web框架、添加路由、MySQL功能-笔记》​​​8.mini-web框架-路由支持正则importpymysqlimporttimeimportosimportret......
  • Python怎样写个通用爬虫模板?
    其实经常写爬虫的程序员应该都知道,做一个爬虫工作无非就是三个步骤:下载数据、解析数据、保存数据。基本所有爬虫万变不离其宗,都是这样的套路。下面就是Python写出来一个完......
  • python图片转文字
    转载先将图片每个像素的hsv(色相,饱和度,明度)值转化为可读取对象再将图像按v用取平均值的方法进行压缩,最后根据压缩后图像的每个像素点的v值转换成字符进行输出scale......
  • VSCode中Debug远程Python程序,断点失效
    解决方法:找到调试要使用的python解释器路径Linux命令:whereispython我这里是/home/tao/anaconda/envs/spyketorchproject/bin/python3.10在VSCode中修改Debug配置文件......
  • python实操案例__02—利用prettytable库模拟高铁售票系统
    1问题描述设计一个简易售票系统,可循环购票并显示余票。2功能拆解2.1什么是prettytable?PrettyTable是python中的一个第三方库,可用来生成美观的ASCII格式的表格,本实操......