Python에서 상속이란 무엇입니까:
새 클래스 처음부터 작성할 필요가 없습니다
새 클래스는 기존 클래스를 상속받아 자동으로 기존 클래스의 모든 기능을 갖습니다
새로운 클래스만 기존 클래스에 없는 새로운 기능을 작성하면 됩니다 >
기존 클래스의 모든 기능을 자동으로 소유class Person(object): def init(self, name, gender): self.name = name self.gender = gender class Teacher(Person): def init(self, name, gender, course): super(Teacher, self).init(name, gender) self.course = course t = Teacher('Alice', 'Female', 'English') print t.name print t.course
class Person(object): def init(self, name, gender): self.name = name self.gender = gender class Student(Person): def init(self, name, gender, score): super(Student, self).init(name, gender) self.score = score class Teacher(Person): def init(self, name, gender, course): super(Teacher, self).init(name, gender) self.course = course t = Teacher('Alice', 'Female', 'English') print isinstance(t, Person) print isinstance(t, Student) print isinstance(t, Teacher) print isinstance(t, object)
class Person(object): def init(self, name, gender): self.name = name self.gender = gender def whoAmI(self): return 'I am a Person, my name is %s' % self.name class Student(Person): def init(self, name, gender, score): super(Student, self).init(name, gender) self.score = score def whoAmI(self): return 'I am a Student, my name is %s' % self.name class Teacher(Person): def init(self, name, gender, course): super(Teacher, self).init(name, gender) self.course = course def whoAmI(self): return 'I am a Teacher, my name is %s' % self.name import json class Students(object): def read(self): return r'["Tim", "Bob", "Alice"]' s = Students() print json.load(s)
class A(object): def init(self, a): print 'init A...' self.a = a class B(A): def init(self, a): super(B, self).init(a) print 'init B...' class C(A): def init(self, a): super(C, self).init(a) print 'init C...' class D(B, C): def init(self, a): super(D, self).init(a) print 'init D...' class Person(object): pass class Student(Person): pass class Teacher(Person): pass class SkillMixin(object): pass class BasketballMixin(SkillMixin): def skill(self): return 'basketball' class FootballMixin(SkillMixin): def skill(self): return 'football' class BStudent(BasketballMixin): pass class FTeacher(FootballMixin): pass s = BStudent() print s.skill() t = FTeacher() print t.skill()
class Person(object): def init(self, name, gender): self.name = name self.gender = gender class Student(Person): def init(self, name, gender, score): super(Student, self).init(name, gender) self.score = score def whoAmI(self): return 'I am a Student, my name is %s' % self.name print type(123) # <type 'int'> s = Student('Bob', 'Male', 88) print s # <class 'main.Student'> print dir(123) # ['abs', 'add', 'and', 'class', 'cmp', 'coerce', 'delattr', 'p', 'pmod', 'doc', 'float', 'floorp', 'format', 'getattribute', 'getnewargs', 'hash', 'hex', 'index', 'init', 'int', 'invert', 'long', 'lshift', 'mod', 'mul', 'neg', 'new', 'nonzero', 'oct', 'or', 'pos', 'pow', 'radd', 'rand', 'rp', 'rpmod', 'reduce', 'reduce_ex', 'repr', 'rfloorp', 'rlshift', 'rmod', 'rmul', 'ror', 'rpow', 'rrshift', 'rshift', 'rsub', 'rtruep', 'rxor', 'setattr', 'sizeof', 'str', 'sub', 'subclasshook', 'truep', 'trunc', 'xor', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real'] print dir(s) # ['class', 'delattr', 'dict', 'doc', 'format', 'getattribute', 'hash', 'init', 'module', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook', 'weakref', 'gender', 'name', 'score', 'whoAmI'] print getattr(s, 'name') # Bob setattr(s, 'name', 'Adam') print s.name # Adam class Person(object): def init(self, name, gender, **kw): self.name = name self.gender = gender for k, v in kw.iteritems(): setattr(self, k, v) p = Person('Bob', 'Male', age=18, course='Python') print p.age # 18 print p.course #Python
위 내용은 Python 클래스의 상속에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!