zoukankan      html  css  js  c++  java
  • Python类一

    面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可能不同。
    class ClassName:
         '''类的说明'''
         类的内容
     
    举例:
    class ren(object):
         '''this is a new class'''
         name = 'meinv'
         sex = 'woman'
    a = ren()
    print (type(a))
    print (a.name)
    print (a.sex)
    a.age = 10
    print (a.age)
    结果:
    <class '__main__.ren'>
    meinv
    woman
    10
     
     
    类的构造器
     
    class ren(object):
    def __init__(self, name, sex):
    self.name = name
    self.sex = sex
    def hello(self):
    print ('Hello {0}'.format(self.name))

    test = ren("Kelake", "M")
    test.hello()

    test2 = ren("King", "M")
    test2.hello()
    运行结果:
    Connected to pydev debugger (build 172.3968.37)
    Hello Kelake
    Hello King
     
    Process finished with exit code 0
     
     
    类的继承
    class A(object):
         pass
    class B(object)
         pass
    class C(A,B)
         pass
     
     
    类C继承类A和类B  .
     
     
     
     
     
     
  • 相关阅读:
    开发管理工具(日常)
    python之uWSGI和WSGI
    php之Opcache
    nginx之健康检查
    重构糟糕的代码(一)
    PHP之50个开源项目
    Redis之各版本特性
    Redis之淘汰策略
    Redis之过期策略
    高并发之nginx限制
  • 原文地址:https://www.cnblogs.com/kelake/p/7795539.html
Copyright © 2011-2022 走看看