简体   繁体   English

Python 2.7类,用户输入生日,以备后用

[英]Python 2.7 classes, user input birthdates for later use

"The class should initialize with a name and a birthday, but the birthday should be None." “该类应使用名称和生日来初始化,但生日应为“无”。 "There should be two methods, name and birthday" "setBirthday sets their Birthday to a date" these are the instructions I was given, this is a small piece in the larger picture. “应该有两种方法,名称和生日”“ setBirthday将其生日设置为日期”,这是我得到的指示,这是大图中的一小部分。

I'm trying to set the day, the month and year to be entered in by the user...this will be used later for other calculations.. 我正在尝试设置要由用户输入的日期,月份和年份...这将在以后用于其他计算。

Code: 码:

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()

Can you point me in the right direction? 你能为我指出正确的方向吗? This class stuff really confuses me...I need to have the user be able to input the day, month and year to be saved for later use. 这个类的东西确实让我感到困惑...我需要让用户能够输入要保存以供以后使用的日期,月份和年份。 error is below in comments. 错误在注释中。 I did remove the returns in my code. 我确实删除了代码中的返回值。

  1. Your class defines setBirthday as a method, but as soon as you instantiate the class that method disappears and setBirthday just points to None . 您的类将setBirthday定义为方法,但是实例化该类后,该方法就会消失,并且setBirthday只是指向None Note that birthday and setBirthday are two unique names, and that class is already using the latter for that method. 需要注意的是birthdaysetBirthday是两个唯一的名称,以及类已经使用后者的这种方法。
  2. You have unnecessary parentheses. 您有不必要的括号。 Try to use only what is necessary for grouping, with the occasional extra pair when required for clarity. 尝试仅使用分组所需的内容,为清晰起见,偶尔使用额外的一对。 var = (input()) is not such an occasion. var = (input())并非这种情况。
  3. Python does not require getters and setters. Python 不需要 getter和setter。 There's no such thing as a private variable, so anyone who wants to modify something can do so to their heart's content. 没有私有变量之类的东西,因此任何想要修改某项内容的人都可以这样做。 The only thing I'm aware of that can't be redefined is a property (a method tagged with the @property decorator). 我知道的唯一无法重新定义的是属性(用@property装饰器标记的方法)。
  4. You are confusing instance variables with global variables, and when to turn each into the other. 您正在将实例变量与全局变量,以及何时将其相互转化的混淆。 The setters in that class should simply set instance variables, not return them to the caller (it's like this in Java as well, which is where you actually would use getters and setters. 该类中的setter应该只设置实例变量,而不是将它们返回给调用方(Java中也是如此,这实际上是您将使用getter和setter的地方)。
  5. When you call a class's instance methods directly, such as MyClass.methodname() , you need to pass it an instance of the class. 当直接调用类的实例方法(例如MyClass.methodname() ,您需要向其传递该类的实例。 This is why standard Python style (PEP-8) calls for class names to be Uppercase or UpperCase and other objects to be lowercase or snake_case . 这就是为什么标准Python样式(PEP-8)要求类名称为UppercaseUpperCase而其他对象为lowercasesnake_case Naming the class person made it look like an instance of something when it's actually a class, making it all too easy to misuse it. 给班级person命名实际上使它看起来像是某个事物的实例,因此很容易滥用它。

Here is what the code should look like: 代码如下所示:

class Person(object):
    def __init__(self, name):
        self.name = name
        self.birthday = None

    def getName(self):
        '''return the name of the person'''
        # this method should be removed. to get a person's name,
        # use "person.name" instead of "person.getName()"
        return self.name

    def setBirthday(self):
        '''set their Birthday to a date'''
        day = raw_input('Please enter the date of the month you were born on here ->')
        month = raw_input('Please enter the month of the year you were born on here ->')
        year = raw_input('Please enter the year you were born on here ->')
        self.birthday = int(day), int(month), int(year)

person = Person(raw_input('Please enter your name here ->'))
person.setBirthday()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM