1、Python中类的学习

2021/11/19 11:11:33

本文主要是介绍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()



这篇关于1、Python中类的学习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程