Python对象 继承 多态 获取对象信息 类的属性
来源:互联网 发布:android apk安装源码 编辑:程序博客网 时间:2024/06/05 07:28
#定义类#Student这种数据类型被视为一个对象,这个对象拥有name和score这两个属性(Property)。#如果要打印一个学生的成绩,首先必须创建出这个学生对应的对象#然后,给对象发一个print_score消息,让对象自己把自己的数据打印出来#给对象发消息实际上就是调用对象对应的关联函数,我们称之为对象的方法(Method)class Student(object):#(object),表示该类是从哪个类继承下来的,通常,如果没有合适的继承类,就使用object类,这是所有类最终都会继承的类。 def __init__(self,name,score): #self必不可少,必须位于其他形参的前面。 #当py调用这个__init__()方法来创建实例时,将自动传入self #其本身是一个指向实例本身的引用,让实例能够访问类中的属性和方法。 self.name=name self.score=score #变量被关联到当前创建的实例 def print_score(self): print('%s:%s' % (self.name,self.score)) def get_grade(self): if self.score >= 90: return 'A' elif self.score >= 60: return 'B' else: return 'C'#在类中定义的函数只有一点不同,就是第一个参数永远是实例变量self,并且,调用时,不用传递该参数。#除此之外,类的方法和普通函数没有什么区别,所以,你仍然可以用默认参数、可变参数、关键字参数和命名关键字参数。bart=Student('Bart Simpson',59) #有了__init__方法,在创建实例的时候,必须传入与__init__方法匹配的参数lisa=Student('Lisa Simpson', 87)bart.print_score() #Bart Simpson:59 lisa.print_score() #Lisa Simpson:87bart.score=100bart.print_score() #Bart Simpson:100print(bart.name) #Bart Simpson#和静态语言不同,Python允许对实例变量绑定任何数据,#也就是说,对于两个实例变量,虽然它们都是同一个类的不同实例,但拥有的变量名称都可能不同bart.age=8print(bart.age) #8 此处与C++中不同,允许实例绑定任何数据,而不要求一定是先定义的属性,但是该属性不会影响到其他对象的属性#print(lisa.age) #报错误:AttributeError: 'Student' object has no attribute 'age'#如果要让内部属性不被外部访问,可以把属性的名称前加上两个下划线__,#在Python中,实例的变量名如果以__开头,就变成了一个私有变量(private),只有内部可以访问,外部不能访问class Student2(object): def __init__(self, name, score): self.__name = name #私有变量(必须是两个下划线) self.__score = score def print_score(self): print('%s: %s' % (self.__name, self.__score))bart2=Student2('Bart Simpson',59) lisa2=Student2('Lisa Simpson', 87)bart2.print_score() #Bart Simpson:59 lisa2.print_score() #Lisa Simpson:87#print(bart2.__name) #错误 AttributeError: 'Student2' object has no attribute '__name'#但是仍然可以为对象重新绑定一个属性bart2.__name='Hello'print(bart2.__name) #Hello 此时应当是新建了一个属性,因此要避免为对象的属性随意赋值#如果外部代码要获取name和score可以给Student类增加get_name和get_score这样的方法#如果又要允许外部代码修改score,可以再给Student类增加set_score方法,在方法中,可以对参数做检查,避免传入无效的参数class Student3(object): def __init__(self, name, score): self.__name = name self.__score = score def print_score(self): print('%s: %s' % (self.__name, self.__score)) def get_grade(self): if self.score >= 90: return 'A' elif self.score >= 60: return 'B' else: return 'C' def get_name(self): return self.__name def get_score(self): return self.__score def set_score(self, score): if 0 <= score <= 100: self.__score = score else: raise ValueError('bad score')bart3=Student3('Bart Simpson',59) bart3.__name='Hello'print(bart3.__name) #Hello#这个__name变量和class内部的__name变量不是一个变量!#内部的__name变量已经被Python解释器自动改成了_Student__name,而外部代码给bart新增了一个__name变量print(bart3.get_name()) #Bart Simpson 内部的属性__name#继承class Animal(object): def run(self): print('Animal is running……')class Dog(Animal): def run(self): #子类的run()覆盖了父类的run() print('Dog is running……')class Cat(Animal): def run(self): print('Cat is running……')Animaler=Animal()Animaler.run() #Animal is running……Doger=Dog()Doger.run() #Dog is running……Cater=Cat()Cater.run() #Cat is running……#多态 由于Animal类型有run()方法,因此,传入的任意类型,只要是Animal类或者子类,就会自动调用实际类型的run()方法#对于一个变量,我们只需要知道它是Animal类型,无需确切地知道它的子类型,就可以放心地调用run()方法,#而具体调用的run()方法是作用在Animal、Dog、Cat还是Tortoise对象上,由运行时该对象的确切类型决定,#这就是多态真正的威力:调用方只管调用,不管细节,#而当我们新增一种Animal的子类时,只要确保run()方法编写正确,不用管原来的代码是如何调用的。这就是著名的“开闭”原则:#对扩展开放:允许新增Animal子类;#对修改封闭:不需要修改依赖Animal类型的run_twice()等函数。def run_twice(x): x.run() x.run()run_twice(Animaler) #Animal is running…… Animal is running……run_twice(Doger) #Dog is running…… Dog is running……run_twice(Cater) #Cat is running…… Cat is running……#静态语言 vs 动态语言#对于静态语言(例如Java)来说,如果需要传入Animal类型,则传入的对象必须是Animal类型或者它的子类,否则,将无法调用run()方法。#对于Python这样的动态语言来说,则不一定需要传入Animal类型。我们只需要保证传入的对象有一个run()方法就可以了:class Timer(object): def run(self): print('Start...')time=Timer() time.run() #Start...run_twice(time) #Start... Start...即不要求一定为run_twice传入Animal类型的对象#判断对象的类型#(1)type()print(type(123)) #<class 'int'>print(type('123')) #<class 'str'>print(type(None)) #<class 'NoneType'>print(type(abs)) #<class 'builtin_function_or_method'>print(type(Animal)) #<class 'type'>print(type(123)==type(345)) #Trueprint(type(123)==int ) #Trueprint(type('123')==str ) #Trueprint(type('123')==type(123)) #False#如果要判断一个对象是否是函数怎么办?可以使用types模块中定义的常量:import typesprint(type(run_twice)==types.FunctionType) #Trueprint(type(abs)==types.BuiltinFunctionType) #Trueprint(type(lambda x:x*x) ==types.LambdaType) #Trueprint(type((x for x in range(10)))==types.GeneratorType) #True#(2)使用isinstance()print(isinstance(Animaler,Animal)) #Trueprint(isinstance(Doger,Dog)) #Trueprint(isinstance(Cater,Dog)) #Falseprint(isinstance(Doger,Animal)) #Trueprint(isinstance(Animaler,Dog)) #Falseprint(isinstance('a', str)) #Trueprint(isinstance(b'a', bytes)) #True#还可以判断一个变量是否是某些类型中的一种print(isinstance([1, 2, 3], (list, tuple))) #Trueprint(isinstance((1, 2, 3), (list, tuple))) #True#(3)使用dir可以获得一个对象的所有属性和方法,它返回一个包含字符串的listprint(dir('ABC'))#['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getat#tribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__'#, '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setat#tr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', '#expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'isl#ower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'pa#rtition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith',#'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']#属性配合getattr()、setattr()以及hasattr(),我们可以直接操作一个对象的状态:class MyObject(object): def __init__(self): self.x = 9 def power(self): return self.x * self.x return self.x * self.xobj = MyObject()print(hasattr(obj,'x')) #Trueprint(obj.x) # 9setattr(obj, 'y', 19) # 设置一个属性'y'print(hasattr(obj, 'y')) # 有属性'y'吗? Trueprint(getattr(obj, 'y')) # 19print(getattr(obj, 'z', 404))#可以传入一个default参数,如果属性不存在,就返回默认值 404fn = getattr(obj, 'power') # 获取属性'power'并赋值到变量fnprint(fn()) #81#实例属性和类属性#给实例绑定属性的方法是通过实例变量,或者通过self变量:class Student4(object): def __init__(self, name): self.name = names = Student4('Bob')s.score = 90#如果Student类本身需要绑定一个属性,可以直接在class中定义属性,这种属性是类属性,归Student类所有,#通过实例可以访问该属性,当通过实例为该属性进行赋值时,属于实例的属性的值变化,但是属于类的属性的值不变,#当通过类直接为该属性赋值,就会改变其值class Student4(object): name = 'Student4' #相当于C++中的static静态成员变量#定义了一个类属性后,这个属性虽然归类所有,但类的所有实例都可以访问到。s = Student4() # 创建实例sprint(s.name) #Student4 打印name属性,因为实例并没有name属性,所以会继续查找class的name属性 print(Student4.name) # Student4 打印类的name属性s.name = 'Michael' # 给实例绑定name属性print(s.name) #Michael 由于实例属性优先级比类属性高,因此,它会屏蔽掉类的name属性print(Student4.name) #Student4 但是类属性并未消失,用Student.name仍然可以访问del s.name # 如果删除实例的name属性print(s.name) #Student4 再次调用s.name,由于实例的name属性没有找到,类的name属性就显示出来了Student4.name='Bob'print(Student4.name) #Bob 直接为类的变量赋值,就会改变
阅读全文
0 0
- Python对象 继承 多态 获取对象信息 类的属性
- python类的继承和多态,获取对象信息
- Python面向对象编程(类和实例 访问限制 继承和多态 获取对象信息 实例属性和类属性)
- Python中继承、多态、多继承、判断类型、json.load()解析、获取/设置对象信息
- python获取对象信息
- Python 获取对象信息
- Python 获取对象信息
- 如何获取GDI对象的属性信息
- python的类、对象、继承
- 【python】获取对象的类型和信息
- python 获取对象信息的方法
- python元编程_获取类或对象的属性
- python中获取对象信息
- python getattr获取对象属性
- 获取对象的接口信息(方法/属性/事件)(VB6代码)
- 获取对象的接口信息(方法/属性/事件)(VB6代码)
- 【Python】Python的类、对象、属性、方法
- Python面向对象,类,继承,多态及鸭子类型,获取类的类型,方法和属性(类似java的反射)
- POJ 3123 Ticket to Ride 笔记
- 《剑指Offer》替换空格(将字符串中的空格替换为%20)
- ATECC508A芯片开发笔记(一):初识加密芯片
- 文章标题
- linux下MySQL数据库安装初始化
- Python对象 继承 多态 获取对象信息 类的属性
- -----找规律 ZOJ 3829-Known Notation
- 基于Spring Boot和Spring Cloud实现微服务架构学习
- 第七天-Java常用类之一
- 选弹球
- bzoj2405 数字 打表找规律
- 阿里巴巴智能操作系统YunOS 6 系统发布
- hdu 1950 Bridging signals
- Junit4注解@Test|@Before等使用