Java基础问题
问题1:
Question 49:
public class Ques49 {
public static void main(String[] args) {
Employee employee = new Employee("4321"); System.out.println(employee.empID); } }
class Person{
String name = "No name"; /*public Person(){ this("fern");
System.out.println("Person1"); };*/
public Person(String nm){ name = nm;
System.out.println("Person2 = " + nm); } }
class Employee extends Person{
String empID = "0000";
public //super(id); empID = id; } }
问题:为什么需要在父类中定义一个默认的构造函数 或者 需要在子类的构造函数中加 super
问题2:
Question101:
public classextends Fruit1{ int size = 42; public static void main(String[] args) { Ques101 b = new Ques101(); //对象序列化 ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream("D:\\food.txt")); oos.writeObject(b); } catch (Exception e) { e.printStackTrace(); } finally{ } //反序列化 ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream("D:\\food.txt")); b = (Ques101) ois.readObject(); System.out.println("restored"+b.size+""); } catch (Exception e) { e.printStackTrace(); } finally{ } } }
class Food1{ Food1(){ System.out.println("1"); } }
class Fruit1 extends Food1 implements Serializable{ private static final long serialVersionUID = 1L; public Fruit1(){ System.out.println("2"); } }
输出:121restored42
问题:反序列化机制无需通过构造器来初始化对象,如何理解?
问题3:
Question29:
public class Ques29 {
public static void main(String[] args) { int [] x ={1,2,3,4,5,6}; int [] y =x; /**
* 输出结果相同,“[I@de6ced [I@de6ced” ,内存地址相同,说明x和y指向同一个对堆内存空间 */
System.out.println(x); System.out.println(y);
System.out.println(x[2]); System.out.println(y[2]);
} }
输出结果:
[I@de6ced [I@de6ced 3 3
问题:数组x和数组y的 栈内存 和 堆内存 的情况?
问题4:
Question102:
public classimplements Serializable{ private int x; public int getX(){ return x; } public Ques102(int x){ this.x = x; } private void writeObject(ObjectOutputStream s,Object b) throws IOException{ //s.defaultWriteObject(); s.writeObject(b); } public static void main(String[] args) { Ques102 b = new Ques102(100); //对象序列化 ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream("D:\\food.txt")); //oos.writeObject(b); //oos.defaultWriteObject();//使用默认的 “写对象” b.writeObject(oos,b); } catch (Exception e) { e.printStackTrace(); } finally{ } //反序列化 ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream("D:\\food.txt")); b = (Ques102) ois.readObject(); System.out.println("restored"+b.x+"");
}
}
} catch (Exception e) { e.printStackTrace(); } finally{ }
问题:defaultWriteObject()的使用
问题5:
Question119:
public class Ques119 extends Thread{ private int x=2; public static void main(String[] args) throws Exception { new Ques119().makeItSo(); new Ques119().go(); } public Ques119(){ x=5; System.out.println(Thread.currentThread().getName()); start(); } public void makeItSo() throws Exception{ join(); x=x-1; System.out.println("makeItSo "+x); } public void run(){ System.out.println(Thread.currentThread().getName()); x*=2; System.out.println("run "+x); } public void go(){
System.out.println("gogogo " + x); } }
输出:
main Thread-0 run 10 makeItSo 9