Pythonの特殊メソッドについて new

迷茫
リリース: 2017-03-25 14:33:22
オリジナル
1616 人が閲覧しました

新しい(cls [, ...]) span>
new(cls[, ...])

Called to create a new instance of class cls. new() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of new() should be the new object instance (usually an instance of cls).

Typical implementations create a new instance of the class by invoking the superclass’s new() method using <span class="pre">super(currentclass, <span class="pre">cls).new(cls[, <span class="pre">...])</span></span></span> with appropriate arguments and then modifying the newly-created instance as necessary before returning it.

If new() returns an instance of cls, then the new instance’s init() method will be invoked like <span class="pre">init(self[, <span class="pre">...])</span></span>, where self is the new instance and the remaining arguments are the same as were passed to new().

If new() does not return an instance of cls, then the new instance’s init() method will not be invoked.

new() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.

调用产生一个新的类的实例,clsnew()是一个<a href="http://www.php.cn/wiki/188.html" target="_blank">静态</a>方法(不需要声明),类本身(cls)作为第一个参数,其他的的参数是传递给<a href="http://www.php.cn/wiki/60.html" target="_blank">对象</a><a href="http://www.php.cn/wiki/176.html" target="_blank">构造函数</a>的<a href="http://www.php.cn/wiki/81.html" target="_blank">表达式</a>(对类的调用),new()的返回值应该是一个新的对象实例(一般是cls的实例)。典型的实现方法就是在返回新生成的实例之前,调用父类的new()方法(super(currentclass, <span class="pre">cls).new(cls[, <span class="pre">...])</span></span>)来改变这个实例对象,比如说可以把实例里面字符的空格去掉等等(这句是我自己加的)。

クラス cls の新しいインスタンスを作成するために呼び出されます。 new() は、インスタンスがリクエストされたクラスをそのクラスとして受け取る静的メソッド (特殊な場合があるため、そのように宣言する必要はありません) です。最初の引数。残りの引数は、オブジェクト コンストラクター式 (クラスへの呼び出し) に渡される引数です。 new() の戻り値は、新しいオブジェクト インスタンス (通常は cls のインスタンス) である必要があります。

典型的な実装<span class="pre">new()</span> メソッドを呼び出して、クラスの新しいインスタンスを作成します。 >super(currentclass, cls).new(cls[, ...])適切な引数を指定し、必要に応じて新しく作成されたインスタンスを変更してから返します。

new()cls にすると、新しいインスタンスの init() メソッドが <span class="pre">init(self[, <span class="pre">...])</span></span>self は新しいインスタンス、残りの引数は次のとおりです。 new() に渡されたものと同じです。

If new() code> が <em>cls</em> のインスタンスを返さない場合、新しいインスタンスの <code class="xref py py-meth docutils literal">init() メソッドは呼び出されません。

new() は主に、不変型 (int、str、tuple など) のサブクラスがインスタンスの作成をカスタマイズできるようにすることを目的としています。また、クラスの作成をカスタマイズするためにカスタム メタクラスでもオーバーライドされることが一般的です。

调用产生一个新的类的实例、cls。 new() 是一个<a href="http://www.php.cn/wiki/188%20.html" target="_blank">静态🎜メソッド(リスク不要)、类本身(cls)は第一のパラメータとして、その他のパラメータは传递给</a><a href="http://www.php.cn%20/wiki/60.html" target="_blank">对象🎜</a><a href="http://www.php.cn/wiki/176.html" target="_blank">构造関数🎜の</a><a href="http://www.php.cn/wiki/81.html" target="_blank">表达式🎜(对类的调用),new()の返值应该は一つの新しいオブジェクト实例(一般的にはcls)典型的な実装方法は、新しく生成されたサンプルを返す前に、父クラスの new() メソッド(super(currentclass, <span class="pre">cls).new(cls[,<span class="pre">...])</span></span>)この例のオブジェクトを変更するには、例の本文の文字の空格削除などを行うことができます(この句は自己追加的)。</a> 🎜🎜結果 new() は cls のサンプル オブジェクトを返し、その後、この新しいサンプルを使用して init() メソッド (init[,...]) を実行し、新しく作成されたサンプルの残りのパラメータと継承を指示します。 🎜🎜 new() が cls の例を正常に返せなかった場合、この例を使用する init() メソッドは使用されません。 🎜🎜 new() は主に、不可能な型 (イメージは int,str, 🎜🎜例: 🎜文字列🎜は、オブジェクトの例に先立って処理されます。新しい、次の例を使用して空格処理を行うことができます。

以上がPythonの特殊メソッドについて newの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!