Objective c 一些协议问题

Objective c 一些协议问题,objective-c,ios,interface,protocols,Objective C,Ios,Interface,Protocols,我知道如何用java来做,但我在objetice-c中很难做到 在java中,我会有这样一个接口: public interface Car { public void startCar(); } //public interface Car { // public void startCar(); //} @protocol Car - (void)startCar; @end //public class SomeCarImpl implements Car { //

我知道如何用java来做,但我在objetice-c中很难做到

在java中,我会有这样一个接口:

public interface Car {
    public void startCar();
}
//public interface Car {
//    public void startCar();
//}

@protocol Car
- (void)startCar;
@end

//public class SomeCarImpl implements Car {
//    public void startCar() {
//        System.out.println("starting the car...");
//    }
//}

@interface SomeCarImpl : NSObject<Car>

@end

@implementation SomeCarImpl

- (void)startCar {
    NSLog(@"starting the car...");
}

@end

// Car myCar = new SomeCarImpl();
// car.startCar();
id<Car> myCar = [[SomeCarImpl alloc] init];
[myCar startCar];
以及将实现此接口的a类:

public class SomeCarImpl implements Car {
   public void startCar() {
      System.out.println("starting the car...");
   }
}
现在我可以在我的主课上讲这个了

public void MainClass {
   public static void main(String [] args) {
       Car myCar = new SomeCarImpl();
       car.startCar();
   }
}
现在我在objective-c遇到麻烦了。前两件事很容易通过协议实现,但当我想这样调用它时,什么都不会发生

//header
id <Car> *myCar;

//instance
myCar = [[SomeCarImpl alloc] init];

//calling and nothing happens
[myCar startCar];

您的Java代码将模糊地转换为如下内容:

public interface Car {
    public void startCar();
}
//public interface Car {
//    public void startCar();
//}

@protocol Car
- (void)startCar;
@end

//public class SomeCarImpl implements Car {
//    public void startCar() {
//        System.out.println("starting the car...");
//    }
//}

@interface SomeCarImpl : NSObject<Car>

@end

@implementation SomeCarImpl

- (void)startCar {
    NSLog(@"starting the car...");
}

@end

// Car myCar = new SomeCarImpl();
// car.startCar();
id<Car> myCar = [[SomeCarImpl alloc] init];
[myCar startCar];
//公共接口车{
//公共无效startCar();
//}
@协议车
-(无效)startCar;
@结束
//公共类SomeCarImpl实现Car{
//公共无效startCar(){
//System.out.println(“启动汽车…”);
//    }
//}
@接口SomeCarImpl:NSObject
@结束
@mpl的实现
-(无效)startCar{
NSLog(@“启动汽车…”);
}
@结束
//Car myCar=new SomeCarImpl();
//car.startCar();
id myCar=[[SomeCarImpl alloc]init];
[myCar startCar];
在ObjC代码中,需要删除此行中的星号:

id <Car> *myCar;
id*myCar;

因为类型
id
已经是一个指针了(尽管我不认为这不是问题的根源)。

您的Java代码将模糊地转换为如下内容:

public interface Car {
    public void startCar();
}
//public interface Car {
//    public void startCar();
//}

@protocol Car
- (void)startCar;
@end

//public class SomeCarImpl implements Car {
//    public void startCar() {
//        System.out.println("starting the car...");
//    }
//}

@interface SomeCarImpl : NSObject<Car>

@end

@implementation SomeCarImpl

- (void)startCar {
    NSLog(@"starting the car...");
}

@end

// Car myCar = new SomeCarImpl();
// car.startCar();
id<Car> myCar = [[SomeCarImpl alloc] init];
[myCar startCar];
//公共接口车{
//公共无效startCar();
//}
@协议车
-(无效)startCar;
@结束
//公共类SomeCarImpl实现Car{
//公共无效startCar(){
//System.out.println(“启动汽车…”);
//    }
//}
@接口SomeCarImpl:NSObject
@结束
@mpl的实现
-(无效)startCar{
NSLog(@“启动汽车…”);
}
@结束
//Car myCar=new SomeCarImpl();
//car.startCar();
id myCar=[[SomeCarImpl alloc]init];
[myCar startCar];
在ObjC代码中,需要删除此行中的星号:

id <Car> *myCar;
id*myCar;

因为类型
id
已经是一个指针(尽管我不认为这不是问题的根源).

让我们看看目标C代码-java代码不重要你设置了一些断点吗?让我们看看目标C代码-java代码不重要你设置了一些断点吗?真的不知道为什么它不起作用,但现在一切都很好…可能是xcode 4.2的问题;-)thx很多人真的不知道为什么它不起作用,但现在一切都很好…可能是xcode 4.2的问题;-)thx很多