■ 클래스에서 자식 클래스의 메소드를 추가하는 방법을 보여준다.
▶ 예제 코드 (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 32 33 |
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 PrintStudentData(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.PrintStudentData() """ Person(Name : Marry, Phone Number : 010-654-1234) Student(Subject : Computer Science, Student ID : 990999) """ |