python -- 面向对象 - 反射
class Animal:
def eat(self):
print('动物的世界你不懂') class Cat(Animal):
def play(self):
print('毛霸王') c = Cat() print(isinstance(c,Cat)) # True
# 向上判断
print(isinstance(c,Animal)) # True a = Animal()
print(isinstance(a,Cat)) # False # 不能向下判断 print(type(a)) # <class '__main__.Animal'>
print(type([])) # <class 'list'>
print(type(c)) # <class '__main__.Cat'> # 判断 **类是否是**类的子类
print(issubclass(Cat,Animal)) # True
print(issubclass(Animal,Cat)) # False
def cul(a,b):
if (type(a) == int or type(a) == float) and (type(b) == int or type(b) == float):
return a+b
else:
print('提供的数据无法计算')
print(cul(6,7))
def func():
print('我是函数') class Foo:
def eat(self):
print('要吃饭了') print(func) # <function func at 0x0000020EDE211E18>
f = Foo()
f.eat() print(f.eat) # <bound method Foo.eat of <__main__.Foo object at 0x00000237390BE358>>
# 类也是对象
# 这个对象:属性就是类变量
# 方法就是类方法 class Person():
def eat(self):
print('明天去吃鱼') @classmethod
def he(cls):
print('我是类方法') @staticmethod
def lang():
print('我是静态方法') p = Person()
Person.eat(1) # 不符合面向对象的思维 print(p.eat) # <bound method Person.eat of <__main__.Person object at 0x00000253E992E3C8>>
print(Person.eat) # <function Person.eat at 0x00000253E992DF28> # 类方法都是方法
print(Person.he) # <bound method Person.he of <class '__main__.Person'>>
print(p.he) # <bound method Person.he of <class '__main__.Person'>> # 静态方法都是函数
print(Person.lang) # <function Person.lang at 0x0000024C942F1158>
print(p.lang) # <function Person.lang at 0x0000024C942F1158>
def eat():
print('吃遍人间美味') def travel():
print('游遍万水千山') def see():
print('看遍人间繁华') def play():
print('玩玩玩') def learn():
print('好好学习天天向上')
import master
while 1:
content = input('请输入你要测试的功能:')
# 判断 XXX对象中是否包含了xxx if hasattr(master,content):
s = getattr(master,content)
s()
print('有这个功能')
else:
print('没有这个功能')
class Foo:
def drink(self):
print('要少喝酒') f = Foo()
s = (getattr(f,'drink')) # 从对象中获取到str属性的值
s()
print(hasattr(f,'eat')) # 判断对象中是否包含str属性
setattr(f,'eat','西瓜') # 把对象中的str属性设置为value
print(f.eat)
setattr(f,'eat',lambda x:x+1)
print(f.eat(3))
print(f.eat) # 把str属性从对象中删除
delattr(f,"eat")
print(hasattr(f,'eat'))
python -- 面向对象 - 反射的更多相关文章
- python面向对象 : 反射和内置方法
一. 反射 1. isinstance()和issubclass() isinstance( 对象名, 类名) : 判断对象所属关系,包括父类 (注:type(对象名) is 类名 : 判断对象所属 ...
- python面向对象反射-框架原理-动态导入-元类-自定义类-单例模式-项目的生命周期-05
反射 reflect 反射(reflect)其实是反省,自省的意思 反省:指的是一个对象应该具备可以检测.修改.增加自身属性的能力 反射:通过字符串获取对象或者类的属性,进行操作 设计框架时需要通过反 ...
- python 面向对象反射以及内置方法
一.反射 什么是反射:可以用字符串的方式去访问对象的属性,调用对象的方法(但是不能去访问方法),python中一切皆对象,都可以使用放射. 反射的四种方法: hasattr:hasattr(objec ...
- python面向对象(反射)(四)
1. isinstance, type, issubclass isinstance: 判断你给对象是否是xx类型的. (向上判断 type: 返回xxx对象的数据类型 issubclass: 判断x ...
- python面向对象--反射机制
class Black: feture="ugly" def __init__(self,name,addr): self.addr=addr self.name=name def ...
- Python 面向对象之反射
Python 面向对象之反射 TOC 什么是反射? hasattr getattr setattr delattr 哪些对象可以使用反射 反射的好处 例子一 例子二 什么是反射? 程序可以访问.检查和 ...
- Python面向对象之-反射
Python中一切皆对象,在Python中的反射:通过字符串的形式操作对象的属性 hasattr 判断是否有改属性或者方法,有返回True,没有返回false getattr 如果是属性获得该属性 ...
- python 面向对象编程 之 反射
1 什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被 ...
- Python之面向对象-反射
一.什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问,检测和修改它本省状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被 ...
随机推荐
- Linux的is not in the sudoers file 解决
https://blog.csdn.net/hxpjava1/article/details/79566822
- nginx rewrite 指令
ginx通过ngx_http_rewrite_module模块支持url重写.支持if条件判断,但不支持else. 该模块需要PCRE支持,应在编译nginx时指定PCRE源码目录, nginx安装方 ...
- 擦他丫的,今天在Django项目中引用静态文件jQuery.js 就是引入报错,终于找到原因了!
擦 ,今天在Django项目中引用静态文件jQuery.js 就是引入报错,终于找到原因了! 问题在于我使用的谷歌浏览器,默认使用了缓存,导致每次访问同一个url时,都返回的是缓存里面的东西.通过谷歌 ...
- [macOS] PHP双版本,5.6跟7.1
转过来的,原文看这里,https://www.symfony.fi/page/how-to-run-both-php-5-6-and-php-7-x-with-homebrew-on-os-x-wit ...
- Select input 两个元素的宽度高度跟设定值不一致的问题
在做登录框的时候,需要一个select 元素作为账号输入,一个input作为密码输入框.在css 文件中,将这两个元素的position 设置为relative ,并且width 设置为100%.刷新 ...
- Delphi数据库技术中Disablecontrols和Enablecontrols的功能
一般来说,用来扫描整个数据库表并修改每个记录的某一个字段的程序如下所示: with Table Do begin DisableControls;{在修改记录的过程中,使其它部件无效} First; ...
- Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4
Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from https://repo.mave ...
- Tomcat增加Context配置不带项目名访问导致启动的时候项目加载两次
eclipse发布web应用至tomcat,默认方式下访问该项目是需要带项目名称的,例http://localhost:8080/myapp/.现在需要改成这样访问http://localhost.修 ...
- Python xml模块
xml模块 自己创建xml文档 import xml.etree.cElementTree as ET new_xml = ET.Element("personinfolist") ...
- openvpn 初步使用
服务端:Centos 7.2 openvpn 2.4.3 客户端:Windows 10 安装包 openvpn的官网在国内访问不了,服务端通过yum安装,客户端在第三方网站下载的 一般的国内源应该都包 ...