大书呆子牧场目标-c:第18章 - 继承

时间:2012-10-21 20:23:39

标签: objective-c

我目前正在通过“大书呆子牧场”手册学习目标-c。我重复了第18章的代码,但是我遇到了错误。使用64位macos 10.6女巫的实际版本。

编译说:

  

构建目标BMITime

     

CompileC   /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/Objects-normal/x86_64/main.o   BMITime / main.m正常x86_64 objective-c   com.apple.compilers.llvm.clang.1_0.compiler       cd / Users / pm / Dropbox / objective-c / BMITime       setenv LANG en_US.US-ASCII       /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang   -x objective-c -arch x86_64 -fmessage-length = 0 -std = gnu99 -fobjc-arc -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Wreturn-type -Wno-implicit-atomic-properties -Wformat -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wuninitialized -Wno -unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-sign-compare -Wshorten-64-to-32 -Wpointer-sign -Wno-newline-eof -Wno-selector -Wno-strict-selector -match -Wno-undeclared-selector -Wno-deprecated-implementation -DDEBUG = 1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk   -fasm-blocks -Wprotocol -Wdeprecated-declarations -mmacosx-version-min = 10.7 -g -Wno-conversion -Wno-sign-conversion“-DIBOutlet = attribute ((iboutlet))”   “-DIBOutletCollection(类名)= 属性((iboutletcollection(类名)))”   “-DIBAction = void”属性((ibaction)“ - isquote   /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/BMITime-generated-files.hmap   -I /用户/ PM /库/开发商/ Xcode中/ DerivedData / BMITime-cjqpmtvlmdoeiecyxbofznzjgpij /编译/中间体/ BMITime.build /调试/ BMITime.build / BMITime-自己的目标,headers.hmap   -I /用户/ PM /库/开发商/ Xcode中/ DerivedData / BMITime-cjqpmtvlmdoeiecyxbofznzjgpij /编译/中间体/ BMITime.build /调试/ BMITime.build / BMITime-所有目标headers.hmap   -iquote /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/BMITime-project-headers.hmap   -I /用户/ PM /库/开发商/ Xcode中/ DerivedData / BMITime-cjqpmtvlmdoeiecyxbofznzjgpij /编译/产品/调试/包括   -I /用户/ PM /库/开发商/ Xcode中/ DerivedData / BMITime-cjqpmtvlmdoeiecyxbofznzjgpij /编译/中间体/ BMITime.build /调试/ BMITime.build / DerivedSources / x86_64的   -I /用户/ PM /库/开发商/ Xcode中/ DerivedData / BMITime-cjqpmtvlmdoeiecyxbofznzjgpij /编译/中间体/ BMITime.build /调试/ BMITime.build / DerivedSources   -F /用户/ PM /库/开发商/ Xcode中/ DerivedData / BMITime-cjqpmtvlmdoeiecyxbofznzjgpij /编译/产品/调试   -include /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/PrecompiledHeaders/BMITime-Prefix-bbjacqhmgaerfrhblktcvkpzzntb/BMITime-Prefix.pch   -MMD -MT依赖项-MF /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/Objects-normal/x86_64/main.d   --serialize-diagnostics /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/Objects-normal/x86_64/main.dia   -c /Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m -o /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime .build /对象 - 正常/ x86_64的/ main.o

     

/Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m:21:10:错误:否   'Employee'的可见@interface声明了选择器   'setWeightInKilos:'           [person setWeightInKilos:96];            ^ /Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m:22:10:错误:'Employee'没有可见的@interface声明选择器   'setHeightInMeters:'           [person setHeightInMeters:1.8];            ^ /Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m:26:22:错误:'Employee'没有可见的@interface声明选择器   'bodyMassIndex'           float bmi = [person bodyMassIndex];                        生成^ 3错误。

我的代码如下:

的main.m

#import <Foundation/Foundation.h>
#import "Employee.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        // Erstellt eine Instanz von Person
        Employee *person = [[Employee alloc] init];

        // Gibt den Instanzvariablen interessante Werte
        [person setWeightInKilos:96];
        [person setHeightInMeters:1.8];
        [person setEmployeeID:15];

        // Ruft die Methode bodyMassIndex auf
        float bmi = [person bodyMassIndex];
        NSLog(@"Employee %d has a BMI of %f", [person employeeID], bmi);


    }
    return 0;
}

Person.h

#import <Foundation/Foundation.h>

// Die Klasse Person erbt alle Instanzvariablen
// und Methoden, die von der Klasse NSObject definiert wurden
@interface Person : NSObject
{
    // Sie hat zwei Instanzvariablen
    float heightInMeters;
    int weightInKilos;
}
@property float heightInMeters;
@property int weightInKilos;

// Diese Methode berechnet den Body-Mass-Index
- (float)bodyMassIndex;
@end

Employee.h

#import <Foundation/Foundation.h>
#import "Person.h"

@interface Employee : NSObject
{
    int employeeID;
}
@property int employeeID;
@end

Employee.m

#import "Employee.h"

@implementation Employee

@synthesize employeeID;

@end

Person.m

#import "Person.h"

@implementation Person

@synthesize heightInMeters, weightInKilos;

- (float)bodyMassIndex
{
    //return weightInKilos / (heightInMeters * heightInMeters);
    float h = [self heightInMeters];
    return [self weightInKilos] / (h * h);
}

@end

1 个答案:

答案 0 :(得分:4)

  

错误:'Employee'没有可见的@interface声明选择器'setWeightInKilos'
  错误:'Employee'没有可见的@interface声明选择器'setHeightInMeters'   错误:'Employee'没有可见的@interface声明选择器'bodyMassIndex'

没有这本书,但在我看来,Employee应该继承Person,而不是NSObject

这将通过继承为Employee提供缺少的属性/函数。