编程语言
首页 > 编程语言> > 1、Python中类的学习

1、Python中类的学习

作者:互联网

class student:   #定义类
    student_count=0   #定义类变量

    def __init__(self, name, age):  #类的初始化
        self.name = name
        self.age = age
        student.student_count += 1

    def  dis_student(self):   #定义类的实例
        print("student name is "+self.name,'student_age is '+self.age)

student1 = student('zhang', '25')
student2 = student('chen', '36')
student1.dis_student()
student2.dis_student()
print(student.student_count)

#子类 父类的定义
class people:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def dis_name(self):
        print('her name is ', self.name)
    def dis_age(self):
        print('her age is ', self.age)
class student(people):
    def __init__(self,name,age,school):
        self.name = name
        self.age = age
        self.school = school
    def dis_school(self):
        print('her school is ',self.school)

Student=student('zhang','23','wcxx')
Student.dis_age()
Student.dis_name()
Student.dis_school()

标签:school,name,Python,self,学习,student,age,中类,dis
来源: https://blog.csdn.net/zhangzhoubin666/article/details/119455737