代码1: #!/usr/bin/env python3 # -*- coding: utf-8 -*- class Student(object): def __init__(self,name,score): self.__name=name self.__score=score def printme(self): print("%s %s"%(self.__name,self.__score)) def getname(self): return self.__name def getscore(self): return self.__score def setname(self,name): print() self.__name=name def setscore(self, score): if 0 <= score <= 100: self.__score = score else: print('input is error!') student1=Student('zhaoyinfjie',150) student1.printme() student1.setname('zhao') print(student1.getname()) student1.printme() student1.setscore(100) print(student1.getscore()) student1.printme() ------------------------------------------------- 代码2: #!/usr/bin/env python3 # -*- coding: utf-8 -*- class Student(object): def __init__(self,name,score): self.__name=name self.__score=score def printme(self): print('%s %s'%(self.__name,self.__score)) student1=Student('赵',200) student1.printme() ---------------------------------------------------- 代码3: #!/usr/bin/env python3 # -*- coding: utf-8 -*- class Student(object): def __init__(self,name,score): self.__name=name self.__score=score def setname(self): self.__name=name def setscore(self): if 0<=score<=200: self.__score=score else: raise ValueError('bad score') def getname(self): return self.__name def getscore(self): return self.__score student1=Student('赵英俊',150) print(student1.getname()) print(student1.getscore()) #-------------------------- 赵英俊 150 ---------------------------------------------------- 代码4: #!/usr/bin/env python3 # -*- coding: utf-8 -*- class Animal(object):#父类 def run(self): print("Animal is running!") class Cat(Animal): def run(self):#改造继承的方法 print("Cat is running!") def eat(self):#增加子类的方法 print("eat meat!") class Dog(Animal): def run(self):#改造继承的方法 print("cat is runing!") def eat(self):#增加子类的方法 print("eat mouse!") dog1=Dog() cat1=Cat() dog1.run() dog1.eat() cat1.run() cat1.eat() #--------------------------------- cat is runing! eat mouse! Cat is running! eat meat! Press any key to continue . . . ---------------------------------------------------- 判断变量的类型: #!/usr/bin/env python3 # -*- coding: utf-8 -*- a=list() #判断变量的类型 z1=isinstance(a,list) z2=isinstance(a,tuple) z3=isinstance(a,str) print(z1)#T print(z2)#F print(z3)#F ---------------------------------------------------- 分享知识,分享快乐!希望中国站在编程之巅!
360图书馆馆号:rsgz002.360doc.com |
|