• Pyuthon一切皆对象


    '''
    #1.一切皆对象
    # 数据类型 : 数字类型 布尔类型 字符串类型 列表 字典 集合 元组
    s = "hello yuan"
    s2 = str("hello yuan")
    print(s, type(s2)) # hello yuan <class 'str'>

    print(s.upper(), type(s2)) # HELLO YUAN <class 'str'>


    l = [1, 2, 3]
    l2 = list([2, 3, 4])
    print(l2, type(l2)) # [2, 3, 4] <class 'list'>
    l2.append(5)
    print(l2, type(l2)) # [2, 3, 4, 5] <class 'list'>



    #2.扩展案例
    class Weapon(object):
    def __init__(self, name, color, attack):
    self.name = name # 武器的名字
    self.color = color # 武器的颜色
    self.attack = attack # 武器的攻击

    class Hero(object):
    def __init__(self, name, sex, hp, weapon, level=2, exp=2000, money=10000):
    self.name = name # 英雄的名字
    self.sex = sex # 英雄的性别
    self.hp = hp # 英雄生命值
    self.level = level # 英雄的等级
    self.exp = exp # 英雄的经验值
    self.money = money # 英雄的金币
    self.weapon = weapon # 英雄的武器
    w1 = Weapon("弓箭", "黄色", 180)
    print(w1) # <__main__.Weapon object at 0x00000250DA3E7FD0>
    huchangxi = Hero("huchangxi","男" ,100, w1)
    print(huchangxi) # <__main__.Hero object at 0x00000250DA3E7940>
    print(huchangxi.weapon) # <__main__.Weapon object at 0x00000250DA3E7FD0>


    #3.误区
    #声明类
    class Student(object):
    # 类属性
    clsas_name = "34期"
    num = 30
    listen = 100
    # 方法
    def listen(self):
    print("听课")

    def homework(self):
    print("写作业")


    print(Student.clsas_name) # 34期
    Student.clsas_name = "xxx"

    s1 = Student()
    print(s1.clsas_name) # xxx
    s1.listen = 100
    s1.listen() # 报错:TypeError: 'int' object is not callable

    #详解
    #===============================

    def TEST():
    TEST = "呼长喜"
    print(TEST)

    print (TEST) # 呼长喜
    #===============================
    TEST2 = 150
    def TEST2():
    TEST2 = "呼长喜"
    print(TEST2)

    print (TEST2) # 呼长喜

    def TEST3():
    TEST3 = "呼长喜"
    print(TEST3)
    TEST3 = 150
    print (TEST3) # 150
    #===============================
    '''
  • 相关阅读:
    纯CSS打造好看的按钮样式
    jQuery手机端触摸卡片切换效果
    CSS手动改变DIV高宽
    Windows 10 的音频和 MIDI API将统一
    美食网站响应式精美模板
    三道Javascript的练习题
    html5手机端遮罩弹出菜单代码
    CSS的::selection使用方法
    Html5绘制饼图统计图
    JQuery实现一个简单的鼠标跟随提示效果
  • 原文地址:https://www.cnblogs.com/A121/p/16136534.html
Copyright © 2020-2023  润新知