Pyhton入门 笔记 第三天 面向对象 类

类的定义

class Student():           #定义类  class

    name=''

    age=0

    def print_file(self):      #定义类中的函数称为方法,没有参数时要用self

        print('name:' + self.name)      #使用变量时也要加self

        print('age:' +str(self.age))

student = Student()       #类的实例化,赋值给变量

student.print_file()        #调用类的方法

猜你喜欢

转载自www.cnblogs.com/tngh/p/9313679.html