-
# Enter a code
class Person(object):
? ? def __init__(self, name, gender, **kw):
? ? ? ? self.name = name
? ? ? ? self.gender = gender
? ? ? ? for k, v in kw.items():
? ? ? ? ? ? setattr(self, k, v)
p = Person('Bob', 'Male', age=18, course='Python')
print(p.age)
print(p.course)
查看全部 -
xiaohong.name = 'xiaohong'
xiaohong.sex = 'girl'
xiaohong.age = 13
print(xiaohong.name)
print(xiaohong.sex)
print(xiaohong.age)查看全部 -
xiaohong.age = xiaohong.age + 1查看全部
-
class Person(object):? pass xiaohong = Person() xiaoming = Person()查看全部
-
class Person(): pass? class Person(object):? pass查看全部
-
class Person:? pass查看全部
-
重點(diǎn)
查看全部 -
li=['bob', 'about', 'Zoo', 'Credit']
def f (x):
return li[0]
l=sorted(li,key=f)
print(l)查看全部 -
li=list(range(1,101))
print(li)
def f(x):
return (x**0.5) % 1 == 0
for i in filter (f,li):
print(i)查看全部 -
li=['lalice', 'BOB', 'CanDY']
def f(s):
return s.capitalize()
for i in map(f,li):
l=list(map(f,li))
print(l)
查看全部 -
#請參考Student類,編寫Teacher類,老師擁有任教某個科目的屬性。
class Students:
def __init__(self, name, gender,score ):
self.name = name
self.gender = gender
self.score = score
class Taecher (Students):
def __init__(self,name,gender,score,account):
super().__init__(name,gender,score)
self.account=account
def get_info (self):
return self.name,self.gender,self.score,self.account
tt=Taecher("張三","女",90,"語文")
print(tt.get_info())
?查看全部 -
#果將類屬性count改為私有屬性__count,則外部無法獲取__count,但是可以通過一個類方法獲取,請編寫類方法獲得__count值。
class Animal(object):
__count= 999
def __init__(self, name, age):
self.name = name
self.age = age
@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())
在類上調(diào)用方法查看全部 -
#Animal類的age、name、localtion定義成私有屬性,并定義對應(yīng)的方法修改和獲取他們的值。 class Animal : def __init__(self,name,age,localtion): self.__name=name self.__age=age self.__localtion=localtion def get_name (self): return self.__name def change_name (self,new_name): self.__name=new_name def get_age (self): return self.__age def change_age (self,new_age): self.__age=new_age def get_location (self): return self.__localtion def change_localtion(self,new_localtion): self.__localtion=new_localtion dog=Animal('dog',88,'鄭州') print(dog.get_name(),dog.get_location()) dog.change_localtion('北京') print(dog.get_name(),dog.get_location())查看全部
-
# 實(shí)例私有屬性
class Animal(object):
??? def __init__(self, name, age):
??????? self.name = name
??????? self._age = age
dog = Animal('wangwang',8)
print(dog.name) # ==> wangwang
print(dog._age) # ==> 1#不能從外部訪問私有屬性查看全部 -
請給 Animal類添加一個類屬性 count,每創(chuàng)建一個實(shí)例,count 屬性就加 1,這樣就可以統(tǒng)計(jì)出一共創(chuàng)建了多少個 Animal的實(shí)例。
class Animal ():
count =0
def __init__(self,name,age):
self.name=name
self.age=age
count+=1
dog= Animal ('dog',2)
cat= Animal ('cat',6)
print(dog.name,dog.age)
print(cat.name,cat.age)
查看全部
舉報