Python 有没有更好的方法可以用面向对象编程打印一个以上的学生?

Python 有没有更好的方法可以用面向对象编程打印一个以上的学生?,python,python-3.x,oop,Python,Python 3.x,Oop,有没有办法像我下面所做的那样,在不复制和粘贴代码的情况下打印每个新学生?有可能使用循环或类似的东西吗 class Student: def __init__(self, name, course, age): self.name = name self.course = course self.age = age def roomNumber(self): if self.course == "Computing":

有没有办法像我下面所做的那样,在不复制和粘贴代码的情况下打印每个新学生?有可能使用循环或类似的东西吗

class Student:
    def __init__(self, name, course, age):
        self.name = name
        self.course = course
        self.age = age
    def roomNumber(self):
       if self.course == "Computing":
            room = "S227"
       elif self.course == "Art":
            room = "Art Studio 1"
       else:
            room = "Main hall"
       return (room)
    def parientSign(self):
       if self.age > 17:
            print("Parent doesn't need to sign")
       else:
            print("Parent needs to sign")
       return
newStudent = Student("Name One", "Computing", 18)
newStudent1 = Student("Bob Smart", "Art", 19)
newStudent2 = Student("Big Terry", "Computing", 16)
print("Student Name: ", newStudent.name)
print("Student Course: ",newStudent.course)
print("Your room number is: ", newStudent.roomNumber())
print("Your Age is: ",newStudent.age)
newStudent.parientSign()
print ("\n--------------\n")
print("Student Name: ", newStudent1.name)
print("Student Course: ",newStudent1.course)
print("Your room number is: ", newStudent1.roomNumber())
print("Your Age is: ",newStudent1.age)
newStudent1.parientSign()
print ("\n--------------\n")
print("Student Name: ", newStudent2.name)
print("Student Course: ",newStudent2.course)
print("Your room number is: ", newStudent2.roomNumber())
print("Your Age is: ",newStudent2.age)
newStudent2.parientSign()
print ("\n--------------\n")

信用&正如DeepSpace在评论中提到的那样

您可以尝试以下方法:

class Student:

    def __init__(self, name, course, age):
        self.name = name
        self.course = course
        self.age = age

    def __str__(self):
        return """Student Name: %s\n
                Student Course: %s\n
                Your room number is: %s\n
                Your Age is: %s\n
                %s""" % (self.name, self.course, self.age, self.roomNumber(), self.parentSign())

    def roomNumber(self):
        if self.course == "Computing":
            room = "S227"
        elif self.course == "Art":
            room = "Art Studio 1"
        else:
            room = "Main hall"
        return (room)

    def parentSign(self):
            return "Parent doesn't need to sign" if self.age > 17 else "Parent needs to sign"

newStudent = Student("Name One", "Computing", 18)
newStudent1 = Student("Bob Smart", "Art", 19)
newStudent2 = Student("Big Terry", "Computing", 16)

print(newStudent)
print(newStudent1)
print(newStudent2)

信用&正如DeepSpace在评论中提到的那样

您可以尝试以下方法:

class Student:

    def __init__(self, name, course, age):
        self.name = name
        self.course = course
        self.age = age

    def __str__(self):
        return """Student Name: %s\n
                Student Course: %s\n
                Your room number is: %s\n
                Your Age is: %s\n
                %s""" % (self.name, self.course, self.age, self.roomNumber(), self.parentSign())

    def roomNumber(self):
        if self.course == "Computing":
            room = "S227"
        elif self.course == "Art":
            room = "Art Studio 1"
        else:
            room = "Main hall"
        return (room)

    def parentSign(self):
            return "Parent doesn't need to sign" if self.age > 17 else "Parent needs to sign"

newStudent = Student("Name One", "Computing", 18)
newStudent1 = Student("Bob Smart", "Art", 19)
newStudent2 = Student("Big Terry", "Computing", 16)

print(newStudent)
print(newStudent1)
print(newStudent2)
您可以创建一个打印机函数,在列表中创建学生,并循环打印他们的属性


您可以创建一个打印机函数,在列表中创建学生,并循环打印他们的属性

解决此问题的方法有很多。 我更喜欢的方法是使用类的
\uuuu repr\uuu
方法

下面是一个实现:

class Student:
    def __init__(self, name, course, age):
        self.name = name
        self.course = course
        self.age = age
    def roomNumber(self):
        if self.course == "Computing":
            room = "S227"
        elif self.course == "Art":
            room = "Art Studio 1"
        else:
            room = "Main hall"
        return (room)
    def parientSign(self):
        if self.age > 17:
            print("Parent doesn't need to sign")
        else:
            print("Parent needs to sign")
        return
    def __repr__(self):
        return "Student({},{},{})".format(self.name, self.course,self.age)

newStudent = Student("Name One", "Computing", 18)
newStudent1 = Student("Bob Smart", "Art", 19)
newStudent2 = Student("Big Terry", "Computing", 16)

print(newStudent,newStudent1,newStudent2,sep = "\n")
我假设
方法的使用是自解释的。基本上可以打印类名及其所有属性

另一种(不推荐)方法是使用students对象列表

class Student:
    def __init__(self, name, course, age):
        self.name = name
        self.course = course
        self.age = age
    def roomNumber(self):
        if self.course == "Computing":
            room = "S227"
        elif self.course == "Art":
            room = "Art Studio 1"
        else:
            room = "Main hall"
        return (room)
    def parientSign(self):
        if self.age > 17:
            print("Parent doesn't need to sign")
        else:
            print("Parent needs to sign")
        return


newStudent = Student("Name One", "Computing", 18)
newStudent1 = Student("Bob Smart", "Art", 19)
newStudent2 = Student("Big Terry", "Computing", 16)

l = [newStudent,newStudent1,newStudent2]

for students in l:
    print("Student Name: ", students.name)
    print("Student Course: ",students.course)
    print("Your room number is: ", students.roomNumber())
    print("Your Age is: ",students.age)
    students.parientSign()

有很多方法可以解决这个问题。 我更喜欢的方法是使用类的
\uuuu repr\uuu
方法

下面是一个实现:

class Student:
    def __init__(self, name, course, age):
        self.name = name
        self.course = course
        self.age = age
    def roomNumber(self):
        if self.course == "Computing":
            room = "S227"
        elif self.course == "Art":
            room = "Art Studio 1"
        else:
            room = "Main hall"
        return (room)
    def parientSign(self):
        if self.age > 17:
            print("Parent doesn't need to sign")
        else:
            print("Parent needs to sign")
        return
    def __repr__(self):
        return "Student({},{},{})".format(self.name, self.course,self.age)

newStudent = Student("Name One", "Computing", 18)
newStudent1 = Student("Bob Smart", "Art", 19)
newStudent2 = Student("Big Terry", "Computing", 16)

print(newStudent,newStudent1,newStudent2,sep = "\n")
我假设
方法的使用是自解释的。基本上可以打印类名及其所有属性

另一种(不推荐)方法是使用students对象列表

class Student:
    def __init__(self, name, course, age):
        self.name = name
        self.course = course
        self.age = age
    def roomNumber(self):
        if self.course == "Computing":
            room = "S227"
        elif self.course == "Art":
            room = "Art Studio 1"
        else:
            room = "Main hall"
        return (room)
    def parientSign(self):
        if self.age > 17:
            print("Parent doesn't need to sign")
        else:
            print("Parent needs to sign")
        return


newStudent = Student("Name One", "Computing", 18)
newStudent1 = Student("Bob Smart", "Art", 19)
newStudent2 = Student("Big Terry", "Computing", 16)

l = [newStudent,newStudent1,newStudent2]

for students in l:
    print("Student Name: ", students.name)
    print("Student Course: ",students.course)
    print("Your room number is: ", students.roomNumber())
    print("Your Age is: ",students.age)
    students.parientSign()

可能最适合并需要适当的缩进…python教程第4章:
for
循环。重写
\uuu str\uuuu
?使用for循环。可能最适合并需要适当的缩进…python教程第4章:
for
循环。重写
\uu str\uuuu
?使用for循环。为什么要列出不建议这样做?因为您必须显式创建一个占用内存(尽管内存不足)的列表。另外,通过使用
\uuuu repr\uuuuu
您并没有创建任何新的内容,因为它是每个python类的一部分,更面向对象,而且也非常实用。什么是面向对象的,什么不是?列表可以在您需要时使用!让我们想象一下,您必须从Db获取学生,您将如何存储这些学生?当涉及到编码实践时,不要僵化。如果扩展适合您的问题,您可能需要扩展此想法。从数据库中获取学员与仅仅打印查看对象存储了哪些值不同。如果您使用的是数据库,那么您肯定希望将值存储在列表中,以便以后使用,但对于仅打印以查看对象属性中存储了哪些值的对象,创建列表(或新函数)更是不必要的工作。只需使用dunder方法,这是专门为这些目的而设计的。但是如果你仍然感到困惑,为什么不推荐这个列表呢?因为你必须明确地创建一个占用内存(虽然内存很低)的列表。另外,通过使用
\uuuu repr\uuuuu
您并没有创建任何新的内容,因为它是每个python类的一部分,更面向对象,而且也非常实用。什么是面向对象的,什么不是?列表可以在您需要时使用!让我们想象一下,您必须从Db获取学生,您将如何存储这些学生?当涉及到编码实践时,不要僵化。如果扩展适合您的问题,您可能需要扩展此想法。从数据库中获取学员与仅仅打印查看对象存储了哪些值不同。如果您使用的是数据库,那么您肯定希望将值存储在列表中,以便以后使用,但对于仅打印以查看对象属性中存储了哪些值的对象,创建列表(或新函数)更是不必要的工作。只需使用dunder方法,这是专门为这些目的而设计的。但是如果你仍然感到困惑,看看这个