//方法 getText() 读取标签的文字。
g.drawString( \ g.drawString( \ } }
23:字符串类(字符常量与字符变量)。
在程序中存在大量的数据来代表程序的状态,其中有些数据在程序的运行过程中值会发生改变,有些数据在程序运行过程中值不能发生改变,这些数据在程序中分别被叫做变量和常量。 变量代表程序的状态。程序通过改变变量的值来改变整个程序的状态,或者说得更大一些,也就是实现程序的功能逻辑。 例如:int x; 常量代表程序运行过程中不能改变的值。 常量在程序运行过程中主要有2个作用 1、代表常数,便于程序的修改 2、增强程序的可读性
常量的语法格式和变量类型,只需要在变量的语法格式前面添加关键字final即可。在Java编码规范中,要求常量名必须大写。 例如: final double PI = 3.14;int r =5;
24:读写文件(字节流与格式流)。
public static void main(String args[]) {
double x, x1; int n; try {
FileInputStream f11 = new FileInputStream( \先创建输入字节流对象f11
DataInputStream f12 = new DataInputStream( f11 ); //再创建输入格式流对象f12 n = 1000;
for( int i = 1; i <= n; i++ ) {
x = Math.sqrt( i );
x1 = f12.readDouble( ); //按双精度格式将一个数据读入 x1 if( i >= 990 )
System.out.println( i + \ }
f12.close( ); }
catch( Exception e ) {
System.out.println(e); } }
25:线程。
public class PrintTest {
public static void main( String args[] ) {
//声明 4 个线程类 PrintThread 的类对象:thread1, thread2, thread3, thread4。
PrintThread thread1, thread2, thread3, thread4; //创建这 4 个线程。
thread1 = new PrintThread(); thread2 = new PrintThread(); thread3 = new PrintThread(); thread4 = new PrintThread();
//启动 4 个线程。各自按首先确定的暂停时间\睡觉\,谁先\睡醒\就输出自己的名称。
thread1.start(); thread2.start(); thread3.start(); thread4.start(); } }
//自定义的线程类 PrintThread 继承于线程类 Thread。 class PrintThread extends Thread int sleepTime; //暂停时间。
// PrintThread constructor assigns name to thread // by calling Thread constructor
// 线程类 PrintThread 的构造方法:确定不超过 5 秒的“睡觉”时间,输出该线程的名称和暂停时间。 public PrintThread( ) {
// sleep between 0 and 5 seconds
sleepTime = (int) ( Math.random() * 5000 ); System.out.println( \
\ sleep: \ }
// execute the thread
//线程启动后,执行 run()方法。完成后,该线程结束。 public void run() {
// put thread to sleep for a random interval //先按确定的暂停时间“睡觉”。 try {
Thread.sleep( sleepTime ); }
catch ( InterruptedException exception ) { System.err.println( exception.toString() ); }
// print thread name
//\睡醒\后输出本身的名称。
System.out.println( getName() ); } }