小白學python之類與實例_學習筆記

本文以廖雪峯的官方網站爲參考來學習python的。其學習鏈接爲廖雪峯小白學python教程

本文是學習到python的類與實例。參考鏈接廖雪峯python類與實例

class Student(object):
    pass

bart = Student()
print(bart)

print(Student)

bart.name = 'Bart Simpson'
print(bart.name)

class Student(object):

    def __init__(self,name,score):
        self.name = name
        self.score = score

bart = Student('Bart Simpson', 59)
print(bart.name)
print(bart.score)

通過上面學習,學習到了有關python類的使用。

def print_score(std):
    print('%s:%s' % (std.name, std.score))

print_score(bart)

print("*********")

class Student(object):
    def __init__(self,name,score):
        self.name = name
        self.score = score

    def print_score(self):
        print('%s: %s' % (self.name, self.score))
bart = Student("Bart Simpson", 59)
bart.print_score()
#print(bart.print_score())

通過這段代碼學習學習到了類的封裝以及使用。


# -*- coding: utf-8 -*-
class Student(object):
    def __init__(self,name,score):
        self.name = name
        self.score = score

    def get_grade(self):
        if self.score >= 90:
            return 'A'
        elif self.score >= 60:
            return 'B'
        else:
            return 'C'

lisa = Student('Lisa',99)
bart = Student('Bart',59)
print(lisa.name, lisa.get_grade())
print(bart.name, bart.get_grade())

敲代碼,完成了作業。

原來python類以及實例概念的理解以及使用沒有那麼難學。python還是很適合容易上手學習的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章