[自学Python纪录] HackerRank 新手30天挑战-Day04

Hi there! 我是嘟嘟~受到前辈启发,想说可以纪录一下自己练习的过程,小女子为程式超超菜鸟,此系列非教学文,仅为个人解题笔记,可能有错误或未补充详尽之处,欢迎前辈们不吝指教!也欢迎正在自学的伙伴一起讨论学习~


Day 4: Class vs. Instance (类别VS实例)

第4天难度突变,没学过class所以花了一些时间,也不确定自己有没有理解错误,欢迎指正补充!

题目要求一个Person类别(class),整数的属性参数为initialAge,指定到实例变数age,若age < 0,则将age设为0然后印出Age is not valid, setting age to 0.,除此之外还要定义两个方法(method):

yearPasses() : should increase the instance variable ageby 1 (题目有要求3年后再判断一次,故让年龄加1)amIOld() : should perform the following conditional actions:If age< 13, print You are young.If age≧ 13 and age< 18, print You are a teenager.Otherwise, print You are old.

注意: 不要修改到题目原始格式
Note: Do not remove or alter the stub code in the editor.

输入格式

第一行输入整数储存成t,代表测试值的数量,后面t行输入整数储存成age,作为类别Person的实例变数。

输出格式

age ≧ 0 会印出两行,若age < 0 会印出三行

样本输入

4-1101618

样本输出

Age is not valid, setting age to 0. #因为-1<0,所以要设成0You are young. #0岁You are young. #3岁You are young. #10岁You are a teenager. #13岁You are a teenager. #16岁You are old. #19岁You are old. #18岁You are old. #21岁

原始格式

class Person:    def __init__(self,initialAge):        # Add some more code to run some checks on initialAge    def amIOld(self):        # Do some computations in here and print out the correct statement to the console    def yearPasses(self):        # Increment the age of the person in heret = int(input())for i in range(0, t):    age = int(input())             p = Person(age)      p.amIOld()    for j in range(0, 3):        p.yearPasses()           p.amIOld()    print("") #最后这行是系统要判断是不是和预期输出一样,所以不能动到

我的解答

class Person(): #定义类别Person    def __init__(self, initialAge):        #定义建构子函式__init__,输入参数self和initialAge        if initialAge < 0:            print('Age is not valid, setting age to 0.')            initialAge = 0 #如果initialAge小于0则将其改为0        self.initialAge = initialAge        #将initialAge指定给变数self.initialAge    def amIOld(self):        #定义函式amIOld,参数为self,印出变数self.initialAge的条件式结果        if self.initialAge < 13:            print('You are young.')        elif 13 <= self.initialAge < 18:            print('You are a teenager.')        else:            print('You are old.')                def yearPasses(self):        #定义函式yearPasses,参数为self,宣告变数self.initialAge = self.initialAge + 1        self.initialAge += 1t = int(input())for i in range(0,t):    age = int(input()) #宣告输入值为变数age    p = Person(age)    #宣告类别为Person的物件p,将变数age作为参数传回类别Person的函式中    p.amIOld() #呼叫函式amIOld,此时p.initialAge = 原initialAge        for j in range(0,3):         p.yearPasses() ##呼叫函式yearPasses三次    p.amIOld() #此时p.initialAge = 原initialAge + 3

执行结果

输入值

2-524

结果为

Age is not valid, setting age to 0.You are young. #0You are young. #3You are old. #30You are old. #33

补充:

基本上只要输入定义函式的部分就好,下面部分题目本来就给好了(怕太难新手打不出来XD),这题重点就是了解 class (类别) ,以下整理了一些资料:

class (类别),就是像一个模组,可以产出具有相似特性(属性Attribute 及方法Method)的实体(物件Object),也有人会说他像是一个蛋糕模子,可以一直套用生产蛋糕,代表之后的大家都可以取用这个类别的设定,达到避免重複设定函式

类别名称建议为驼峰式大小写(CamelCase),字母首字为大写,单字之间不以空格断开(例:camel case)或连接号(-,例:camel-case)、底线(_,例:camel_case)连结

每个类别有两种变数: 类别变数(class variables) 和 实例变数(instance variables)

def __init__(self, <parameters>):
这边代表宣告时会自动执行的函式,也就是宣告类别的"起手式",所以一般会拿来放基础的属性设定。(非强制)

类别方法的第一个参数一定是 self ,他是 Python 类别定义中预设的参数,代表建立的物件实体,因此 self.a 与 self.b 都是实体属性。

存取属性与执行方法,语法都是利用点号:
<object>.<attribute> # 存取属性(变数)
<object>.<method>() # 执行方法(函式)

题目的Tutorial有提到 Function overloading (函数重载、函数多载),重载函数是函数的一种特殊情况,为方便使用,允许在同一範围中声明几个功能类似的同名函数,但是这些同名函数的形式参数(指参数的个数、类型或者顺序)必须不同,也就是说用同一个函数完成不同的功能。参考文章【为什么Python不支持函数重载?而其他语言大都支持?】

参考来源:
【Python 程式设计-第 15 章 物件与类别】
【Python类别与例外-高中资讯科技概论教师黄建庭的教学网站】
↑ 嘟嘟觉得他写得很浅显易懂又详细,还有延伸变化(继承、多型....比较複杂的还没有理解,未来可以回来看)
【关于Python的类别(Class)...基本篇-张凯乔-Medium】
【Python 入门指南 - 类别】
【Python 入门指南 - init()】
【Python 使用类别】(像教科书但写得很清楚)


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章