简体   繁体   中英

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. 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). 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. Im new to python so any help would be great. 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'. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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