封装、重写、重载
l 代码的高效率并不是封装的优点。
l 实现一个接口的类和接口是is a 的关系。
l 子类重写父类方法的时候,方法返回值的类型一定相同,否则编译不通过。
l 如果子类实现多态时,若只是方法的返回值不同,将在第二个及以后的方法编译出错。
l 如果实现多态时,方法的参数是f(int ,int)和f(long , int);那么f(1,2)将执行f(int , int)。
l 子类中如果调用super( )或this( )则必须在方法的第一行,且不能同时出现。
内部类
l static inner classes:
只有最上层的类可以有,并且它只能访问static 变量
l member inner classes:
要创建它的实例,必须有一个指向上层的引用,它不能含有static方法。
l local inner classes:
只有被声明为final的本地变量(不是上层)才可以直接访问,local and anonymous 内部类不能被声明为private、public、protected or static。
l anonymous inner classes:
匿名类不能含有构造函数。
注:在内部类中,this 指的是内部类的实例,要得到enclosing class 的实例,要在前面加前缀。
线程
l 创建、初始化、开始新线程
线程可以通过继承Thread 和重写public void run( )来加以创建
也可以通过有Runnable 对象参数的构造方法来实现。
Start( )方法只能调用一否会抛出RunnableException, (ThreadStateException)
Thread 类实现了Runnable
l 线程的状态的转换
runnable->running->dead
wait( )和sleep( )可使线程waiting
waiting->runnable
一个线程dead后不能start again
Object 定义了wait( )、notify( )、notifyAll()等方法。
Notify() is to sigmal that it should proceed in executing instructions.
Interrupted() and interrupt() are instance methods of Thread.
Sleep( ) and yield( ) are static methods of Thread.
l 使线程sleep
sleep( )可以使线程停止执行一段时间,但锁没解开。
Interrupt( )可以停止sleep( )和waiting( )
处于I/O阻塞状态或在等待锁的线程不能被Interrupt( )
sleep( )方法必须有try,catch,或被throws
l 通过waiting 和 notifying 进行通信
注意用synchronized
l 死锁、yield( )
l 只有run( )方法在Runnable interface 中被定义。
The java.lang.Math
Using the java.lang.Math Class
l The abs() method is overloaded to take an int ,a long ,a float ,or a double.
l The abs() method can return a negative if the argument is the minimum int or long value equal to the value of Integer.MIN_VALUE or Long.MIN_VALUE, respectively.
l The max() method is overloaded to take int,long,float,double arguments.
l The min() method is overloaded to take int,long,float,double arguments.
l The random() method returns a double greater than or equal to 0.0 and less than 1.0.
l Random does not take any arguments.
l The methods ceil(), floor(), round() all return integer equivalent doubles.
l The round() method is overloaded to take a float(the other method takes a double). Math.round( 9.5) = 10
l The method sin( ), cos( ),and tan() take a double angle in radians.
l The method sqrt() can return NaN if the argument is NaN or less than zero. Static double sqrt(double)
Strings
Creating and Working with Strings in java
l String operations are carried out by String class, which can be found in the java.lang package.
l Strings can be concatenated with the following syntax:
String s = new String(<>);
l “ + ” and toString( )
Storage of Strings and String immutability
l In java, strings are immutable. Their contents can never be changed.
l Text for strings defined at compile time and strings with identical contents may share the same memory space.
Constructors and Methods for the String Class
l None of the methods for the String class change the actual contends of the string; they only return new values.
l The equals() method is used to compare two strings and determine whether they are identical. The == operator should not be used, as it will only determine whether the two string variables reference the same object.
l The indexOf( ) and lastIndexOf() methods of the String class can be used to search a string’s contents for a specific character or sequence of characters.
Using the StringBuffer Class
l Their contents can be changed.
l StringBuffers are initialized with a certain character capacity. If this capacity is extended, the StringBuffer object automatically grows.
Constructors and Methods for the StringBuffer Class
l The setCharAt() , insert() , delete(), and append() methods are used to work with individual characters and string of characters in StringBuffers.
l All positional arguments for methods in the String or StringBuffer classes are zero-based. The first charater is reference as zero.
The java.util Package
The Collection Framework
l The four properties that differentiate collections are
sorted,ordered,allows duplicates,and uses keys.
l In strict terminology ,a set cannot be both ordered and sorted.
l An ordered set keeps track of the sequence in which objects are added to it. It is unsorted, however.
l A sorted set uses the natural ordering of its objects, such as alphabetical.
Collection Interfaces and Classes
l A Set can store only unique objects, not duplicates.
l A List keeps the objects indexed in a definite order.
l A Map stores objects in ascending to keys.
l SortedSet stores objects in ascending order without duplicates.
l The null object can’t be used to sort objects, so a SortedSet can’t contain objects that are null. Likewise, a SortedMap can’t contain keys that are null.
l HashSet implements the Set Interface.
l LinkedList ,vector, and ArrayList implement List.
l HashMap and Hashtable implement the Map interface.
l TreeMap implements the SortedMap interface.
The java.awt Package
Components
l The java.awt.Component class is the abstract superclass for all nonmenu graphical components in java.awt package.
l A custom component can be created by extending an existing component subclass or by extending the java.awt.Component class directly.
Containers
A container is a type if component that contains other components.
A container is itself a subclass of component,so it is possible to nest containers within one another.
Layout Managers
A container uses a layout manager to position the components that it contains. It is specified using the Container.setLayout() method.
The layout manager for a container determines the size and position of each component within a container.
If no layout manager is specified for a container then absolute positioning is used to determine each component’s position.
Menus
MenuComponent is at the top of the hierarchy for all menu subclasses.MenuComponent does not inherit from Component.
A MenuBar must be associated with a Frame in order to visible.That is accomplished using the Frame.setMenuBar() method.
A MenuBar can have Menu objects added to it using the add() method.
Menus can have MenuItems and all subclasses of MenuItem added to it using the add() method.Menu is a subclass of MenuItem,so a Menu can be added to another Menu.
Painting
The Canvas class is used by creating a subclass of Canvas, then overriding the paint() method.
All Components have a paint() method.
To ask the GUI to refresh a component, the repaint() menthod can be called. There is no guarantee it will be refreshed right away.
Images,text and shapes can be rendered to a Canvas or any Component using the Graphics object associated with the paint() method.