JavaSerializable, Cloneable, Observable
などのすべてのインターフェイスには、「-able」という接尾辞が付きます。ただし、java.lang.Throwable
インターフェイスではなくクラスです。
の用法はjava.lang.Throwable
理解できますが、なぜそのような名前が付けられているのか理解できません。この異常には特定の理由がありますか?
JavaSerializable, Cloneable, Observable
などのすべてのインターフェイスには、「-able」という接尾辞が付きます。ただし、java.lang.Throwable
インターフェイスではなくクラスです。
の用法はjava.lang.Throwable
理解できますが、なぜそのような名前が付けられているのか理解できません。この異常には特定の理由がありますか?
Sunの元VPでJavaのメインアーキテクトであるJamesGoslingとのインターネットのゴミ箱に負けたインタビューは、Throwableをインターフェイスではなくクラスにすることを決定した理由を説明しています。主な理由は、スローアブルが状態を追跡する必要があるためです。
JDC: Why is Throwable not an interface? The name kind of suggests it should have been.
Being able to catch for types, that is, something like try{}catch (<some interface or
class>), instead of only classes. That would make [the] Java [programming language]
much more flexible.
JG: The reason that the Throwable and the rest of those guys are not interfaces is
because we decided, or I decided fairly early on. I decided that I wanted to have some
state associated with every exception that gets thrown. And you can't do that with
interfaces; you can only do that with classes. The state that's there is basically
standard. There's a message, there's a snapshot, stuff like that — that's always there.
and also, if you make Throwable an interface the temptation is to assign, to make any
old object be a Throwable thing. It feels stylistically that throwing general objects
is probably a bad idea, that the things you want to throw really ought to be things
that are intended to be exceptions that really capture the nature of the exception and
what went on. They're not just general data structures.
Throwable クラスは、Java 言語のすべてのエラーと例外のスーパークラスです。このクラス (またはそのサブクラスの 1 つ) のインスタンスであるオブジェクトのみが、Java 仮想マシンによってスローされるか、Java の throw ステートメントによってスローされる可能性があります。同様に、このクラスまたはそのサブクラスの 1 つだけが catch 句の引数の型になることができます。
インターフェイス/抽象クラスと同じように、スローできるすべてのものを網羅しています。-able
それがサフィックスを持つ背後にあるロジックであるべきだと思います。これは議論のポイントではありませんが...一般に、で終わるものはすべてable
インターフェースであると想定すべきではありません。
更新
実生活からの別の例は、私のプロジェクトの 1 つです... サブクラスを (MemcacheD に) キャッシュできる (抽象的な) スーパークラスを作成する必要がありました。キャッシュの追加、削除、更新に必要なすべてのロジックを抽象化しました。それにふさわしい名前は何でしょうか?と名付けましたCacheable
。アイデアは、それCacheable
がキャッシュされる場合です。
したがって、それは単なるセマンティクスです。命名パターンとは関係ありません。Java が持っている唯一の命名パターンをここに示します: Java Naming Convention