热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python学习笔记(二十八)编写类的细节(ClassCodingDetails)

1.什么是抽象超类(abstractsuperclass)?答:抽象超类是一个调用方法的类,但不继承或定义它-

1.什么是抽象超类(abstract superclass)?
答:抽象超类是一个调用方法的类,但不继承或定义它 - 它希望该方法由子类来填充。这通常用作在编写更具体的子类之前无法预测行为时概括类的方法。 OOP框架还将此作为一种调度到基于客户定义的可自定义操作的方式。

 

2.当一个简单的赋值语句出现在类语句的顶层时会发生什么?
答:.当一个简单赋值语句(X = Y)出现在类语句的顶层时,它会将一个数据属性附加到该类(Class.X)。与所有类属性一样,这将由所有实例共享;但是,数据属性不是可调用的方法函数。

 

3.为什么一个类需要在超类中手动调用__init__方法?
答:如果类定义了自己的__init__构造函数并且仍然希望运行超类的构造代码,则必须在超类中手动调用__init__方法。 Python本身只会自动运行一个构造函数 - 树中最低的构造函数。超类构造函数通常通过类名调用,手动传递自我实例:Superclass .__ init __(self, ...)。

 

4.如何加强而不是完全替换继承的方法?
答:要加强而不是完全替换继承的方法,请在子类中重新定义它,但是从子类中的方法的新版本手动回调该方法的超类的版本。也就是说,手动将self实例传递给方法的超类版本:Superclass.method(self,...)。

 

5.类的局部范围与函数的范围有何不同?
答:类是局部作用域,可以访问封闭的局部作用域,但它不能作为进一步嵌套代码的封闭局部作用域。与模块一样,类的局部范围在运行类语句后变为属性命名空间。

 

6.亚述的首都。。。是什么?
答:Ashur(或Qalat Sherqat),Calah(或Nimrud),短命的Dur Sharrukin(或Khorsabad),最后是Nineveh。

 

一些别的东东:

1.

class name(superclass, ...): #把对象赋给nameattr = value #共享的类数据def method(self, ...): #方法(method)self.attr = value #单个实例的数据

2.static method 静态函数 没有实例的函数

3.Module和Class

Module和Class都是命名空间

Modules:

(1)实现数据、逻辑包

(2)被Python文件或者其他语言扩展

(3)通过被导入来使用

(4)形成Python程序架构的顶层

Class:

(1)实现新的全功能对象

(2)用class语句创建

(3)通过被调用使用

(4)总是在模块中生存(live)

 

 

注:转载《Learning Python 5th Edition》[奥莱理]

1. What is an abstract superclass?
2. What happens when a simple assignment statement appears at the top level of a class statement?
3. Why might a class need to manually call the __init__ method in a superclass?
4. How can you augment, instead of completely replacing, an inherited method?
5. How does a class's local scope differ from that of a function?
6. What...was the capital of Assyri

1. An abstract superclass is a class that calls a method, but does not inherit or define it—it expects the method to be filled in by a subclass. This is often used as a way
to generalize classes when behavior cannot be predicted until a more specific subclass is coded. OOP frameworks also use this as a way to dispatch to client-defined, customizable operations.
2. When a simple assignment statement (X = Y) appears at the top level of a class statement, it attaches a data attribute to the class (Class.X). Like all class attributes, this will be shared by all instances; data attributes are not callable method functions, though.
3. A class must manually call the __init__ method in a superclass if it defines an __init__ constructor of its own and still wants the superclass's construction code to run. Python itself automatically runs just one constructor—the lowest one in the tree. Superclass constructors are usually called through the class name, passing in the self instance manually: Superclass.__init__(self, ...).
4. To augment instead of completely replacing an inherited method, redefine it in a subclass, but call back to the superclass's version of the method manually from the new version of the method in the subclass. That is, pass the self instance to the superclass's version of the method manually: Superclass.method(self, ...).
5. A class is a local scope and has access to enclosing local scopes, but it does not serve as an enclosing local scope to further nested code. Like modules, the class local scope morphs into an attribute namespace after the class statement is run.
6. Ashur (or Qalat Sherqat), Calah (or Nimrud), the short-lived Dur Sharrukin (or Khorsabad), and finally Nineveh.


推荐阅读
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • PatchODAX8: ... [详细]
author-avatar
太阳神神神_890
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有