for (id thing in animals)
{
if ([thing respondsToSelector:@selector(bark)])
{
//You are allowed to send messages to id
[thing bark];
[thing setDogName:@"Lassie"];
//compiler error because it doesn't know it is a Dog class
//thing.dogName = @"Lassie";
}
if ([thing isKindOfClass:[Dog class]])
{
[thing bark];
//this is safe because we checked the class first and now we can cast it as so
Dog *dog = (Dog *)thing;
dog.dogName = @"Lassie";
}
}
然后期望输入
val result = Process("kinit user@SOMETHING.COM")
我有办法从代码输入吗?
答案 0 :(得分:2)
要使用流程的输入和输出,您需要打开它,以便获取Toast.LENGTH_LONG
和STDIN
流。问题Java Process with Input/Output Stream中可以找到一个很好的例子。如果您尝试自动执行需要在提示上输入的代码,这仍然非常痛苦。
在unix中,传统上使用Expect库进行处理,幸运的是,github上有一个java端口Expect4J。使用Expect4J,您仍然以相同的方式启动流程并获取它的输入和输出流,然后将它们提供给Expect4J并从那里编程您的交互:
STDOUT