在python2中没有声明父类,则没有父类,
在python3中没有声明父类,则父类为object,object为所有类的父类
类
: 1:实例属性:
: 最好在__init__(self,…)中初始化
: 内部调用时都需要加上self.
: 外部调用时用instancename.propertyname
- 2:类属性:
-
类属性:(修改多个对象的某个特征更加简单,程序运行中)
: 在__init__()外初始化
: 在内部用classname.类属性名调用
: 外部既可以用classname.类属性名又可以用instancename.类属性名来调用
3:私有属性:
1):单下划线_开头:只是告诉别人这是私有属性,外部依然可以访问更改
2):双下划线__开头:外部不可通instancename.propertyname来访问或者更改, (Python不允许实例化的类访问私有数据,但你可以使用 object._className__attrName 访问属性
: 实际将其转化为了_classname__propertyname
子类继承父类,继承了父类的公有方法、属性
dir(对象名):显示对象的属性和方法
dir(str)
dir(dict)
dir(tuple)
# 继承也能调用私有的属性和方法
class Person(object):
def __init__(self, name, age, money):
self.name = name
self.age = age
self.__money = money
def __step(self):
print(self)
class Women(Person):
pass
yang = Women('yue', 18, 1000)
print(dir(yang))
print(yang._Person__money)
yang._Person__step()
2 类属性的重新赋值(唯一一种方式)
Python的类和类实例都是可变对象,可以随时给属性赋值,并且在原处修改。
在对类属性进行修改时需要特别小心,因为所有的类实例都继承共享类属性,除非实例本身存在和类属性同名的属性。对类属性进行修改,会影响到所有由这个类生成的实例。
class Person(object):
country = 'china'
pass
print(Person.country)
Person.country = 'yang'
print(Person.country)
修改类属性
class People(object):
country = 'china' #类属性
print(People.country) # 类名调用类属性
p = People()
print(p.country) # 对象对用类属性
p.country = 'japan'
print(p.country) # 实例属性会屏蔽掉同名的类属性
print(People.country)
del p.country # 删除实例属性
print(p.country)
china
china
japan
china
china
如果需要在类外修改类属性,必须通过类对象去引用然后进行修改。如果通过实例对象去引用,会产生一个同名的实例属性,这种方式修改的是实例属性,不会影响到类属性,并且之后如果通过实例对象去引用该名称的属性,实例属性会强制屏蔽掉类属性,即引用的是实例属性,除非删除了该实例属性。
空值类型
# 空值类型
n = None
print(type(n)) # <class 'NoneType'>
print(id(n)) # 10353568 python已经开辟好的空间保存当前的空值类型
print(None is False) # False
new
# __new__
#__new__方法才是真正的构造函数。
# 监听对象创建成功并返回实例对象
def __new__(cls, *args, **kwargs):
return object.__new__(cls)
# init
# __init__ 方法为初始化方法,
# 监听对象创建成功后,给对象添加属性并赋值
#
#
# 追踪通过这个类创建的对象的属性值变化的
# 监听内存地址的引用计数位0,(调用__del__监听对象销毁)
例子1:
# 单例设计模式:通过一个类创建出来的所有的对象的地址都是一样的
# 如果想使用这个类创建的对象代表不同的事物,正常创建即可
# 如果只是想使用类中的方法,不关心对象是不同的对象,可以使用单例模式,(节约内存)
class Singleton(object):
# __country 类变量:可以通过类名、或者对象直接调用
__country = 'china'
# 定义一个类属性保存对象
instance = None
#
__is_first = True
def __new__(cls, *args, **kwargs):
# 判断是否第一次创建对象,如果是则调用父类的__new__方法返回实例化出来的实例,
# 保证只有一个对象
if not cls.instance:
cls.instance = object.__new__(cls)
return cls.instance
def __init__(self, age, name):
if self.__is_first:
self.__name = name
self.age = age
self.__is_first = False
# cls -> class -> Person
@classmethod # 修饰器
def get_country(cls):
return cls.__country
def func(self):
print(self.__name)
@staticmethod
def hello():
print('hello')
a = Singleton(18, 'xizi')
b = Singleton(20, 'xiaojia')
print(id(a) == id(b)) # True 地址一样,不创建新的对象
print(a.age)
print(b.age)
a.age = 19
print(a.age)
print(Singleton.get_country()) # china
print(a.func()) # 18 \n None, 函数执行完事之后,打印的话为None
print(Singleton)
例二
class HelloWorld():
def set(self, word):
self.word = word
def say_hello(self):
print(self.word)
class SingleCase(object):
def __new__(cls, *args, **kwargs):
if not hasattr(SingleCase, "_instance"):
cls._instance = super(SingleCase, cls).__new__(cls, *args, **kwargs)
cls._instance.hello = HelloWorld()
print("cls._instance.hello", id(cls._instance.hello))
cls._instance.hello.set("Hello world")
cls_addr = id(cls._instance)
print("cls_addr", cls_addr)
return cls._instance
def print(self):
self_addr = id(self)
instance_addr = id(self._instance)
print("self_addr", self_addr)
print("instance_addr", instance_addr)
self.hello.say_hello()
a = SingleCase()
b = SingleCase()
a.print()
print(id(a))
print(id(b))
"""output
cls._instance.hello 140312550013864
cls_addr 140312550013696
self_addr 140312550013696
instance_addr 140312550013696
Hello world
140312550013696
140312550013696
"""