zoukankan      html  css  js  c++  java
  • 特殊成员

    #
    # class Foo:
    #     def __init__(self): # 初始化操作
    #         print("我是init,  我是老二")
    #         print("初始化操作. 在创建对象的时候自动调用这个方法")
    #
    #     def __new__(cls, *args, **kwargs): # 创建, 它是真正的构造方法,  可以开辟内存
    #         print("我是new. 我是老大")
    #         return object.__new__(cls)
    #
    #
    #     # 为了 对象()
    #     def __call__(self, *args, **kwargs):
    #         print("我是对象()")
    #
    #     # 对象[]
    #     def __getitem__(self, item):
    #         print("item=",item)
    #         print("你执行了__getitem__")
    #         return "哈哈"
    #
    #     # 对象[key] = value
    #     def __setitem__(self, key, value):
    #         print("key, ", key)
    #         print("value, ", value)
    #
    #     # del lst[1]
    #     def __delitem__(self, key):
    #         print("key=", key)
    #
    #     # with 对象:
    #     def __enter__(self):
    #         print("我是enter")
    #
    #     # with 对象: 代码执行完毕. 最后执行这里
    #     def __exit__(self, exc_type, exc_val, exc_tb):
    #         print("我叫exit")
    #
    #     def __len__(self):
    #         print("我的天哪")
    #         return 3
    #
    #
    # f = Foo()    # 自动执行__init__()
    # f() # 调用-> __call__()
    # print(callable(f)) # 对象()
    
    # print(f["李嘉诚"]) # 自动调用__getitem__()
    # f['jay'] = "林俊杰"
    
    # del f['哈哈']
    
    # with f:
    #     print("我是哈哈哈哈")
    
    # with open() :
    
    
    # lst = ["孙艺珍", "李金珠", "井柏然"]
    #
    # lst[2] # =>自动的调用__getitem__()
    
    
    # def func():
    #     pass
    # func = 3
    # print(callable(func)) # 判断xxx是否是可调用的
    
    #
    # f.__init__()  # 第一次这么写. 以后别这么写
    # lst = [1,2,3,4]
    # it = iter(lst)
    #
    # print(it.__next__())
    #
    # print(next(it)) # __next__()
    
    # 面试之前翻一番
    # 写出15个特殊成员, 并给出具体作用
    
    # class H:
    #     country = "大清"
    #
    # print(H.country)
    
    # 面向对象编程的执行流程 ->
    # 1. 加载类 -> 给类创建一个名称空间 -> 主要存放类变量.
    # 2. 创建对象 -> 先找类. -> 根据类来开辟内存 -> 执行类中的__new__()  -> 执行__init__()  -> 返回对象
    
    
    
    class Student:
        def __init__(self, name, no, gender, cls, age):
            self.name = name
            self.no = no
            self.gender = gender
            self.cls = cls
            self.age = age
    
    
        # 这个对象字符串的表示.
        def __str__(self): # 返回该对象的字符串表示形式
            return f"{self.name}, {self.no}, {self.gender}"
    
        def __repr__(self): # 该对象的官方的字符串表示形式
            return f"{self.name}, {self.no}, {self.gender}"
    
    
    s = Student("董仲浩", "3", "男", "S18", "31")
    print(s)
    

      

  • 相关阅读:
    改造vant日期选择
    css3元素垂直居中
    npm综合
    (转)网页加水印方法
    Mac下IDEA自带MAVEN插件的全局环境配置
    隐藏注册控件窗口
    High performance optimization and acceleration for randomWalk, deepwalk, node2vec (Python)
    How to add conda env into jupyter notebook installed by pip
    The Power of WordNet and How to Use It in Python
    背单词app测评,2018年
  • 原文地址:https://www.cnblogs.com/work14/p/10145476.html
Copyright © 2011-2022 走看看