美文网首页
面向对象

面向对象

作者: esskeetit | 来源:发表于2018-05-25 20:12 被阅读0次
    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)
    

    相关文章

      网友评论

          本文标题:面向对象

          本文链接:https://www.haomeiwen.com/subject/rewujftx.html