2026/1/8 20:52:15
网站建设
项目流程
免费行情软件app网站直播下载,网站免费建设推荐,淮北矿业工程建设公司网站,网站备案手机号在子类中重写父类的类变量#xff0c;核心是给子类定义同名的类变量#xff08;或通过子类名动态赋值#xff09;#xff0c;让子类拥有独立的类变量#xff0c;不再共享父类的同名变量。下面分「静态重写」「动态重写」两种场景#xff0c;结合示例讲透具体操作、效果和…在子类中重写父类的类变量核心是给子类定义同名的类变量或通过子类名动态赋值让子类拥有独立的类变量不再共享父类的同名变量。下面分「静态重写」「动态重写」两种场景结合示例讲透具体操作、效果和避坑点。一、核心原理父类的类变量会被子类继承若子类未定义同名变量访问子类 / 子类实例的该变量时会读取父类的若子类定义了同名类变量重写子类会在自己的命名空间中创建独立变量与父类解耦后续修改子类的该变量不会影响父类。二、场景 1静态重写定义子类时直接声明这是最常用的方式 —— 在子类定义时直接声明与父类同名的类变量完成重写。示例基础静态重写python运行class Parent: # 父类类变量 brand 父类默认品牌 max_count 10 class Child(Parent): # 重写子类声明同名类变量覆盖父类的 brand 子类定制品牌 # 可只重写部分变量如只重写brand保留max_count # 验证重写效果 print(Parent.brand) # 输出父类默认品牌父类不变 print(Child.brand) # 输出子类定制品牌子类用自己的 print(Child.max_count) # 输出10未重写仍继承父类 # 修改子类重写后的变量不影响父类 Child.brand 子类修改后的品牌 print(Child.brand) # 输出子类修改后的品牌 print(Parent.brand) # 输出父类默认品牌不受影响示例重写可变类型类变量若父类类变量是列表、字典等可变类型子类重写时需赋值新的可变对象而非修改父类对象的内部元素python运行class Parent: # 父类可变类变量 items [1, 2, 3] class Child(Parent): # 重写赋值新的列表子类独立变量 items [10, 20, 30] # 验证 print(Parent.items) # 输出[1,2,3]父类不变 print(Child.items) # 输出[10,20,30]子类自己的 # 修改子类变量的内部元素仅影响子类 Child.items.append(40) print(Child.items) # 输出[10,20,30,40] print(Parent.items) # 输出[1,2,3]父类不受影响三、场景 2动态重写运行时赋值若需要在程序运行时而非定义子类时重写父类类变量可通过「子类名。变量名 新值」动态赋值效果与静态重写一致。示例动态重写类变量python运行class Parent: version 1.0 config {mode: normal} class Child(Parent): # 初始未重写继承父类的version和config pass # 运行时动态重写单个变量 Child.version 2.0 print(Child.version) # 输出2.0子类独立变量 print(Parent.version) # 输出1.0父类不变 # 动态重写可变类型变量赋值新对象 Child.config {mode: debug, timeout: 10} print(Child.config) # 输出{mode: debug, timeout: 10} print(Parent.config) # 输出{mode: normal}父类不变四、场景 3在子类方法中重写 / 修改重写后的类变量子类重写类变量后在子类的实例方法 / 类方法中修改时需用「子类名。变量名」而非selfself赋值会创建实例变量。示例子类方法中操作重写后的类变量python运行class Parent: count 0 class Child(Parent): # 静态重写类变量 count 100 def add_count(self): # 正确修改子类自己的类变量 Child.count 1 def wrong_add(self): # 错误self.count 1 → 给实例新增count变量而非修改类变量 self.count 1 # 验证正确操作 c Child() c.add_count() print(Child.count) # 输出101子类类变量已改 print(c.count) # 输出101读子类类变量 # 验证错误操作 c.wrong_add() print(c.count) # 输出102实例变量 print(Child.count) # 输出101子类类变量未变五、重写的避坑点不要通过实例重写类变量子类实例.变量名 新值只会给实例新增同名实例变量不会重写子类的类变量python运行c Child() c.brand 实例临时值 print(c.brand) # 输出实例临时值实例变量 print(Child.brand) # 输出子类定制品牌子类类变量未变可变类型变量避免 “修改父类元素”若子类未重写直接修改父类可变变量的内部元素如Child.items.append(4)会影响父类和所有未重写的子类这不是 “重写”而是 “修改父类变量”不要滥用双下划线命名父类类变量若用双下划线开头如__var会触发名称改写变成_Parent__var子类需用_Parent__var才能访问重写时易出错非必要不要用。六、重写 vs 继承核心对比操作效果适用场景子类不重写父类变量子类共享父类的变量父类修改会同步影响子类子类需继承父类的全局配置子类重写父类变量子类拥有独立变量与父类解耦子类需要定制化配置总结子类重写父类类变量的 3 条核心规则静态重写定义子类时直接声明同名类变量动态重写运行时通过子类名.变量名 新值赋值可变类型重写时必须赋值新的可变对象如[]/{}而非修改父类对象的内部元素。只要遵循「子类名绑定新值」的原则就能实现类变量的重写让子类拥有独立的类级属性。https://avg.163.com/topic/detail/8089550https://avg.163.com/topic/detail/8089584https://avg.163.com/topic/detail/8089628https://avg.163.com/topic/detail/8089740https://avg.163.com/topic/detail/8089361https://avg.163.com/topic/detail/8089549https://avg.163.com/topic/detail/8089586https://avg.163.com/topic/detail/8089629https://avg.163.com/topic/detail/8089743https://avg.163.com/topic/detail/8089359https://avg.163.com/topic/detail/8089548https://avg.163.com/topic/detail/8089623https://avg.163.com/topic/detail/8089742https://avg.163.com/topic/detail/8089547https://avg.163.com/topic/detail/8089741https://avg.163.com/topic/detail/8089355https://avg.163.com/topic/detail/8089540https://avg.163.com/topic/detail/8089621https://avg.163.com/topic/detail/8089738https://avg.163.com/topic/detail/8089368https://avg.163.com/topic/detail/8089542https://avg.163.com/topic/detail/8089590https://avg.163.com/topic/detail/8089625https://avg.163.com/topic/detail/8089739https://avg.163.com/topic/detail/8089546https://avg.163.com/topic/detail/8089588https://avg.163.com/topic/detail/8089619https://avg.163.com/topic/detail/8089545https://avg.163.com/topic/detail/8089587https://avg.163.com/topic/detail/8089626https://avg.163.com/topic/detail/8089735https://avg.163.com/topic/detail/8089544https://avg.163.com/topic/detail/8089585https://avg.163.com/topic/detail/8089624https://avg.163.com/topic/detail/8089736https://avg.163.com/topic/detail/8089364https://avg.163.com/topic/detail/8089543https://avg.163.com/topic/detail/8089734https://avg.163.com/topic/detail/8089541https://avg.163.com/topic/detail/8089620https://avg.163.com/topic/detail/8089539https://avg.163.com/topic/detail/8089580https://avg.163.com/topic/detail/8089618https://avg.163.com/topic/detail/8089731https://avg.163.com/topic/detail/8089358https://avg.163.com/topic/detail/8089538https://avg.163.com/topic/detail/8089574https://avg.163.com/topic/detail/8089617https://avg.163.com/topic/detail/8089732https://avg.163.com/topic/detail/8089351https://avg.163.com/topic/detail/8089537https://avg.163.com/topic/detail/8089573https://avg.163.com/topic/detail/8089616https://avg.163.com/topic/detail/8089730https://avg.163.com/topic/detail/8089349https://avg.163.com/topic/detail/8089581https://avg.163.com/topic/detail/8089728https://avg.163.com/topic/detail/8089348https://avg.163.com/topic/detail/8089535https://avg.163.com/topic/detail/8089579https://avg.163.com/topic/detail/8089613https://avg.163.com/topic/detail/8089729https://avg.163.com/topic/detail/8089346https://avg.163.com/topic/detail/8089720https://avg.163.com/topic/detail/8089345https://avg.163.com/topic/detail/8089533https://avg.163.com/topic/detail/8089342https://avg.163.com/topic/detail/8089725https://avg.163.com/topic/detail/8089532https://avg.163.com/topic/detail/8089576https://avg.163.com/topic/detail/8089717https://avg.163.com/topic/detail/8089347https://avg.163.com/topic/detail/8089575https://avg.163.com/topic/detail/8089722https://avg.163.com/topic/detail/8089344https://avg.163.com/topic/detail/8089530https://avg.163.com/topic/detail/8089572https://avg.163.com/topic/detail/8089607https://avg.163.com/topic/detail/8089727https://avg.163.com/topic/detail/8089343https://avg.163.com/topic/detail/8089528https://avg.163.com/topic/detail/8089571https://avg.163.com/topic/detail/8089602https://avg.163.com/topic/detail/8089341https://avg.163.com/topic/detail/8089605https://avg.163.com/topic/detail/8089713https://avg.163.com/topic/detail/8089336https://avg.163.com/topic/detail/8089340https://avg.163.com/topic/detail/8089567https://avg.163.com/topic/detail/8089569https://avg.163.com/topic/detail/8089614https://avg.163.com/topic/detail/8089565https://avg.163.com/topic/detail/8089719https://avg.163.com/topic/detail/8089612https://avg.163.com/topic/detail/8089337https://avg.163.com/topic/detail/8089527https://avg.163.com/topic/detail/8089529https://avg.163.com/topic/detail/8089608https://avg.163.com/topic/detail/8089563https://avg.163.com/topic/detail/8089715https://avg.163.com/topic/detail/8089610https://avg.163.com/topic/detail/8089330https://avg.163.com/topic/detail/8089597https://avg.163.com/topic/detail/8089708https://avg.163.com/topic/detail/8089333https://avg.163.com/topic/detail/8089566https://avg.163.com/topic/detail/8089604https://avg.163.com/topic/detail/8089338https://avg.163.com/topic/detail/8089335https://avg.163.com/topic/detail/8089524https://avg.163.com/topic/detail/8089521https://avg.163.com/topic/detail/8089564https://avg.163.com/topic/detail/8089606https://avg.163.com/topic/detail/8089601https://avg.163.com/topic/detail/8089714https://avg.163.com/topic/detail/8089709https://avg.163.com/topic/detail/8089519https://avg.163.com/topic/detail/8089520https://avg.163.com/topic/detail/8089559https://avg.163.com/topic/detail/8089560https://avg.163.com/topic/detail/8089598https://avg.163.com/topic/detail/8089724https://avg.163.com/topic/detail/8089721https://avg.163.com/topic/detail/8089328https://avg.163.com/topic/detail/8089518https://avg.163.com/topic/detail/8089558https://avg.163.com/topic/detail/8089596https://avg.163.com/topic/detail/8089710