[コンテキスト:Javaの新機能、4か月のトップ。C++の古い手。]
私は多くの場所で固定サイズ(「固定文字列」)の配列を必要とするライブラリに取り組んでいます。この特定の問題に依存性注入(qv)を使用しようとしているので、次の形式が必要です。
class Foo
{
private Bar injectedBar;
private char[] injectedFixedString;
Foo(Bar injectedBar, /* what can go here? */ char[5] injectedFixedString);
{ /* initializing code goes here /* }
}
シンプルが必要です-これは自動生成された通信プロトコルに入ります。派生元のプロトコルとデータベースを完全に制御することはできません。最終的なコードには、これらのインスタンスが数千とは言わないまでも数百あります。だから、すべてを考えると:
C ++の唯一の代替手段です:
char injectedFixedString[5];
カスタムクラスを作成するには?何かのようなもの:
class FixedBarString {
/* could also set this in the constructor, but this complicates code generation a tad */
public static integer STRING_SIZE = 5; /* string size */
char[] fixedString = new char[STRING_SIZE];
FixedBarString(char[] string) throws RuntimeException {
/* check the string here; throw an exception if it's the wrong size.
I don't like constructors that throw however. */
}
public void setString(char[] string) throws RuntimeException {
/* check the string here */
}
public char[] getString() {
/* this isn't actually safe, aka immutable, without returning clone */
}
public char[] createBlankString() {
return new char[STRING_SIZE];
}
}
ありがとう。(これが多すぎるコードの場合はお詫びします)。