python类和对象实例(四)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###定义类###
class Student(object):
    # 类属性,所有实例都可以访问
     __count=0

     #一个特殊的__slots__变量,来限制该class实例能添加的属性
     __slots__=("__name","__score","genger")

     def __init__(self,name,score):
         self.__name = name
         self.__score=score
         Student.__count = self.__count + 1 #统计实例化对象个数

     def printScore(self):
          print(self.__name,self.__score)

     def setName(self,value):
         self.__name=value;##__name为私有变量,只能在类内部访问

     def getName(self):
         return self.__name;

     def printInstanceCount(self):
         print(Student.__count);

###定义类###

###使用类###
#实例化对象
student=Student("zhangsan",90);
student.printScore()
student.setName("lisi")
print(student.getName())
#给一个实例变量绑定属性
student.genger="female";
print(student.genger)

bool=isinstance(student,Student)
print("判断student是否是Student类型:",bool)

student=Student("wangwu",89);
student=Student("zhaoliu",78);
Student.printInstanceCount(Student)
###使用类###


猜你喜欢

转载自blog.csdn.net/weixin_42274510/article/details/80565910
今日推荐