分享

代码练手集合一--python

 融水公子 2018-09-16
代码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
----------------------------------------------------





分享知识,分享快乐!希望中国站在编程之巅!

               ----

公众微信号:rsgz520

360图书馆馆号:rsgz002.360doc.com 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多