最赞回答 / LVXC
该Animal类中有3个实例属性,分别是name,age和location,并且这3个属性都是带双下划线(__)前缀的,说明是私有属性。私有属性在类的外部不能被直接访问,但可以在类的内部直接访问,所以定义了六个方法分别获取(get)和设置(set)这三个私有属性的值,在类的外部可以通过这六个方法分别获取或设置实例中这3个属性的值。__init__实例方法是构造函数,在创建实例的时候可以方便同时设置实例的属性的初始值。
2021-10-08
最赞回答 / MrQinJS
执行最后有一个None,是因为python函数使用return返回值,如果不用return, 而用print输出值,这个函数默认还有一个返回值为None .
2021-09-29
最赞回答 / MrQinJS
可以用私有属性。1、 super()._init_(param,param)这句话,调用了父类的初始化方法,那么如果需要传父类初始化方法的全部参数,但可以设置默认参数,就可以不传color这个参数,父类通过二个下划线设置私有属性。子类不能继承。
2021-09-29
class Animal(object):
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
Animal.count = Animal.count + 1
cat = Animal('lucy', 10)
dog = Animal('lily', 2)
dog2 = Animal('lily', 2)
cat2 = Animal('lily', 2)
print(Animal.count)
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
Animal.count = Animal.count + 1
cat = Animal('lucy', 10)
dog = Animal('lily', 2)
dog2 = Animal('lily', 2)
cat2 = Animal('lily', 2)
print(Animal.count)
2021-09-07
class Animal(object):
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
#dog = Animal('diandian', 8)
#cat = Animal('mimi', 3)
Animal.count = Animal.count + 1
dog = ("doudou", 3)
print(Animal.count)
cat = ("diandian",4)
print(Animal.count)
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
#dog = Animal('diandian', 8)
#cat = Animal('mimi', 3)
Animal.count = Animal.count + 1
dog = ("doudou", 3)
print(Animal.count)
cat = ("diandian",4)
print(Animal.count)
2021-09-03
class Anaml():
pass
def _init_(self,name,age)
self.name = name
self.age = age
dog = Anmial('diandian ',8)
cat = Anmial('mimi',5)
print(dog.name)
print(dog.age)
print(cat.name)
print(cat.age)
pass
def _init_(self,name,age)
self.name = name
self.age = age
dog = Anmial('diandian ',8)
cat = Anmial('mimi',5)
print(dog.name)
print(dog.age)
print(cat.name)
print(cat.age)
2021-09-03
class Animal:
__count=0
def __init__(self,name):
self.name=name
Animal.__count+=1
@classmethod
def get_count(self):
return self.__count
dog=Animal('liu')
print(dog.get_count())
cat=Animal('wang')
print(cat.get_count())
__count=0
def __init__(self,name):
self.name=name
Animal.__count+=1
@classmethod
def get_count(self):
return self.__count
dog=Animal('liu')
print(dog.get_count())
cat=Animal('wang')
print(cat.get_count())
2021-09-01
# Enter a code
class Animal():
pass
dog=Animal()
cat=Animal()
dog.age=8
dog.name='jiji'
print(dog)
print(dog.name)
class Animal():
pass
dog=Animal()
cat=Animal()
dog.age=8
dog.name='jiji'
print(dog)
print(dog.name)
2021-09-01
class Animal:pass
dog=Animal()
cat=Animal()
print(dog)
dog=Animal()
cat=Animal()
print(dog)
2021-09-01
Python基础我认真看了几遍,进阶我快速上手也操作了一遍,
翻翻网上python的面试题,我懵圈了啊,为啥很多知识点都没有接触到啊?
比如什么是 Flask?还有猴子补丁,晕
翻翻网上python的面试题,我懵圈了啊,为啥很多知识点都没有接触到啊?
比如什么是 Flask?还有猴子补丁,晕
2021-08-27