■ 클래스에서 부모 클래스의 생성자를 호출하는 방법을 보여준다.
▶ 예제 코드 (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 person = Person("Derick", "010-123-4567") person.PrintData() student = Student("Marry", "010-654-1234", "Computer Science", "990999") student.PrintData() """ Person(Name : Derick, Phone Number : 010-123-4567) Person(Name : Marry, Phone Number : 010-654-1234) """ |