-
前兩個數(shù)的值分別為 0 、1 或者 1、1; 從第 3 個數(shù)字開始,它的值是前兩個數(shù)字的和; 為了紀(jì)念他,人們將滿足以上兩個特征的數(shù)列稱為斐波那契數(shù)列。
查看全部 -
# 定義Person類
class Person:
? ? def __init__(self, name, age):
? ? ? ? self.name = name
? ? ? ? self.age = age
# 定義SkillMixin類
class SkillMixin:
? ? def show_skills(self):
? ? ? ? print(f"{self.name}的技能有:{self.skills}")
# 定義BasketballMixin類
class BasketballMixin(SkillMixin):
? ? def __init__(self):
? ? ? ? self.skills = ["打籃球"]
# 定義FootballMixin類
class FootballMixin(SkillMixin):
? ? def __init__(self):
? ? ? ? self.skills = ["踢足球"]
# 定義Student類
class Student(Person):
? ? def __init__(self, name, age, grade):
? ? ? ? super().__init__(name, age)
? ? ? ? self.grade = grade
# 定義Teacher類
class Teacher(Person):
? ? def __init__(self, name, age, subject):
? ? ? ? super().__init__(name, age)
? ? ? ? self.subject = subject
# 定義會打籃球的學(xué)生類
class BasketballStudent(Student, BasketballMixin):
? ? def __init__(self, name, age, grade):
? ? ? ? Student.__init__(self, name, age, grade)
? ? ? ? BasketballMixin.__init__(self)
# 定義會踢足球的老師類
class FootballTeacher(Teacher, FootballMixin):
? ? def __init__(self, name, age, subject):
? ? ? ? Teacher.__init__(self, name, age, subject)
? ? ? ? FootballMixin.__init__(self)
# 測試代碼
bs = BasketballStudent("小明", 18, "高三")
bs.show_skills()
ft = FootballTeacher("李老師", 35, "數(shù)學(xué)")
ft.show_skills()
查看全部 -
向函數(shù)傳遞任意關(guān)鍵字參數(shù)的方法**kw
for k,v in kw.items():
查看全部 -
class Student()定義的時候,需要在括號內(nèi)寫明繼承的類Person
在__init__()方法,需要調(diào)用super(Student, self).__init__(name, gender),來初始化從父類繼承過來的屬性
查看全部 -
class Person(object):
? ?def __init__(self, name, gender):
? ? ? ?self.name = name
? ? ? ?self.gender = gender
class Student(Person):
? ?def __init__(self, name, gender, score):
? ? ? ?super(Student, self).__init__(name, gender)
? ? ? ?self.score = score
class Teacher(Person):
? ?def __init__(self, name, gender, course):
? ? ? ?super(Teacher, self).__init__(name, gender)
? ? ? ?self.course = course
p = Person('Tim', 'Male')
s = Student('Bob', 'Male', 88)
t = Teacher('Alice', 'Female', 'English')
# 判斷t是否是Person類型
if isinstance(t, Person):
? ?print("t is a Person")
else:
? ?print("t is not a Person")
# 判斷t是否是Student類型
if isinstance(t, Student):
? ?print("t is a Student")
else:
? ?print("t is not a Student")
# 判斷t是否是Teacher類型
if isinstance(t, Teacher):
? ?print("t is a Teacher")
else:
? ?print("t is not a Teacher")
# 判斷t是否是object類型
if isinstance(t, object):
? ?print("t is an object")
else:
? ?print("t is not an object")查看全部 -
類屬性和實例屬性同名,實力屬性優(yōu)先;
不能在外部更改類屬性;
__代表類屬性私有化,外部無法訪問;
1.帶有兩個下劃線開頭的函數(shù)是聲明該屬性為私有,不能在類地外部被使用或直接訪問。
2.init函數(shù)(方法)支持帶參數(shù)的類的初始化 ,也可為聲明該類的屬性
3.init函數(shù)(方法)的第一個參數(shù)必須是 self(self為習(xí)慣用法,也可以用別的名字),后續(xù)參數(shù)則可 以自由指定,和定義函數(shù)沒有任何區(qū)別。查看全部 -
class Animal(object):
? ?__localtion = 'Asia'
? ?def __init__(self, name, age):
? ? ? ?self.name = name
? ? ? ?self.age = age
? ?@classmethod
? ?def set_localtion(cls, localtion):
? ? ? ?cls.__localtion = localtion
? ?@classmethod
? ?def get_localtion(cls):
? ? ? ?return cls.__localtion
print(Animal.get_localtion()) # ==> Asia
Animal.set_localtion('Afica')
print(Animal.get_localtion()) # ==> Africa使用了類屬性和類方法的概念。
類屬性是定義在類中且在所有實例之間共享的變量,類方法是定義在類中且可以直接通過類名調(diào)用的函數(shù)。
私有屬性是以兩個下劃線開頭的變量,它們只能在類的內(nèi)部訪問,不能在外部訪問。
為了讓外部能夠獲取私有屬性的值,我們可以定義一個類方法,它可以通過cls參數(shù)訪問到類屬性,并返回它的值。
我們就可以通過Counter.get_count()來獲取__count的值了。
查看全部 -
class Animal:
? ? def __init__(self, age, name, location):
? ? ? ? self.__age = age
? ? ? ? self.__name = name
? ? ? ? self.__location = location
? ? def set_age(self, new_age):
? ? ? ? if isinstance(new_age, int) and new_age > 0:
? ? ? ? ? ? self.__age = new_age
? ? ? ? else:
? ? ? ? ? ? print("Invalid age")
? ? def get_age(self):
? ? ? ? return self.__age
? ? def set_name(self, new_name):
? ? ? ? if isinstance(new_name, str) and new_name:
? ? ? ? ? ? self.__name = new_name
? ? ? ? else:
? ? ? ? ? ? print("Invalid name")
? ? def get_name(self):
? ? ? ? return self.__name
? ? def set_location(self, new_location):
? ? ? ? if isinstance(new_location, str) and new_location:
? ? ? ? ? ? self.__location = new_location
? ? ? ? else:
? ? ? ? ? ? print("Invalid location")
? ? def get_location(self):
? ? ? ? return self.__location
a = Animal(3, "Tom", "New York")
print(a.get_age())
a.set_age(5)
print(a.get_age())
print(a.get_name())
a.set_name("Jerry")
print(a.get_name())
print(a.get_location())
a.set_location("London")
print(a.get_location())
查看全部 -
# 定義一個Animal類
class Animal:
? ?# 初始化方法,接收age, name, location參數(shù)
? ?def __init__(self, age, name, location):
? ? ? ?# 把參數(shù)賦值給私有屬性,用雙下劃線開頭表示
? ? ? ?self.__age = age
? ? ? ?self.__name = name
? ? ? ?self.__location = location
? ?# 定義一個方法,用來修改私有屬性__age的值
? ?def set_age(self, new_age):
? ? ? ?# 判斷新的年齡是否合法,如果是正整數(shù),則修改,否則提示錯誤
? ? ? ?if isinstance(new_age, int) and new_age > 0:
? ? ? ? ? ?self.__age = new_age
? ? ? ?else:
? ? ? ? ? ?print("Invalid age")
? ?# 定義一個方法,用來獲取私有屬性__age的值
? ?def get_age(self):
? ? ? ?# 返回私有屬性__age的值
? ? ? ?return self.__age
? ?# 定義一個方法,用來修改私有屬性__name的值
? ?def set_name(self, new_name):
? ? ? ?# 判斷新的名字是否合法,如果是非空字符串,則修改,否則提示錯誤
? ? ? ?if isinstance(new_name, str) and new_name:
? ? ? ? ? ?self.__name = new_name
? ? ? ?else:
? ? ? ? ? ?print("Invalid name")
? ?# 定義一個方法,用來獲取私有屬性__name的值
? ?def get_name(self):
? ? ? ?# 返回私有屬性__name的值
? ? ? ?return self.__name
? ?# 定義一個方法,用來修改私有屬性__location的值
? ?def set_location(self, new_location):
? ? ? ?# 判斷新的位置是否合法,如果是非空字符串,則修改,否則提示錯誤
? ? ? ?if isinstance(new_location, str) and new_location:
? ? ? ? ? ?self.__location = new_location
? ? ? ?else:
? ? ? ? ? ?print("Invalid location")
? ?# 定義一個方法,用來獲取私有屬性__location的值
? ?def get_location(self):
? ? ? ?# 返回私有屬性__location的值
? ? ? ?return self.__location
# 創(chuàng)建一個Animal對象,傳入年齡,名字和位置參數(shù)
a = Animal(3, "Tom", "New York")
# 調(diào)用get_age方法,打印對象的年齡
print(a.get_age())
# 調(diào)用set_age方法,修改對象的年齡為5
a.set_age(5)
# 再次調(diào)用get_age方法,打印對象的年齡
print(a.get_age())
# 調(diào)用get_name方法,打印對象的名字
print(a.get_name())
# 調(diào)用set_name方法,修改對象的名字為"Jerry"
a.set_name("Jerry")
# 再次調(diào)用get_name方法,打印對象的名字
print(a.get_name())
# 調(diào)用get_location方法,打印對象的位置
print(a.get_location())
# 調(diào)用set_location方法,修改對象的位置為"London"
a.set_location("London")
# 再次調(diào)用get_location方法,打印對象的位置
print(a.get_location())查看全部 -
def __init__()
下劃線是兩個字符_連續(xù)?
查看全部 -
多個繼承,打印順序查看全部
-
class Animal(object):
? ? __count = 5
? ? @classmethod
? ? def get_method(cls):
? ? ? ? return cls.__count
? ??
print(Animal.get_method())
查看全部 -
class Animal(object):
? ? def __init__(self, name, age):
? ? ? ? self.__name = name
? ? ? ? self.__age = age
? ??
? ? def get_name(self):
? ? ? ? return self.__name
? ? def set_name(self,name):
? ? ? ? self.__name = name
? ? ? ??
dog = Animal('wangwang', 1)
print(dog.get_name())
dog.set_name('haha')
print(dog.get_name())
查看全部 -
class Animal(object):
? ? count = 0
? ? def __init__(self, name, age):
? ? ? ? self.name = name
? ? ? ? self.age = age
? ? ??
? ? ? ??
dog = Animal("gougou", 2)
Animal.count = Animal.count + 1
print(dog.count)
cat = Animal("maomao", 3)
Animal.count = Animal.count + 1
print(cat.count)
print(Animal.count)
查看全部 -
class Animal(object):
? ? def __init__(self, name, age):
? ? ? ? self.name = name
? ? ? ? self.age = age
? ? ? ??
dog = Animal("gougou", 2)
cat = Animal("maomao", 3)
print(dog.name)
print(cat.name)
查看全部
舉報