上一页 1 ··· 21 22 23 24 25 26 27 28 29 ··· 65 下一页
摘要: # 单例 司机只有一个 class Driver: _self=None _name = "Driver Jack" # 司机的姓名 _num = 0 # 司机的业绩 初始值是0 def __new__(cls, *args, **kwargs): if cls._self is None: cls 阅读全文
posted @ 2023-07-17 17:40 胖豆芽 阅读(5) 评论(0) 推荐(0) 编辑
摘要: # 定义一个不重复的列表 import random list= [] count= 0 # 成功放入的次数 while count<5: num= random.randint(1,10) if num not in list: list.append(num) count+=1 # 成功后计数一 阅读全文
posted @ 2023-07-17 17:12 胖豆芽 阅读(6) 评论(0) 推荐(0) 编辑
摘要: # 售票员卖票 class Driver: _self=None _name= 'Driver_Jack'# 售票员的名字 _num= 0 # 售票员的业绩 def __new__(cls, *args, **kwargs): if cls._self is None :# 如果售票员这个类是空 c 阅读全文
posted @ 2023-07-16 21:04 胖豆芽 阅读(2) 评论(0) 推荐(0) 编辑
摘要: # 单例 class Driver: _self=None def __new__(cls, *args, **kwargs): if cls._self is None: cls._self=super().__new__(cls) return cls._self def __init__(se 阅读全文
posted @ 2023-07-15 21:01 胖豆芽 阅读(13) 评论(0) 推荐(0) 编辑
摘要: 理解单例前需要了解 init 和new 这是init的效果 # 认识new 和init class People(object): # def __new__(cls, *args, **kwargs): # print("执行了new") # return object.__new__(cls) 阅读全文
posted @ 2023-07-15 18:59 胖豆芽 阅读(2) 评论(0) 推荐(0) 编辑
摘要: import math class Circle: def __init__(self,r): self._r= r @property def area(self):#def >define 定义一个函数或方法 求面积 mianji=round(self._r**2*math.pi,2)# rou 阅读全文
posted @ 2023-07-15 15:26 胖豆芽 阅读(17) 评论(0) 推荐(0) 编辑
摘要: # 反射class Webb: def reg(self): print("欢迎进入注册页面") def login(self): print("欢迎进入登录页面") def home(self): print("欢迎进入主页") def about(self): print("欢迎进入关于页面") 阅读全文
posted @ 2023-07-15 14:09 胖豆芽 阅读(9) 评论(0) 推荐(0) 编辑
摘要: class MyClass: @classmethod def my_class_method(cls, arg1, arg2): # 使用cls参数访问类的属性和调用类的方法 print("This is a class method") print("Arguments:", arg1, arg 阅读全文
posted @ 2023-07-14 17:31 胖豆芽 阅读(29) 评论(0) 推荐(0) 编辑
摘要: 继承的示例:class Animal(object):# 父类 def run(self): print('Animal is running...') class Dog(Animal): def run(self):# 狗类 继承了 动物类 print('Dog is running...') 阅读全文
posted @ 2023-07-14 16:58 胖豆芽 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 1 没有范围限制 在类外部可以更改类内部的属性值 # 但是,既然Student实例本身就拥有这些数据,要访问这些数据,就没有必要从外面的函数去访问, # 可以直接在Student类的内部定义访问数据的函数,这样,就把“数据”给封装起来了. # 这些封装数据的函数是和Student类本身是关联起来的, 阅读全文
posted @ 2023-07-14 16:23 胖豆芽 阅读(9) 评论(0) 推荐(0) 编辑
上一页 1 ··· 21 22 23 24 25 26 27 28 29 ··· 65 下一页