面向对象

class Person():
    def __init__(self): # Self refers to the individual object itself
        pass

someone = Person()
class Person():
    def __init__(self, name, gender): # The first parameter has to be self
        self.name = name
        self.gender = gender

ed = Person('Edward', 'Male')
print('Name:', ed.name)
print('Gender:', ed.gender)
class Person():
    def __init__(self, name, gender): # The first parameter has to be self
        self.name = name
        self.gender = gender
    
    def say(self):
        print("Hi I'm " + self.name + ", it's nice to meet you!")

ed = Person('Edward', 'Male')
ed.say()
class MDPerson(Person) #继承:
    pass

ed = MDPerson("Edward", 'Male')
ed.say()
----------------------------------------------------
class MDPerson(Person):
    def diagnose(self):
        print('You need some treatment.')

ed = MDPerson("Edward", 'Male')
ed.diagnose()
---------------------------------------------------
class MDPerson(Person):
    def __init__(self, name, gender, dept='Cardiac Surgery'):
        self.name = 'Doctor ' + name
        self.gender = gender
        self.dept = dept
    
    def say(self):
        print("Hi I'm %s from %s department, how can I help you" % (self.name, self.dept))

ed = MDPerson("Edward", 'Male')
ed.say()
class Person():
    def __init__(self, name, gender): # The first parameter has to be self
        self.name = name
        self.gender = gender
    
    def say(self):
        print("Hi I'm " + self.name + ", it's nice to meet you!")
ed = Person('Edward', 'Male')        
Person.say(ed)

你可能感兴趣的:(面向对象)