类的定义
1、父类
# coding=gbk
class Person(object):
def __init__(self, provincial_card):
self.provincial_card = provincial_card
def get_info(self):
print("Person get_info")
2、子类
# coding=gbk
from Person import Person
#Student继承Person
class Student(Person):
uuid = 'xxxxxxxxxxxxxx'
sum1 = 0
sum2 = 0
#私有变量:前面有__
__sum3 = 3
# 构造函数
def __init__(self, name, age, card):
self.name = name
self.age = age
super(Student, self).__init__(card)
# 类变量
self.__class__.sum1 += 1
print("创建了第" + str(self.__class__.sum1) + "个Student对象")
# 实例方法
def prin_info(self, aaa):
print(self.uuid + ',' + self.name + ',' + str(self.age) + ',' + aaa)
# 静态方法
@staticmethod
def get_student_info(student):
return student.name + str(student.age)
# 类方法
@classmethod
def do_sum2(cls):
cls.sum2 += 1
print("do_homework" + str(cls.sum2))
# 私有方法:前面有__
def __test(self):
pass
3、调用
# coding=gbk
from Student import *
student1 = Student('Quincy1', 1, '123123123123123')
student2 = Student('Quincy2', 2, '123123123123123')
student3 = Student('Quincy3', 3, '123123123123123')
student4 = Student('Quincy4', 4, '123123123123123')
Student.do_sum2()
student1.prin_info('abcd')
print(Student.get_student_info(student1))
print(student1.name)
#调用父类方法
student1.get_info()