16. Hiding
public class MyDate {
private int day;
private int month;
private int year;
public void setDay(int day){
...
}
public int getDay(){
return day;
}
...
}
18. 构造函数
public class Thing {
private int x;
public Thing() {
x = 47;
}
public Thing(int new_x) {
x = new_x;
}
public int getX() {
return x;
}
}
19. Java Document API
public interface List extends Collection {
/**
* Returns the number of elements in this list. If this list contains
* more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* @return the number of elements in this list.
*/
int size();
…
20. Java Document API
public int size()
Returns the number of elements in this list. If
this list contains more than
Integer.MAX_VALUE elements, returns
Integer.MAX_VALUE.
Returns:
the number of elements in this list.
…
36. 方法重写例子
class Parent
{
public void Parent()
{System.out.println(“Parent”);}
}
class Child extends Parent
{
public void Child ()
{System.out.println(“Child ”);}
}
37. 问题
public void setVar (int a, int b, float c)
Which overload the setVar method? (Choose Two)
A. Private void setVar (int a, float c, int b) { }
B. Protected void setVar (int a, int b, float c) { }
C. Public int setVar (int a, float c, int b) (return a;)
D. Public int setVar (int a, int b, float c) (return a;)
E. Protected float setVar (int a, int b, float c) (return c;)
38. super 关键字
super 用来引用父类的变量或方法
通常用来扩展父类方法的功能
super() 表示调用父类的构造函数(如果
父类存在缺省的构造函数)
子类的构造函数会自动调用 super()
48. 数组初始化
数组中的每个元素具有相同的数据类型,包括基本数
据类型和对象
对于基本数据类型,系统会自动初始化
如上例: a 初始化成 a = {0,0,0,0,0};
boolean 类型的元素会自动初始化为 false
对于对象类型,系统不会自动初始化,还必须分别手
工创建
如: Circle[] c = new Circle[5];
//c = {null,null,null,null,null};
Circle c1 = new Circle();
C[0] = c1;
49. 多维数组
定义:
int[][] a;
int a[][];
创建多维数组时 , 必须指明第一维的大小
int twoDim[][] = new int [4][];
int twoDim[0] = new int[5];
或: int twoDim[][] = new int[4][5];
51. 问题:
Which of the following statements are true?
1) The elements in a Java array can only be of primitive
tray can only be of primitive types, not objects
2) Arrays are initialized to default values wherever they
are created
3) An array may be dynamically resized using the
setSize method
4) You can find out the size of an array using the size
method
53. Collection 框架
Vector
List
ArrayList
Collection
HashSet
Set
TreeSet
HashMa
p
Map
Hashtable
54. 问题
Which statement is true for the class
java.util.HashSet?
A. The elements in the collection are ordered.
B. The collection is guaranteed to be
immutable.
C. The elements in the collection are
guaranteed to be unique.
D. The elements in the collection are
accessed using a unique key.
E. The elements in the collections are
guaranteed to be synchronized.
55. 问题
You need to store elements in a collection that
guarantees that no duplicates are stored and
all elements
can be accessed in natural order. Which
interface provides that capability?
A. java.util.Map.
B. java.util.Set.
C. java.util.List.
D. java.util.SortedSet.
E. java.util.SortedMap.
F. java.util.Collection.
58. 实例变量的访问
public class Circle
{ int x,y;
public int x(){return x;}
public void setX(int newX){x=newX;}
public static void main(String args[]){
Circle myCircle=new Circle();
myCircle.setX(23);
System.out.println("myCircle.x="+myCircle.x());
}
}
59. 类变量的访问
public class Circle{
static int x,y;
public int x(){return x;}
public void setX(int newX){x=newX;}
public static void main(String args[]){
Circle myCircle=new Circle();
myCircle.setX(23);
System.out.println("myCircle.x="+myCircle.x());
}
}
60. static 方法
Public class GeneralFunction{
public static int addUp(int x,int y){
return x + y;
}
}
65. 问题
Which of the following statements are true?
1) static methods do not have access to the
implicit variable called this
2) A static method may be called without
creating an instance of its class
3) A static method may not be overriden to be
non-static
4) A static method may not be overloaded
66. final 关键字
使用场合:
终极类 (final class)
终极方法 (final method)
常量 (const) ( 例: final int MAX_SIZE=
10)
67. final 类 / 方法
final 类不被继承
final 方法不被重写
声明为 static 或 private 的方法自动地声
明为 final
例:
java 中的 String 类为 final 类,不能被继
承。
71. interface
接口( interface )定义:
public interface Transparency{
public static final int OPAQUE=1;
public static final int BITMASK=2;
public static final int TRANSLUCENT=3;
public int getTransparency();
}
73. 问题
Which of the following statements are true?
1) An interface can only contain method and not
variables
2) Interfaces cannot have constructors
3) A class must extend only one other class and
implement only one interface
4) Interfaces are the Java approach to addressing its
lack of multiple inheritance, but require implementing
classes to create the functionality of the Interfaces.
78. 匿名类 (anonymous class)
public class MainUI extends JDialog{
JTextField txt = new JTextField("");
...
txt.addKeyListener(new KeyListener(){
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
});
}
79. Which statement is true?
A. An anonymous inner class may be declared as final.
B. An anonymous inner class can be declared as
private.
C. An anonymous inner class can implement multiple
interfaces.
D. An anonymous inner class can access final variables
in any enclosing scope.
E. Construction of an instance of a static inner class
requires an instance of the enclosing outer class.