python 面向对象编程举例

放在一个py文件里:
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)
lisa = Student('Lisa Simpson', 87)
bart.print_score()
lisa.print_score()

C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/t1.py
Bart Simpson: 59
Lisa Simpson: 87


单独文件:
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)



from   mycompany.web.Student  import *
bart = Student('Bart Simpson', 59)
lisa = Student('Lisa Simpson', 87)
bart.print_score()
lisa.print_score()


C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a1.py
Bart Simpson: 59
Lisa Simpson: 87


原文地址:https://www.cnblogs.com/hzcya1995/p/13349608.html