-
class Animal():
pass
dog = Animal()
cat = Animal()
dog.name='wangcai'
cat.name='mimi'
dog.age=5
cat.age=3
print(dog.name,dog.age)
print(cat.name,cat.age)
查看全部 -
class Animal():
pass
dog = Animal()
cat = Animal()
print(dog)
print(cat)
查看全部 -
class Animal:
__count = 0
def __init__(self,name):
self.name = name
Animal.__count += 1 #类的实例数量
dog = Animal("wangwang")
cat = Animal("mimi")
bird = Animal("jiji")
print(Animal.__count)In the given Python code, the line Animal.count += 1 is using the class name Animal rather than self for incrementing the count variable. This is because count is a class variable, shared by all instances of the Animal class.
When you use Animal.count, you are directly accessing the class variable and incrementing its value. This means that every time a new instance of the Animal class is created, the count variable is incremented for the entire class, not just for the specific instance.
On the other hand, if you were to use self.count += 1 in the __init__ method, it would create an instance variable count specific to each instance of the class. In that case, each instance would have its own separate count value.
class Animal:
count = 0
def __init__(self,name):
self.name = name
self.count += 1
dog = Animal("wangwang")
cat = Animal("mimi")
bird = Animal("jiji")
print(dog.count)#独立计算每个实例都个数,都是1print(Animal.count)#类变量为0
查看全部 -
class Animal:
__count = 0
def __init__(self,name):
self.name = name
Animal.__count +=1
@classmethod
def get_count(cls):
return cls.__count
dog = Animal("wangwang")
cat = Animal("mimi")
print(Animal.get_count())查看全部 -
class Animal():
def __init__(self,age,name,location):
self.__age = age
self.__name = name
self.__location = location
def get_name(self):#通过定义实例方法来操作私有属性
return self.__name
def get_age(self):#通过定义实例方法来操作私有属性
return self.__age
def get_location(self):#通过定义实例方法来操作私有属性
return self.__location
dog = Animal(1,"sisi","Shanghai")
bird = Animal(12, "gigi", "Anhui")
cat = Animal(3, "mimi", "Canada")
print(dog.get_age())
print(bird.get_name())
print(cat.get_location())查看全部 -
在外部访问私有属性将会抛出异常,提示没有这个属性。
虽然私有属性无法从外部访问,但是,从类的内部是可以访问的。私有属性是为了保护类或实例属性不被外部污染而设计的。# 类私有属性
class Animal(object):
__localtion = 'Asia'
print(Animal.__localtion)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'Animal' has no attribute '__localtion'# 实例私有属性
class Animal(object):
def __init__(self, name, age, localtion):
self.name = name
self.age = age
self.__localtion = localtion
dog = Animal('wangwang', 1, 'GuangDong')
print(dog.name) # ==> wangwang
print(dog.age) # ==> 1
print(dog.__localtion)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Animal' object has no attribute '__localtion'查看全部 -
'''请定义一个动物类
抽象出名字、年龄两个属性
并实例化两个实例dog, cat。
'''
class Animal:
def __init__(self,name,age):
self.name = name
self.age = age
dog = Animal("wangwang",2)
cat = Animal("cici",3)
print(dog.name)
print(cat.age)查看全部 -
'''请定义一个动物类
并创建出两个实例dog, cat
分别赋予不同的名字和年龄
并打印出来'''
class Animal:
def __init__(self,name,age):
self.name = name
self.age = age
dog = Animal("chichi", 2)
cat = Animal("mike", 6)
print(dog)
print(cat)查看全部 -
'''请练习定义一个动物类
并创建出两个实例dog, cat
打印实例
再比较两个实例是否相等'''
class Animal:pass
dog = Animal()
cat = Animal()
print(dog)
print(cat)
print(dog == cat)查看全部 -
mark
查看全部 -
class Student(Person):
def __init__(self, name, gender, score):
super(Student, self).__init__(name, gender)查看全部 -
实例属性的优先级高于类属性
外部无妨访问私有属性(__),可以通过定义实例属性来访问
查看全部 -
reduce(函数,list,初始化值)
查看全部 -
不是纯函数式编程:允许有变量
支持高阶函数:函数可以作为变量
支持闭包:可以返回函数
支持匿名函数。
查看全部 -
functools.partial(原函数,改变默认参数)
查看全部
举报