定义类的Python示例

The task to define a class in Python.

在Python中定义类的任务。

Here, we are defining a class named Number with an attribute num, initializing it with a value 123, then creating two objects N1 and N2 and finally, printing the object's memory locations and attributes value using the object names.

在这里,我们定义一个名为Number的类,其属性为num ,将其初始化为值123,然后创建两个对象N1和N2 ,最后,使用对象名打印该对象的内存位置和属性值。

定义类的Python代码 (Python code to define a class)

# Python code to define a class

# class definition
class Number():
    #Attribute
    num = 123

# main code
if __name__ == "__main__": 
    # creating first object 
    N1 = Number()
    #Printing object's memory (in hexadecimal)
    print(N1)      
    #Accessing and printing Class's Attribute
    print(N1.num)  

    # creating first object 
    N2 = Number()
    #Printing object's memory (in hexadecimal)
    print(N2)      
    #Accessing and printing Class's Attribute
    print(N2.num)

Output

输出量

<__main__.Number object at 0x7f96c06a0160>
123
<__main__.Number object at 0x7f96c06a06d8>
123


翻译自: https://www.includehelp.com/python/example-to-define-a-class.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值