首页 > 代码库 > Python:对象

Python:对象

#!/usr/bin/python3#对象实例class Person:    num=200    def __init__(self,name,sex):        self.name=name        self.sex=sex    def f(self):        return "good"    def speak(self):        print("Person.speak 用户:%s 性别:%s"%(self.name,self.sex))#实例化类x = Person("张三","")print("x.num",x.num)print("x.name",x.name)print("x.sex",x.sex)x.f()x.speak()#单继承class Student(Person):        def __init__(self,name,sex,grade):        Person.__init__(self,name,sex)        self.grade=grade    #方法重写    def speak(self):        print("Student.speak: 用户:%s 性别:%s 年级:%s"%(self.name,self.sex,self.grade))y=Student("李四","","二年级")print("y.name",y.name)print("y.sex",y.sex)print("y.grade",y.grade)y.speak()#私有变量#__privateCount#publicCount

 

Python:对象