响应有参方法

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

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        // 动态类型检测 :响应方法
        
        Animal *ani = [Dog new];
        
        SEL s1 = @selector(eat);
        
        // 1)通过performSelector响应无参的方法
        if ([ani respondsToSelector:s1]) {
            [ani performSelector:s1]; // 作用,响应方法 没有参数
        } else{
            NSLog(@"该对象不能调用此方法");
        }
        
        // 2)通过performSelector witOobject响应有参的方法
        SEL s2 = @selector(eat:);
        [ani performSelector:s2 withObject:@"一块"];
        
        // 3)通过performSelector: withObject: withObject:响应有里两个参的方法
        [ani performSelector:@selector(eat:andFoodName:) withObject:@"他" withObject:@"tang"];
    }
    return 0;
}

以下几种不同语言和框架实现主线任务执行完返回结果后执行异步任务且方法响应的方式: ### Java 使用 CompletableFuture ```java import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; public class CompletableFutureExample { public static CompletableFuture<String> mainTask() { return CompletableFuture.supplyAsync(() -> { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return "Main task result"; }); } public static CompletableFuture<String> asyncTask() { return CompletableFuture.supplyAsync(() -> { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return "Async task result"; }); } public static void main(String[] args) throws ExecutionException, InterruptedException { CompletableFuture<String> mainResult = mainTask(); String result = mainResult.get(); System.out.println(result); CompletableFuture<String> asyncResult = asyncTask(); String asyncRes = asyncResult.get(); System.out.println(asyncRes); } } ``` ### Spring Boot 使用 @Async 和 CompletableFuture ```java import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.stereotype.Service; import java.util.concurrent.CompletableFuture; @Service @EnableAsync public class AsyncService { public CompletableFuture<String> mainTask() { return CompletableFuture.supplyAsync(() -> { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return "Main task result"; }); } @Async public CompletableFuture<String> asyncTask() { return CompletableFuture.supplyAsync(() -> { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return "Async task result"; }); } } ``` ### Python 使用 asyncio ```python import asyncio async def main_task(): await asyncio.sleep(2) return "Main task result" async def async_task(): await asyncio.sleep(1) return "Async task result" async def main(): main_result = await main_task() print(main_result) async_result = await async_task() print(async_result) asyncio.run(main()) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值