class Animal(object):
__count= 999
def __init__(self, name):
self.name = name
@classmethod
def set_count (cls, new_count):
cls.__count=new_count
@classmethod
def get_count(cls):
return cls.__count
print(Animal.get_count())
Animal.set_count(9868)
print(Animal.get_count())
__count= 999
def __init__(self, name):
self.name = name
@classmethod
def set_count (cls, new_count):
cls.__count=new_count
@classmethod
def get_count(cls):
return cls.__count
print(Animal.get_count())
Animal.set_count(9868)
print(Animal.get_count())
2024-10-21
class Animal(object):
def __init__(self,name,age):
self.name=name
self.age=age
pass
dog: Animal = Animal('dog',3)
cat = Animal('cat',5)
def __init__(self,name,age):
self.name=name
self.age=age
pass
dog: Animal = Animal('dog',3)
cat = Animal('cat',5)
2024-09-19
寫(xiě)的一言難盡。。。。推薦大家耐心看這個(gè)算了廖雪峰python學(xué)習(xí):https://liaoxuefeng.com/books/python/introduction/index.html
2024-09-13
類(lèi)的私有屬性和方法指的是只能在類(lèi)的內(nèi)部使用,而不能在類(lèi)外使用的屬性和方法。
2024-08-28
在python中定義私有變量和私有方法只需要在變量名或函數(shù)名前加上 "__"兩個(gè)下劃線
2024-08-28
L = ['alice', 'BOB', 'CanDY']
def trans(theStr):
return str(theStr).title()
for item in map(trans,L):
print(item)
def trans(theStr):
return str(theStr).title()
for item in map(trans,L):
print(item)
2024-07-09
class Animal (object) : pass dog = Animal() cat = Animal()
2024-06-18
class person(object): pass xiaohong = person() xiaoming = person()
2024-06-17
因?yàn)槭窃陬?lèi)上調(diào)用,而非實(shí)例上調(diào)用,因此類(lèi)方法無(wú)法獲得任何實(shí)例變量,只能獲得類(lèi)的引用,這句話是啥意思,上面都挺明白,這個(gè)話突然理解不了了
2024-05-24
p = ['bob', 'about', 'Zoo', 'Credit']
print(sorted(p, key=str.lower))
print(sorted(p, key=str.lower))
2024-05-04
class student(object)def __init__(self, name, gender, score)self.name = nameself.gender = genderself.score = scoredef __str__(self):return 'name: {}, gender: {}, score: {}'.format(self.name, self.gender, self.score def __repr__(self): return 'name: {}, gender: {}'.format(s太多了打不上去
2024-05-03
class pen(object):
def __init__(self, len):
self.len = len
def get_infro(self):
return 'len: {}'.format(self.len)
blackpen = pen(19)
a = blackpen.get_infro
print(a)
def __init__(self, len):
self.len = len
def get_infro(self):
return 'len: {}'.format(self.len)
blackpen = pen(19)
a = blackpen.get_infro
print(a)
2024-05-01