2021-08-23

一、python中类的特殊成员


1.__init__ 初始化方法

2.__new__  构造方法
通过 类() 会执行 __new__ 和 __init__ 方法

3.__call__
通过 对象() 会执行 __call__ 方法

4.__str__
里面必须返回一个字符串
data = str(obj)会执行对象里的__str__方法 data为方法里返回的字符串
如果有 __str__ 方法,直接输出也会执行 __str__ 方法

5.__dict__
print(obj.__dict__) 将实例变量转换成字典输出

6.__getitem__   __setitem__   __delitem__
使用像字典一样

7.__enter__   __exit__
上下文管理语法: with obj as f:    也可以不写对象,直接写  类()
with + 对象 会自动执行 __enter__方法, __enter__里面可以有返回值,返回值是什么,f就是什么
当with缩进的代码执行完毕后,会自动执行 __exit__ 方法

8.__add__  两个对象相加(还有加减乘除平方等)

二、使用案例

# __init__

class Foo(object):
    def __init__(self, name):
        self.name = name


obj1 = Foo("小虾米")

# __new__

class Foo(object):
    def __init__(self, name):
        print("第二步:初始化对象,在空对象中创建数据")
        self.name = name

    def __new__(cls, *args, **kwargs):
        print("第一步:先创建空对象并返回")
        return object.__new__(cls)


obj2 = Foo("小虾米")

# __call__

class Foo(object):
    def __call__(self, *args, **kwargs):
        print("执行call方法")


obj = Foo()
obj()

# __str__

class Foo(object):
    def __str__(self):
        return "HHHHHH"


obj = Foo()
data = str(obj)
print(data)

# __str__

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

    def __str__(self):
        return "{}-{}".format(self.name, self.age)


s1 = Student("aa", 19)
s2 = Student("bb", 20)
s3 = Student("cc", 21)
print(s1)

# __dict__

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


obj = Foo("小虾米", 21)
print(obj.__dict__)
# __getitem__   __setitem__   __delitem__
class Foo(object):
    def __setitem__(self, key, value):
        print(key, value)

    def __getitem__(self, item):
        print(item)

    def __delitem__(self, key):
        print(key)


obj = Foo()
obj["x1"] = 123   # 自动触发类中的 __setitem__ 方法
obj["000"]        # 自动触发类中的 __getitem__ 方法,并将000传递给item输出
del obj["000"]    # 自动触发类中的 __delitem__ 方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Java 中的 LocalDateTime 类来处理时间。 首先,我们需要将给定的时间段转换为 LocalDateTime 对象: ```java LocalDateTime start = LocalDateTime.of(2020, 11, 20, 0, 0, 0); LocalDateTime end = LocalDateTime.of(2021, 10, 9, 23, 59, 59); ``` 这里将结束时间设置为 23:59:59 是因为我们想要包含该日期的所有时间。 接下来,我们可以使用一个循环来遍历时间段中的每一天,并获取该天的开始时间和结束时间: ```java LocalDateTime current = start; while (!current.isAfter(end)) { LocalDateTime dayStart = current.withHour(0).withMinute(0).withSecond(0).withNano(0); LocalDateTime dayEnd = current.withHour(23).withMinute(59).withSecond(59).withNano(999999999); System.out.println("Day " + current.toLocalDate() + " starts at " + dayStart + " and ends at " + dayEnd); current = current.plusDays(1); } ``` 这里,我们使用 `withHour`、`withMinute`、`withSecond` 和 `withNano` 方法来设置每天的开始时间和结束时间。然后,我们可以使用 `plusDays` 方法来增加 `current` 对象的日期,以便遍历整个时间段。 完整代码如下: ```java import java.time.LocalDateTime; public class Main { public static void main(String[] args) { LocalDateTime start = LocalDateTime.of(2020, 11, 20, 0, 0, 0); LocalDateTime end = LocalDateTime.of(2021, 10, 9, 23, 59, 59); LocalDateTime current = start; while (!current.isAfter(end)) { LocalDateTime dayStart = current.withHour(0).withMinute(0).withSecond(0).withNano(0); LocalDateTime dayEnd = current.withHour(23).withMinute(59).withSecond(59).withNano(999999999); System.out.println("Day " + current.toLocalDate() + " starts at " + dayStart + " and ends at " + dayEnd); current = current.plusDays(1); } } } ``` 输出结果如下: ``` Day 2020-11-20 starts at 2020-11-20T00:00 and ends at 2020-11-20T23:59:59.999999999 Day 2020-11-21 starts at 2020-11-21T00:00 and ends at 2020-11-21T23:59:59.999999999 Day 2020-11-22 starts at 2020-11-22T00:00 and ends at 2020-11-22T23:59:59.999999999 ... Day 2021-10-07 starts at 2021-10-07T00:00 and ends at 2021-10-07T23:59:59.999999999 Day 2021-10-08 starts at 2021-10-08T00:00 and ends at 2021-10-08T23:59:59.999999999 Day 2021-10-09 starts at 2021-10-09T00:00 and ends at 2021-10-09T23:59:59.999999999 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值