1

アプリケーションには、もちろん UIViewController を拡張する単一の親 viewController から継承したいいくつかの viewController があります (したがって、親の兄弟クラスを除いてすべての viewController クラスを作成します)。私が知りたいこと (Objective-C での継承は初めてです) は、親の viewController クラスで、NSString *name という名前の .h ファイルでパラメーターを宣言していることです。 @property (nonatomic、retain)、そして .m ファイルに合成します。

親クラスでこれを行った後、子 viewControllers の NSString *name パラメータにアクセスできますか、それとも、親クラスから継承するそれぞれの viewController クラスで個別のパラメータを宣言する必要がありますか? 各 viewController は NSString *name パラメータに一意の値を持ちます。この場合、viewController クラスごとに個別のパラメータを作成する必要がありますか、それとも親クラス内で一度作成するだけで十分でしょうか? ?

また、親クラスでメソッドを宣言した場合、サブクラスからの参照を使用してそれらにアクセスできることを確認したいと思います (そのメソッドが子クラスでオーバーライドされていない限り)。 Javaでできますか?これは Objective-C の本質的な原則の 1 つであるため、私の推測ではイエスです。

4

1 に答える 1

0

If each view controller has a unique value, and that value is immutable, then you can simply override the getter for your name property and return a value unique to that view controller:

- (NSString *)name {
    return @"The name of this view controller";
}

If you would like to be able to mutate the result, you can declare either an iVar, or a property with a different name, then return that in a getter for the class:

- (NSString *)name {
    return _myNameiVar;
}

Also, to answer your second question, Variables, Methods, and even Properties declared inside the method file of a class are invisible to subclasses, where methods and properties declared in the header are visible to all that import the header. iVars can always be accessed through the struct access operator (->), but unless they are marked @public, the compiler will discourage you from going this route.

于 2012-12-16T03:18:59.037 に答える