1,运行代码
package com . atguigu . test ; public class Father { private int i = test (); private static int j = method (); static { System . out . print ("(1)"); } Father (){ System . out . print ("(2)"); } { System . out . print ("(3)"); } public int test (){ System . out . print ("(4)"); return 1; } public static int method (){ System . out . print ("(5)"); return 1; }
package com . atguigu . test ; public class Son extends Father { private int i = test (); private static int i = method (); static { System . out . print ("(6)"); } Son (){ System . out . print ("(7)"); } { System . out . print ("(8)"); } public int test (){ System . out . print ("(9)"); return 1; } public static int method (){ System . out . print ("(10)"); return 1; } public static void main ( String [] args ){ Son s1= new Son (); System . out . println (); Son s2= new Son (); }
答案解析:(5)(1)(10)(6)(9)(3)(2)(9)(8)(7)
(9)(3)(2)(9)(8)(7)
1,本题的考点。
(1)类的初始化过程。
1,一个类创建对象需要先加载并初始化该类。(main方法所在的类需要先加载和初始 化)
2,一个子类初始化需要先初始化父类。
3,类的初始化就是执行<clinit>()方法。(包含静态变量和静态代码块,从上至下依次执行。并且只执行一次)
(2)实例初始化过程
1,实例初始化就是执行<init>()方法。
(1)<init >()方法可能重载有多个,有几个构造器就有几个< init >()方法
(2)< init >()方法由非静态实例变量显示赋值代码和非静态代码块、对应构造器代码 组成。
(3)非静态实例变量显示赋值代码和非静态代码块代码从上到下顺序执行,而对应
(4) 构造器的代码最后执行
(5)每次创建实例对象,调用对应构造器,执行的就是对应的<init >()方法
(6)< init >()方法的首行是super或 super (实参列表),即对应父类的< init >()方法(3)方法的重写
1,final修饰的方法
2,static修饰的方法
3,private修饰的方法
2,运行结果
import java . util . Arrays ;
public class Exam4{
public static void main ( String [] args ){
int i =1;
String str =" hello ";Integer num =2;
int [] arr ={1,2,3,4,5};
MyData my = new MyData ();
change ( i , str , num , arr , my );
System . out . println (" i ="+ i );
System . out . println (" str ="+ str );
System . out . println (" num ="+ num );
System . out . println (" arr ="+ Arrays . toString ( arr ));System . out . println (" my . a ="+ my . a );
public static void change ( int j , String s , Integer n , int [] a , MyData m ){
j +=1;
s +=" world "; n +=1;
a [ e ]+=1;
m . a +=1;
class MyData {
int a =10;}
答案解析:
1,本题的考点。
(1)方法参数的传递机制。
形参是基本类型。传递数据值。
实参是引用数据类型。传递地址值。 (string,包装类具有不可变性)