# -*- 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()