static void Main(string[] args) { const int n = 100; Book[] bk = new Book[n] ; Random ran = new Random(); for (int i = 0; i < n; i++) { int RandKey = ran.Next(0, 2);//左闭右开 if (RandKey == 0) bk[i] = new Book("Computer"); else bk[i] = new Book("English"); } for(int i=0;i<n;i++) { if(bk[i].kind[0]=='C') bk[i].setStrategy( new ComputerStrategy() ); else bk[i].setStrategy(new EnglishStrategy()); bk[i].operation(); } 用Java写
时间: 2024-02-14 14:35:20 浏览: 351
C#中static void Main(string[] args) 参数示例详解
public static void main(String[] args) {
final int n = 100;
Book[] bk = new Book[n];
Random ran = new Random();
for (int i = 0; i < n; i++) {
int RandKey = ran.nextInt(2);
if (RandKey == 0) {
bk[i] = new Book("Computer");
} else {
bk[i] = new Book("English");
}
}
for (int i = 0; i < n; i++) {
if (bk[i].getKind().charAt(0) == 'C') {
bk[i].setStrategy(new ComputerStrategy());
} else {
bk[i].setStrategy(new EnglishStrategy());
}
bk[i].operation();
}
}
阅读全文