设为首页 加入收藏

TOP

python入门基础(14)--类的属性、成员方法、静态方法以及继承、重载(二)
2023-09-23 15:43:38 】 浏览:141
Tags:python 成员方 方法以 重载
调用类中的方法edit_point() self.disp_point() #调用类中的方法disp_point() def edit_point(self,x,y): #定义一个方法 self.x += x self.y += y def disp_point(self): #定义一个方法 print("当前位置:(%d,%d)"% (self.x,self.y)) class Cat(Animal): #定义一个类Cat类,继承类Animal def __init__ (self,x=0,y=0,color="white"): #定义一个构造方法 self.x=x self.y=y self.color=color #新增加一个颜色 self.disp_point() #构造函数中调用类中的方法disp_point() def run(self,x,y): #定义一个方法run() x,y = coord_chng(x,y) #调用全局函数,坐标变换 self.edit_point(x,y) #调用类中的方法edit_point() self.disp_point() #调用类中的方法disp_point() cat_a= Cat() #实例化Cat()类 cat_a.move(2,4) #调用cat_a实例的方法move() cat_a.move(-9,6) #调用cat_a实例的方法move() cat_a.move(12,-16)#调用cat_a实例的方法move() cat_a.run(12,-16) #调用cat_a实例的方法run()

注意红色注释部分,move()这个方法是Cat类没有的,但在Animal中有,Cat类通过继承Animal类,获得了Animal中move方法。

同样,也可以将上篇中的Person类继承Animal类的功能,并进行少量修改,全部代码如下:

# -*- coding: utf-8 -*-
"""
Created on Sun Sep 17 19:59:24 2023

@author: Administrator
"""

def coord_chng(x,y):        #定义一个全局函数,模拟坐标值变换
    return (abs(x),abs(y))  #将x,y 值求绝对值后返回

class Animal:
    def __init__ (self,lifeva lue): 
        self.lifeva lue=lifeva lue
        
    def info(self):      #定义一个方法
         print("当前位置:(%d,%d)"% (self.x,self.y))   
    def crawl(self,x,y):
         self.x=x
         self.y=y
         print("爬行……")
         self.info()  
def move(self,x,y): #定义一个方法move() x,y = coord_chng(x,y) #调用全局函数,坐标变换 self.edit_point(x,y) #调用类中的方法edit_point() self.disp_point() #调用类中的方法disp_point() def edit_point(self,x,y): #定义一个方法 self.x += x self.y += y def disp_point(self): #定义一个方法 print("当前位置:(%d,%d)"% (self.x,self.y)) class Cat(Animal): #定义一个类Cat类,继承类Animal def __init__ (self,x=0,y=0,color="white"): #定义一个构造方法 self.x=x self.y=y self.color=color #新增加一个颜色 self.disp_point() #构造函数中调用类中的方法disp_point() class Person(Animal): #定义一个类Person类,继承类Animal def __init__(self,new_name,x,y,new_age,new_hight,new_weight,edu_certification,new_job): #self.name = "Tom" self.x=x self.y=y self.name=new_name self.age=new_age self.hight=new_hight self.weight=new_weight self.edu_certif=edu_certification self.job=new_job def eat(self): # 哪一个对象调用的方法,self就是哪一个对象的引用 print("%s 爱吃鱼" % self.name) def drink(self): print("%s 要喝水" % self.name) def walk(self): print("%s今天步行了10公里"%self.name) def run(self): # 必须返回一个字符串 return "%s跑了10公里,用时56分钟。" % self.name def sing(self): # 必须返回一个字符串 return "%s唱了一首《我的中国心》" % self.name def working(self): # 必须返回一个字符串 return "%s工作了很久!" % self.name tom=Person("Tom",10,20,24,175,70,"bachelor","writer") #lucy=Person("Lucy") #lily=Person("Lily") print("%s的身高是%s cm\n" %(tom.name,tom.hight)) print("%s的体重是%sKG\n" %(tom.name,tom.weight)) print(tom.sing()) print(tom.run()) tom.move(2,4) tom.move(10,-12) print("------------------显示分隔线--------------------\n") cat_a= Cat() #实例化Cat()类 cat_a.move(2,4) #调用cat_a实例的方法move() cat_a.move(12,-16)#调用cat_a实例的方法move()

代码中红色部分为修改。可以看出,tom继承Animal类后,也继承了move()方法。运行结果:

 

四、类的多重继承

python编程中,一个类不仅可以继承一个类,还可以继承多个类,即多重继承。和上述所讲继承一样,只不过在括号中,用“,”分隔开。可以当作思考题,自动动手,比如利用上述的person类,C

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇安装及认识变量 下一篇HNU个人项目互评

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目