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  .
     
     
     
     
     
     
  • 相关阅读:
    用户管理
    网线制作与分类
    5.虚函数,覆盖,多态,异常处理
    4.类的继承
    3.运算符重载
    7.STL
    6.泛型编程与模板
    C++中>>,<<的重载问题
    2.名字空间和构造函数
    1.C和C++的区别
  • 原文地址:https://www.cnblogs.com/kelake/p/7795539.html
Copyright © 2011-2022 走看看