简体   繁体   English

如何创建具有某些属性的 Student 类并稍后从其中的方法调用它

[英]how to create Student class with some attributes and calling it later from method within

It is necessary to create a template for creating objects of type Student.有必要创建一个模板来创建 Student 类型的对象。 Each Student should have the following characteristics:Name, address, phone, course, index number Each object that represents a student should have the following method: get info() getInfo() method should print data about one student (for example Name: Jon, address:JonJon Phone: 1231 Subject:math Index number: 11/22).每个学生应具有以下特征:姓名、地址、电话、课程、索引号 每个代表学生的对象都应具有以下方法: get info() getInfo() 方法应打印有关一名学生的数据(例如姓名:Jon , 地址:JonJon 电话:1231 学科:数学 索引号:11/22)。 Finally, based on the created template, it is necessary to create three objects that represent three students with data as desired.最后,根据创建的模板,需要根据需要创建三个对象,代表三个学生的数据。 Above the three created objects, it is necessary to call the method for printing data - getInfo() and display the information with the print command.在创建的三个对象之上,需要调用打印数据的方法——getInfo(),用打印命令显示信息。 Im new to python so any help would be great.我是 python 的新手,所以任何帮助都会很棒。 Thanks谢谢

class Student:
    def __init__(self, name, adress, phone, course, index_number): 
        self.name = name
        self.adress = adress
        self.phone = phone
        self.course = course
        self.index_number = index_number

    @classmethod
    def get_info(self):
    s1 = Student("")
    s2 = Student("")
    s3 = Student("")
    print(s1, s2, s3)

nothing gets printed out什么都没有打印出来

A class method is a method that is bound to class rather than a class instance.类方法是绑定到类而不是类实例的方法。 One example of when this can be useful is in the Factory pattern.这在何时有用的一个例子是在工厂模式中。 A class method can accept many parameters, but the first has to be the class itself.类方法可以接受很多参数,但第一个必须是类本身。 Looking to your code, it seems that you did pass the class as a parameter, although as it was pointed out in the comments, it's usually written as 'cls'.查看您的代码,您似乎确实将类作为参数传递,尽管正如评论中指出的那样,它通常写为“cls”。 In any case, you still didn't add the other parameters that the class accepts and, more importantly, you're not using the class (remember, you passed it as a parameter) to create the instances.在任何情况下,您仍然没有添加该类接受的其他参数,更重要的是,您没有使用该类(记住,您将它作为参数传递)来创建实例。

Fixing the code you wrote,修复您编写的代码,

class Student:
    def __init__(self, name, adress, phone, course, index_number): 
        self.name = name
        self.adress = adress
        self.phone = phone
        self.course = course
        self.index_number = index_number

    @classmethod
    def get_info(cls, name, adress, phone, course, index_number):
        s1 = cls(name, adress,phone, course, index_number)
        s2 = cls(name, adress,phone, course, index_number)
        s3 = cls(name, adress,phone, course, index_number)
        print(s1, s2, s3)

Student.get_info('test', 'street', '42', 'so','1')

Notice two things, first, now I'm using the class passed as a parameter, and the rest of the required parameters to create instances of the class within the class method.请注意两件事,首先,现在我使用作为参数传递的类,以及在类方法中创建类实例的其余必需参数。 As I mentioned, this is useful because you can return a specific object depending on any condition you want to set.正如我提到的,这很有用,因为您可以根据要设置的任何条件返回特定对象。

Second, this is not the adequate approximation to answer the problem you're trying to solve.其次,这不是回答您要解决的问题的适当近似值。 The question is asking you to create a class with a method that outputs the parameters of the object, so the method you want has to be bound to the object, and the solution is much simpler than the one you're trying to implement.问题是要求你创建一个类,它有一个输出对象参数的方法,所以你想要的方法必须绑定到对象,解决方案比你试图实现的要简单得多。

class Student:
    def __init__(self, name, adress, phone, course, index_number): 
        self.name = name
        self.adress = adress
        self.phone = phone
        self.course = course
        self.index_number = index_number
    
    def get_info(self): #passing the object so it has access to its parameters
        print(self.name) #here you print anything you want from the object
                         #using 'self' as if it was the actual name

#outside of the class you instantiate the class 
s1 = Student('test', 'street', '42', 'so','1')
s1.get_info()

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

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