是否有更好的方法通过面向对象编程来打印一名以上的学生?

时间:2018-10-17 14:20:30

标签: 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":
            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")

3 个答案:

答案 0 :(得分:1)

信用和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)

答案 1 :(得分:1)

window.root

您可以创建打印机功能,并在列表中创建学生,然后遍历他们以打印其属性

答案 2 :(得分:0)

有很多方法可以解决此问题。 我更喜欢的方法是使用课程的__repr__方法

这是一个实现:

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

我认为使用__repr__方法是不言而喻的。您基本上可以打印类名称及其所有属性

执行此操作的另一种方法(不推荐)是通过使用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()