这是整个计划。我不确定为什么,但错误说明了这一点,但我使用一个单独的.py程序来测试这个类中的所有函数,我遇到了这个错误,我似乎无法找到解决方案。
File "C:\Python\PythonLab\PythonLab.py\classes.py", line 73, in printEmployeeNames Supervisor.printName(worker) File "C:\Python\PythonLab\PythonLab.py\classes.py", line 56, in printName print(str(self.name) + "'" + str(self.department)) AttributeError: 'str' object has no attribute 'name'
class Employee:
def __init__(self, fullname, datestart, monthstart, yearstart):
self.fullname = fullname
self.datestart = datestart
self.monthstart = monthstart
self.yearstart = yearstart
def getService(self):
from datetime import date
current_date = date.today()
date1 = date(self.yearstart, self.monthstart, 1)
date_now = date(current_date.year, current_date.month, 1)
serviceTime = date_now - date1
day_format = serviceTime.days
years = int((day_format/365))
months = int(((day_format % 365)/30))
if day_format < 0:
return('Still In Service')
if day_format == 1:
return("Last Service Time was Yesterday")
if day_format < 365:
return("Last Service Time was " + str(months) + " months ago.")
if day_format > 365:
return('Last Service Time was ' + str(years) + "-" + str(months) + " ago.")
def printName(self):
print(self.fullname)
def setName(self, name):
self.fullname = name
class Supervisor(Employee):
def __init__(self, name, datestart, department):
Employee.__init__(self, name, int(datestart[0:2]), int(datestart[2:4]), int(datestart[5:8]))
self.employees = {}
self.contact_info = {}
self.department = department
self.name = Employee.__name__
def getName(self):
return self.fullname
def printName(self):
print(str(self.name) + "'" + str(self.department))
def setName(self, name, department):
self.name = name
self.department = department
def addEmployee(self, Employee):
self.employees[str(Supervisor.getName(self))] = Employee
def isManager(self):
if self.employees:
return True
else:
return False
def printEmployeeNames(self):
for worker in self.employees:
Supervisor.printName(worker)
def removeEmployee(self, employeename):
for worker in self.employees:
if employeename in self.employees:
del self.employees[employeename]
def getContactInfo(self):
return self.employees
def setContactInfo(self, phone, fax, email):
self.contact_info["phone"] = phone
self.contact_info["fax"] = fax
self.contact_info["email"] = email
def getPhone(self):
return self.contact_info["phone"]
def getFax(self):
return self.contact_info["fax"]
def getEmail(self):
return self.contact_info["email"]
答案 0 :(得分:1)
dict
是for worker in self.employees:
Supervisor.printName(worker)
。迭代它意味着迭代它的键。因此,在此代码中
worker
for worker in self.employees:
Supervisor.printName(self.employees[worker])
是一个字符串。将其更改为:
for name, worker in self.employees.items(): # iterates key-value pairs
Supervisor.printName(worker)
或者更重要的是:
public function store(Request $request, $post_id){
$like = Like::create([ 'user_id' => Auth::user()->id, 'post_id' => $post_id]);
$post = Post::whereId($post_id)->first();
if( $post->user_id !== Auth::user()->id ) {
//SEND Notification code here
}
}