Given two files, GrizzlyBear.java and Salmon.java:
1. package animals.mammals;
2.
3. public class GrizzlyBear extends Bear{
4. void hunt() {
5. Salmon s = findSalmon();
6. s.consume();
7. }
8. }
1. package animals.fish;
2.
3. public class Salmon extends Fish {
4. public void consume() { /* do stuff */ }
5. }
If both classes are in the correct directories for their packages, and the Mammal class
correctly defines the findSalmon() method, which change allows this code to compile?
A. add import animals. mammals.*; at line 2 in Salmon.java
B. add import animals.fish.*; at line 2 in GrizzlyBearjava
C. add import animals.fish.Salmon.*; at line 2 in GrizzlyBear.java
D. add import animals. mammals.GrizzlyBear*; at line 2 in Salmon.java
答案:B
參考:7-1 Java 套件
-------------------------------------------------------
5. Salmon s = findSalmon(); 用到了這支魚的方法, 所以要import這個類別
沒有留言:
張貼留言