python中的类继承
你好,我的练习题是 在自己的文件中创建一个测量类,该文件扩展了测量类。以下是构造函数的标题: 定义初始化(自我,数据库) YPSurvey构造函数将调用Survey类构造函数来创建连接。YPSurvey类将从数据库的连接对象创建自己的私有游标 这就是我的调查课的样子python中的类继承,python,sql,class,inheritance,Python,Sql,Class,Inheritance,你好,我的练习题是 在自己的文件中创建一个测量类,该文件扩展了测量类。以下是构造函数的标题: 定义初始化(自我,数据库) YPSurvey构造函数将调用Survey类构造函数来创建连接。YPSurvey类将从数据库的连接对象创建自己的私有游标 这就是我的调查课的样子 """ File survey.py Accesses the demographics data in a sqlite3 survey database """ import sqlite3 class Surve
"""
File survey.py
Accesses the demographics data in a sqlite3 survey database
"""
import sqlite3
class Survey:
"""Represents survey information contained in a Demographic table"""
def __init__(self, database):
"""Constructor creates a Survey object and connects to a
Survey database using the input database parameter.
A cursor is also initialized to execute queries and hold the data.
Initializes a List for use in retrieving demographic information."""
self.__databaseName = database
self.__conn = sqlite3.connect(database)
self.__cur = self.__conn.cursor()
self.__demographicList = list()
def __str__(self):
"""Returns the database name"""
return print("Connected to "+str(self.__databaseName))
def getConn(self):
"""Returns the database connection for use by child class objects"""
return self.__conn
def clearDemographicList(self):
"""Clears the demographicList for reuse"""
self.__demographicList.clear()
def getNumberOfPersonIDs(self):
"""Returns the total number of people who took the survey"""
self.__cur.execute('Select count(PersonID) from Demographics')
for row in self.__cur:
total = row
return total
def getNumberByDemographic(self, userDemographic):
"""Returns a copy of the demographicList, filled with the number of
people in a particular demographic.
Example: if userDemographic = "Gender", demographicList will contain
a list of tuples with the number of females and males who took the survey"""
self.clearDemographicList()
self.__cur.execute("Select "+userDemographic+", count(?) from Demographics group by "
+userDemographic,(userDemographic,) )
for row in self.__cur:
self.__demographicList.append(row)
return self.__demographicList
import sqlite3
class YPSurvey_Final:
def __init__(self, database):
self.__databaseName = database
self.__conn = sqlite3.connect(database)
self.__cur = self.__conn.cursor()
self.__phobiaList = list()
self.__demographicList = list()
self.__phobia_nameList = list()
def getNumberOfEachPhobia(self):
"""Returns the total number of people who took the survey"""
self.__cur.execute("SELECT Phobia, COUNT(*) FROM Phobias GROUP BY Phobia ORDER BY Phobia DESC")
for row in self.__cur:
self.__phobiaList.append(row)
return self.__phobiaList
def phobias(self):
print('I counted this many phobias: {}'.format(len(self.__phobiaList)))
for phobia in self.__phobiaList:
print('Phobia and # of people: {}'.format(phobia))
def phobias_names(self):
print('There is this many:{}'.format(len(self.__phobia_nameList)))
for phobias_n in self.__phobia_nameList:
print ("test",phobias_n)
def clearDemographicList(self):
"""Clears the demographicList for reuse"""
self.__demographicList.clear()
def getNumberByDemographic(self, userDemographic, phobia_name):
self.clearDemographicList()
self.__cur.execute("SELECT "+userDemographic+", phobia, 1.0 * COUNT(*) / (SELECT COUNT(*) FROM Phobias) AS percentage FROM Demographics,Phobias WHERE Phobia = ? GROUP BY "+userDemographic+"",(phobia_name,))
for row in self.__cur:
self.__demographicList.append(row)
return self.__demographicList
def demographics(self):
for demographic in self.__demographicList:
print(demographic)
def phobia_names(self):
for phobia_name in self.__phobia_nameList:
print(phobia_name)
这是我的课看起来像
"""
File survey.py
Accesses the demographics data in a sqlite3 survey database
"""
import sqlite3
class Survey:
"""Represents survey information contained in a Demographic table"""
def __init__(self, database):
"""Constructor creates a Survey object and connects to a
Survey database using the input database parameter.
A cursor is also initialized to execute queries and hold the data.
Initializes a List for use in retrieving demographic information."""
self.__databaseName = database
self.__conn = sqlite3.connect(database)
self.__cur = self.__conn.cursor()
self.__demographicList = list()
def __str__(self):
"""Returns the database name"""
return print("Connected to "+str(self.__databaseName))
def getConn(self):
"""Returns the database connection for use by child class objects"""
return self.__conn
def clearDemographicList(self):
"""Clears the demographicList for reuse"""
self.__demographicList.clear()
def getNumberOfPersonIDs(self):
"""Returns the total number of people who took the survey"""
self.__cur.execute('Select count(PersonID) from Demographics')
for row in self.__cur:
total = row
return total
def getNumberByDemographic(self, userDemographic):
"""Returns a copy of the demographicList, filled with the number of
people in a particular demographic.
Example: if userDemographic = "Gender", demographicList will contain
a list of tuples with the number of females and males who took the survey"""
self.clearDemographicList()
self.__cur.execute("Select "+userDemographic+", count(?) from Demographics group by "
+userDemographic,(userDemographic,) )
for row in self.__cur:
self.__demographicList.append(row)
return self.__demographicList
import sqlite3
class YPSurvey_Final:
def __init__(self, database):
self.__databaseName = database
self.__conn = sqlite3.connect(database)
self.__cur = self.__conn.cursor()
self.__phobiaList = list()
self.__demographicList = list()
self.__phobia_nameList = list()
def getNumberOfEachPhobia(self):
"""Returns the total number of people who took the survey"""
self.__cur.execute("SELECT Phobia, COUNT(*) FROM Phobias GROUP BY Phobia ORDER BY Phobia DESC")
for row in self.__cur:
self.__phobiaList.append(row)
return self.__phobiaList
def phobias(self):
print('I counted this many phobias: {}'.format(len(self.__phobiaList)))
for phobia in self.__phobiaList:
print('Phobia and # of people: {}'.format(phobia))
def phobias_names(self):
print('There is this many:{}'.format(len(self.__phobia_nameList)))
for phobias_n in self.__phobia_nameList:
print ("test",phobias_n)
def clearDemographicList(self):
"""Clears the demographicList for reuse"""
self.__demographicList.clear()
def getNumberByDemographic(self, userDemographic, phobia_name):
self.clearDemographicList()
self.__cur.execute("SELECT "+userDemographic+", phobia, 1.0 * COUNT(*) / (SELECT COUNT(*) FROM Phobias) AS percentage FROM Demographics,Phobias WHERE Phobia = ? GROUP BY "+userDemographic+"",(phobia_name,))
for row in self.__cur:
self.__demographicList.append(row)
return self.__demographicList
def demographics(self):
for demographic in self.__demographicList:
print(demographic)
def phobia_names(self):
for phobia_name in self.__phobia_nameList:
print(phobia_name)
因此,我不明白“扩展调查类”是什么意思,以及如何将我的YP调查类更改为与调查类一起使用?如果您希望从调查类扩展YP调查最终版,则需要替换YP调查最终版的类定义,如下所示:
class YPSurvey_Final(Survey):
....
“extends”通常在OOP中用来表示继承。您可以使您的类从另一个类继承,如下所示:
class MyClass(BaseClass):
def __init__(self):
super().__init__()
您可以使用super
方法将调用委托给super-/baseclass构造函数,如下所示:
class MyClass(BaseClass):
def __init__(self):
super().__init__()