■ 클래스에서 메소드를 재정의하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Person: "부모 클래스" def __init__(self, name, phoneNumber): self.Name = name self.PhoneNumber = phoneNumber def PrintData(self): print("Person(Name : {0}, Phone Number : {1})".format(self.Name, self.PhoneNumber)) class Student(Person): "자식 클래스" def __init__(self, name, phoneNumber, subject, studentID): Person.__init__(self, name, phoneNumber) self.Subject = subject self.StudentID = studentID def PrintData(self): print("Student(Subject : {0}, Student ID : {1})".format(self.Subject, self.StudentID)) student = Student("Marry", "010-654-1234", "Computer Science", "990999") student.PrintData() """ Student(Subject : Computer Science, Student ID : 990999) """ |