首页 > 编程语言 > 详细

Python类和实例调用

时间:2019-09-30 10:00:01      阅读:105      评论:0      收藏:0      [点我收藏+]

self指向的是实例对象,作为第一个参数,使用时不需要传入此参数。

class Student(object):
    #定义一个Student类,
    def __init__(self, name, score,age):
        self.name = name
        self.score = score
        self.age = age
    def print_score(self):
        #定义类方法,实例对象可调用
        return self.name,self.score
    def get_grade(self):
        if self.score >= 90:
            return A
        elif self.score >= 60:
            return B
        else:
            return C
    
st1 = Student(lingyummu,67,25)
    #创建一个Student类对象st1
print(st1.name,st1.age,st1.score)
print(st1.print_score())
print(st1.get_grade())

读书和健身总有一个在路上

Python类和实例调用

原文:https://www.cnblogs.com/Renqy/p/11610956.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!