测试
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 13 20:37:35 2018
@author: Administrator
"""
"""破坏私密方法 不建议使用"""
class Person:
def __serect(self):
print("this is a serect")
def getSerect(self):
self.__serect()
p = Person()
p._Person__serect()
"""在命名空间内定义的属性 可以通过方法和对象进行访问"""
class Member:
number = 0;
def getNewNumber(self):
Member.number+=1
m = Member()
for i in range (1,10):
m.getNewNumber()
print(Member.number)
m1 = Member()
m1.getNewNumber()
print(m1.number)
m2 = Member()
m2.number = "asd"
print(m2.number)
"""超类(父类)的使用 孩子继承父亲后可以重写父亲的方法也可以使用父亲的方法"""
class Father:
def Arr(self):
self.arr=[]
def giveArr(self,sequence):
return [x for x in sequence if x not in self.arr]
class Children(Father):
def Arr(self):
self.arr=["jsp","servlet","Ajax","Json","JDBC"]
father = Father()
father.Arr()
print(father.giveArr(["jsp"]))
children = Children()
children.Arr()
print(children.giveArr(["jsp","abc"]))
#判断两个类是否存在继承关系
if(issubclass(Children,Father)):
print("是父类")
#查看一个类的父类
print(Children.__bases__)
#判断是不是同类对象
print(isinstance(children,Children))
print(isinstance(children,Father))
#查询对象是哪一个类
print(children.__class__)
"""多个超类 多重继承"""
class a:
def getAnswer(self,expression):
self.value = eval(expression)
class b:
def getResult(self):
print("Answer is",self.value)
class c(a,b):
"""占位符"""
pass
mic=c()
mic.getAnswer("3*2*2+1")
mic.getResult()
"""接口"""
#判断方方法是否存在
print(hasattr(mic,"getAnswer"))
"""抽象类"""
from abc import ABC,abstractmethod
class ABA(ABC):
@abstractmethod
def abatext(self):
pass
class ABB(ABA):
def abatext(self):
print("该方法已经被重写 不是抽象类 可以进行实例化")
abb = ABB()
abb.abatext()
if(isinstance(abb,ABB)):
print("abb是aba的对象")
class ABD():
def abcText(self):
print("NONE")
abd=ABD()
"""将ABD注册为ABB但是不是继承"""
ABB.register(ABD)
print(isinstance(abd,ABB))
"""判断该方法是否可以调用"""
print(callable(getattr(abb,"abatext",None)))
setattr(abb,"name","张三")#设置属性以及属性名字
print(abb.name)
"""拿到属性的value"""
print(getattr(abb,"name",None))
运行结果