Python基础学习-12

Python基础学习-12

# Classes and objects

# use class key to create the class
class My_Class:
    x = 6


print(My_Class)
#  create the object p1
p1 = My_Class()
print(p1.x)


# init()函数、所有类都有该内置函数
# 使用init()函数为对象属性赋值、
# 或在创建对象时需要执行的其他操作


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age


people = Person("曾小贤", 31)
print(people.name, people.age)


# init()函数每次使用该类创建新对象时
# 都会自动调用该函数

class Person1:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def function(self):
        print(self.name + "的年龄是" + self.age)

people1 = Person1("关谷神奇", "23")
people1.function()

# self参数对类当前实例进行引用,
# 用于访问类中的变量。
# self变量可以命名为其他名字,
# 但是它必须是类中任何函数的第一个参数

class Person2:
    def __init__(main, name, age):
        main.name = name
        main.age = age
    def function(a):
        print(a.name + "还有"+ a.age + "年")

people2 = Person2("胡一菲", "23")
people2.age = "24"
print(people2.function())

# change
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def fun(self):
    print("我的名字是 " + self.name)

p1 = Person("吕小布", 20)
p1.age = 21
print(p1.age)
# Delete the properties
del p1.age
# print(p1.age) 会打印报错
del p1
# print(p1) 会打印报错
class Person3:
    pass

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七七高7777

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值