• 技术文章 >Python技术 >Python基础教程

    python中的class是什么

    小妮浅浅小妮浅浅2021-03-15 11:14:46原创12475

    1、概念

    用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。

    2、类定义的语法

    1

    2

    3

    4

    5

    6

    class ClassName:

        <statement-1>

        .

        .

        .

        <statement-N>

    3、实例

    为了代码的编写方便简洁,引入了类的定义;

    一般,使用 class 语句来创建一个新类,class之后为类的名称(通常首字母大写)并以冒号结尾,例如:、

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    class Ticket():

        def __init__(self,checi,fstation,tstation,fdate,ftime,ttime):

            self.checi=checi

            self.fstation=fstation

            self.tstation=tstation

            self.fdate=fdate

            self.ftime=ftime

            self.ttime=ttime

        def printinfo(self):

            print("车次:",self.checi)

            print("出发站:", self.fstation)

            print("到达站:", self.tstation)

            print("出发时间:", self.fdate)

    以上就是python中class的介绍,在面向对象中我们会频繁接触到这个概念,本篇先把class的基本内容学会吧。更多Python学习推荐:python教学

    (推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)

    专题推荐:python class
    上一篇:python ctypes是什么 下一篇:Python pop是什么

    相关文章推荐

    • python program是什么• python 基类是什么意思• python ctypes是什么

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网