Я хочу знать, как я могу переписать Visual Basic OOP в ООП Python, это моя версия Visual Basic:
Public Class Actor
'Declare instance variables
Private _Name As String
Private _Age As Integer
Private _Area As String
Private _NumEggs() As Integer
Private _NumStudents() As Integer
Private _EggDistributionRatio() As Double
Private _BestIndex As Integer
'Define function "CheckValue" that accepts one parameter
Private Function CheckValue(ByRef Value As Integer) As Integer
If Value < 0 Then Return 0
End Function
'Use Accessor and Mutator methods, along with validation
Public WriteOnly Property Name As String
Set(value As String)
_Name = value
End Set
End Property
Public WriteOnly Property Age As Integer
Set(value As Integer)
CheckValue(value)
_Age = value
End Set
End Property
Public WriteOnly Property Area As String
Set(value As String)
_Area = value
End Set
End Property
Public Property NumEggs(ByVal index As Integer) As Integer
Get
Return _NumEggs(index)
End Get
Set(value As Integer)
CheckValue(value)
_NumEggs(index) = value
End Set
End Property
Public Property NumStudents(ByVal index As Integer) As Integer
Get
Return _NumStudents(index)
End Get
Set(value As Integer)
CheckValue(value)
_NumStudents(index) = value
End Set
End Property
Public Property EggDistributionRatio(ByVal index As Integer) As Double
Get
Return _EggDistributionRatio(index)
End Get
Set(value As Double)
_EggDistributionRatio(index) = value
End Set
End Property
Public Property BestIndex As Integer
Get
Return _BestIndex
End Get
Set(value As Integer)
_BestIndex = value
End Set
End Property
'Define subroutine "CalculatePercentage" that will accept three parameters
Public Sub CalculatePercentage(ByRef nEggs As Integer, ByRef nStudents As Integer, ByRef Percentage As Double)
If nEggs >= nStudents Then
Percentage = (nStudents / nEggs) * 100
End If
If nStudents >= nEggs Then
Percentage = (nEggs / nStudents) * 100
End If
End Sub
'Declare global variable
Private BestSchool As Double
'Define function "DetermineBestIndex" that will accept some parameters
Public Function DetermineBestIndex() As Integer
Dim arrSize As Integer
arrSize = _EggDistributionRatio.Length - 1
BestSchool = _EggDistributionRatio(1)
DetermineBestIndex = 1
For I As Integer = 1 To arrSize
If BestSchool < _EggDistributionRatio(I) Then
BestSchool = _EggDistributionRatio(I)
DetermineBestIndex = I
End If
Next I
End Function
'Define subroutine "AssignValues" that accepts one parameter
Public Sub AssignValues(ByRef arrSchools() As Double)
Dim arrSize As Integer
arrSize = arrSchools.Length - 1
For I As Integer = 1 To arrSize
arrSchools(I) = BestSchool
Next I
End Sub
'Define constructor method that will resize the array
Public Sub New(ByVal nSchools As Integer)
ReDim _NumEggs(nSchools)
ReDim _NumStudents(nSchools)
ReDim _EggDistributionRatio(nSchools)
End Sub
End Class
А затем это моя версия Python:
class Actor:
#Declare instance variables
__Name = ""
__Age = 0
__Area = ""
__NumEggs = list()
__NumStudents = list()
__EggDistRatio = list()
__BestIndex = 0
#Define function "CheckValue"
def __CheckValue(intValue):
if intValue < 0:
return 0
else:
return intValue
#Use accessor and mutator methods along with validation
#Define write only property for "Name" attribute
def setName(self,value):
self.__Name = value
Name = property(setName)
#Define write only property for "Age" attribute
def setAge(self,value):
self.__Age = self.__CheckValue(value)
Age = property(setAge)
#Define write only property for "Area" attribute
def setArea(self,value):
self.__Area = value
Area = property(setAge)
#Define property for "NumEggs" attribute
def getNumEggs(self):
return self.__NumEggs[index]
def setNumEggs(self,value):
self.__NumEggs[index] = self.__CheckValue(value)
NumEggs = property(getNumEggs,setNumEggs)
#Define property for "NumStudents" attribute
def getNumStudents(self):
return self.__NumStudents[index]
def setNumStudents(self,value):
self.__NumStudents[index] = self.__CheckValue(value)
NumStudents = property(getNumStudents,setNumStudents)
#Define property for "EggDistRatio" attribute
def getEggDistRatio(self):
return self.__EggDistRatio[index]
def setEggDistRatio(self,value):
self.__EggDistRatio[index] = self.__CheckValue(int(value))
EggDistRatio = property(getEggDistRatio,setEggDistRatio)
#Define property for "BestIndex" attribute
def getBestIndex(self):
return self.__BestIndex
def setBestIndex(self,value):
self.__BestIndex = self.__CheckValue(value)
BestIndex = property(getBestIndex,setBestIndex)
#Define function "CalculatePercentage"
def CalculatePercentage(self,nEggs,nStudents,Percentage):
if nEggs >= nStudents:
Percentage = (nStudents/nEggs) * 100
elif nStudents >= nEggs:
Percentage = (nEggs/nStudents) * 100
#Define function "DetermineBestIndex"
def DetermineBestIndex(self):
return self.__EggDistRatio.index(max(self.__EggDistRatio))
#Define constructor method
def __init__(self,nSchools):
self.__NumEggs = [0 for e in range(nSchools)]
self.__NumStudents = [0 for s in range(nSchools)]
self.__EggDistRatio = [0.0 for r in range(nSchools)]
* 1006Причина, по которой я хочу придерживаться визуального базового формата, заключается в том, что это стандарт, которому меня учили программировать в ООП, и я считаю его очень полезным.Теперь проблема заключается в том, что каждый раз, когда я пытаюсь запустить версию Python, я сталкиваюсь с этой ошибкой:
Message File Name Line Position
Traceback
<module> C:\Users\My User\source\other\Python\P2018B - 03\P2018B - 03.py 35
> AttributeError: can't set attribute
Не могли бы вы помочь мне решить эту ошибку, так как я исчерпал все свои параметры отладки и могу пройти?этот момент.
Заранее спасибо