JAVA期末考试试题

package z;

import java.io.RandomAccessFile;

public class J_Test1 {
	public static void main(String[] args) throws Exception {
		RandomAccessFile f = new RandomAccessFile("a.txt", "rw");
		f.writeBoolean(true);
		f.writeBoolean(false);
		for (int i = 1; i < 10; ++i)
			f.writeInt(i);
		f.seek(1);
		System.out.println(f.readInt());
		f.close();
	}
}

writeBoolean

按单字节值将 boolean 写入该文件。值 true 写出为值 (byte)1;值false 写出为值(byte)0。写入从文件指针的当前位置开始。

true false 1 2 3 4 5 6 7 8 9

1 + 1 + 4

所以输出为2


package z;

import java.io.RandomAccessFile;

public class J_Test1 {
	public static void main(String[] args) throws Exception {
		RandomAccessFile f = new RandomAccessFile("a.txt", "rw");
		f.writeBoolean(true);
		f.writeBoolean(false);
		for (int i = 1; i < 10; ++i)
			f.writeInt(i);
		f.seek(0);
		System.out.println(f.readBoolean());
		System.out.println(f.readBoolean());
		for (int i = 1; i < 10; ++i){
			System.out.print(f.readInt() + " ");
		}
		f.close();
	}
}


package ss;

class J_SuperClass {
	int m_data = 0;

	J_SuperClass() {
		System.out.println("J_SuperClass::构造函数");
		mb_add(1);
	}

	public void mb_add(int i) {
		System.out.println("J_SuperClass::mb_add");
		m_data += i;
	}

	public void mb_print() {
		System.out.println(m_data);
	}
}

class J_SubClass extends J_SuperClass {
	J_SubClass() {
		System.out.println("J_SubClass::构造函数");
		mb_add(2);
	}

	public void mb_add(int i) {
		System.out.println("J_SubClass::mb_add");
		m_data += i * 2;
	}
}

public class J_Test {
	public static void main(String[] args) {
		J_SuperClass a = new J_SubClass();
		a.mb_print();
		a.mb_add(6);
		a.mb_print();
	}
}


J_SuperClass::构造函数
J_SubClass::mb_add
J_SubClass::构造函数
J_SubClass::mb_add
6
J_SubClass::mb_add
18

上转型对象,书上的知识点太挫了

package ss;

class A{
	A(){
		System.out.println("A::构造函数");
		show();
	}
	public void show(){
		System.out.println("A:show");
	}
}
class B extends A{
	B(){
		System.out.println("B::构造函数");
		show();
	}
	public void show(){
		System.out.println("B:show");
	}
}
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		A a = new B();
	}

}


posted @ 2013-01-13 14:03  N3verL4nd  阅读(623)  评论(0编辑  收藏  举报