• python类基础


    #coding:gbk
    
    
    class Person():
        def __init__(self,age,gender,height,weight):
            self.age = age
            self.gender = gender
            self.height = height
            self.weight = weight
            
        def run(self):
            print("体重为%s的人在跑步" % self.weight)
            
    deo = Person(20,'man','170cmm','70kg')
    deo.run()

    类实例 =  类对象
    函数 = 方法
    一个类对象(类实例)可以调用类实例属性和类实例方法
     
    类继承
    #coding:gbk
    class Person:
        
        can_eat = True
        
        def __init__(self, age, gander):
            self.age = age
            self.gander = gander
            
        def speck(self, words):
            print('I said :%s' % words)
        
        def run(self):
            print('run')
    
    
    class Father(Person):
        
        def __init__(self, age, gander):
            Person.__init__(self, age, gander)
        
        def run(self):
            print('我跑不动了')
    
    
    class Son(Father):
        
        def __init__(self, age, gander, height):
            Father.__init__(self, age, gander)
            self.height = height
    
        def eat(self):
            print('我想吃糖果')
    
    
    son = Son(10, 'boy', '110cm')
    son.run()
    son.speck('我想吃')

    1: 子类默认继承父类的类实例方法
    2: 类实例属性继承需要在__init__(self)中加上
         FatherClass. __init__(self)
     
    类的方法
    #coding: gbk
    
    class T:
        def __init__(self,name):
            self.name = name
            
        def ord_func(self):
            
            print('实例方法')
            
        @classmethod
        def class_func(cls):
            print('类方法')
            
        @staticmethod
        def static_func():
            print('静态方法')
            
    f = T('jack')
    f.ord_func()
    #Foo.ord_func(f)
    
    T.class_func()
    #f.class_func()
    
    T.static_func()

    继承

    #coding: gbk
    class People:
        def __init__(self,name,age,weight):
            self.name = name
            self.age = age
            self.__weight = weight
        
        def speak(self):
            print('%s 说: 我 %d 岁...' %(self.name,self.age))
            
    
    class Son(People):
        def __init__(self,name,age,weight,grade):
            super(Son,self).__init__(name=name,age=age,weight=weight)    #等同于People.__init__(self, name, age, weight)


    ##
    使用super()函数!最常见的就是通过super调用父类的实例化方法__init__! 语法:super(子类名, self).方法名(),需要传入的是子类名和self,调用的是父类里的方法,按父类的方法需要传入参数
    
            self.grade = grade
            
        def speak(self):
            print("%s 说: 我 %d 岁了,我在读 %d 年级" % (self.name, self.age, self.grade))
            
    s = Son('jacky',18,100,2)
    s.speak()

     

  • 相关阅读:
    设计模式之桥接模式
    设计模式之适配器模式
    设计模式之建造者模式
    设计模式之原型设计
    Exception in thread "main" java.lang.UnsupportedOperationException
    设计模式7大原则
    设计模式之单例模式
    初识python
    消息传递:发布订阅模式详解
    哨兵机制(Redis Sentinel)
  • 原文地址:https://www.cnblogs.com/hellojackyleon/p/9153128.html
Copyright © 2020-2023  润新知