类应该用一个名字和一个生日初始化,但是生日不应该是None 应该有两种方法,名字和生日 “挫折日将他们的生日定为一个日期”这是我得到的指示,这是大图中的一小部分。在
我正在尝试设置用户输入的日期、月份和年份…这将在以后用于其他计算。。在
代码:
class person(object):
def __init__(self, name):
self.name = name
self.setBirthday = None
#getName returns the name of the person
def getName(self):
return self.name
#setBirthday sets their Birthday to a date
def setBirthday(self):
day = (raw_input('Please enter the date of the month you were born on here ->'))
return self.day
month = (raw_input('Please enter the month of the year you were born on here ->'))
return self.month
year = (raw_input('Please enter the year you were born on here ->'))
return self.year
varName = person(raw_input('Please enter your name here ->'))
varDate = person.setBirthday()
你能给我指出正确的方向吗?这个类的东西真的让我困惑…我需要让用户能够输入日,月和年,以备日后使用。错误在下面的注释中。我确实删除了代码中的返回。在
setBirthday
定义为一个方法,但是一旦实例化该类,该方法就会消失,setBirthday
只指向None
。注意,birthday
和{var = (input())
不是这样的场合。在@property
修饰符标记的方法)。在MyClass.methodname()
,您需要向它传递一个类的实例。这就是为什么标准Python样式(PEP-8)要求类名为Uppercase
或{lowercase
或{person
使它看起来像是某个实际类的实例,这使得它很容易被滥用。在代码应该是这样的:
相关问题 更多 >
编程相关推荐