您的位置:首页 > 职场人生

JAVA基础面试必须要掌握的几道题之(1)

2017-02-19 20:43 537 查看
关键字题目:

static:

 public class T{

   static int i=1;

  static void runs(){

     i++;

}

 main(){

  T t1,t2;

  system,out.printer(t1.i+","+t2.i);

   t1.runs();

  system.out.printer(t1.i+","+t2.i);

}

}

///////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////

public class Value3 {

    static int c = 0;

    

    Value3(){

     c = 15;

    }

    

    Value3(int i){

     c = i;

    }

    

    static void inc(){

     c++;

    }

    

}

public class Count {

  public static void prt(String s){

  System.out.println(s);

  }

  

   Value3 v = new Value3(10);

   static Value3 v1,v2;

   static{//此即为static块

   System.out.println("v1.c="+v1.c+" v2.c="+v2.c);

   v1 = new Value3(27);

   System.out.println("v1.c="+v1.c+" v2.c="+v2.c);

   v2 = new Value3(15);

   System.out.println("v1.c="+v1.c +" v2.c="+v2.c);

   }

   public static void main(String[] args) {

Count ct =new Count();

System.out.println("ct.c="+ct.v.c);

System.out.println("v1.c="+v1.c + " v2.c="+v2.c);

v1.inc();

System.out.println("v1.c="+v1.c + " v2.c="+v2.c);

System.out.println("ct.c="+ct.v.c);

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////

public class StaticCls {

    public static void main(String[] args) {

       OuterCls.InnerCls oi = new OuterCls.InnerCls();

    }

}

class OuterCls {

    public static class InnerCls {

       InnerCls() {

           System.out.println("InnerCls");

       }

    }



////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////

this:

public class Hello { 

String s = "Hello"; 

public Hello(String s)



System.out.println("s = " + s); 

System.out.println("1 -> this.s = " + this.s); 

this.s = s; 

System.out.println("2 -> this.s = " + this.s); 



public static void main(String[] args) { 

Hello x="new" Hello("HelloWorld!"); 





public class A { 

public A() { 

new B(this).print(); 



public void print() { 

System.out.println("Hello from A!"); 





public class B { 

A a; 

public B(A a) { 

this.a = a; 



public void print() { 

a.print(); 

System.out.println("Hello from B!"); 



main(){

   B b = new B();

system.out.printer(b)

}



//难度考题:

public static void main(String[] args) {

     char[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',

             'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',

             'W', 'X', 'Y', 'Z' };

     int len = letters.length;

     char[] chs = new char[5];

               空白处1         

     Random rd = new Random();

     for (int i = 0; i < chs.length; i++) {

         int index;

         do {

                       空白处2        

         } while (          空白处3          );

                   空白处4         

                   空白处5         

     }

     System.out.println(Arrays.toString(chs));

    }

  (1).

下列选项中,能填入空白处1的代码是(    )

 A.

boolean[] flags = new boolean[len];

 B.

boolean[] flags = new boolean[]{};

 C.

boolean[] flags = new boolean[5];

 D.

boolean[] flags = new boolean[];

  (2).

下列选项中,能填入空白处2的代码是(    )

 A.

index = rd.nextInt();

 B.

index = rd.nextInt(5);

 C.

index = rd.nextInt(len + 1);

 D.

index = rd.nextInt(len);

 (3).

下列选项中,能填入空白处3的代码是(    )

 A.

!flags[index]

 B.

!flags[i]

 C.

flags[index]

 D.

flags[i]

  (4).

下列选项中,能填入空白处4的代码是(    )

 A.

chs[i] = letters[index];

 B.

chs[i] = letters[i];

 C.

chs[index] = letters[i];

 D.

chs[index] = letters[index];

 (5).

下列选项中,能填入空白处5的代码是(    )

 A.

flags[i] = true;

 B.

flags[i] = false;

 C.

flags[index] = true;

 D.

flags[index] = false;

//非静态方法时,掌握:编译看左,运行看右 ---这句话的意思是?

/////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////

class Fu {

 intnum = 5;

static void method4() {

 System.out.println("fu method_4");

 }

void method3() {

 System.out.println("fu method_3");

 }

}

class Zi extends Fu {

 intnum = 8;

static void method4() {

 System.out.println("zi method_4");

 }

void method3() {

 System.out.println("zi method_3");

 }

}

class DuoTaiDemo4 {

 public static void main(String[] args) {

Fu f = new Zi();

 System.out.println(f.num);//与父类一致

 f.method4();//与父类一致

 f.method3();//编译时与父类一致,运行时与子类一致

  Ziz = new Zi();

 System.out.println(z.num);

 z.method4();

 z.method3();

 }

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////

public class Test6 {

 private int number;

 private String username;

 private String password;

 private int x = 100;

 public Test6(int n) {

  number = n;  // 这个还可以写为: this.number=n;

 }

 public Test6(int i, String username, String password) {

  // 成员变量和参数同名,成员变量被屏蔽,用"this.成员变量"的方式访问成员变量.

  this.username = username;

  this.password = password;

 }

 // 默认不带参数的构造方法

 public Test6() {

  this(0, "未知", "空"); // 通过this调用另一个构造方法

 }

 public Test6(String name) {

  this(1, name, "空"); // 通过this调用另一个构造方法

 }

 public static void main(String args[]) {

  Test6 t1 = new Test6();

  Test6 t2 = new Test6("游客");

  t1.outinfo(t1);

  t2.outinfo(t2);

 }

 private void outinfo(Test6 t) {

  System.out.println("-----------");

  System.out.println(t.number);

  System.out.println(t.username);

  System.out.println(t.password);

  f(); // 这个可以写为: this.f();

 }

 private void f() {

  // 局部变量与成员变量同名,成员变量被屏蔽,用"this.成员变量"的方式访问成员变量.

  int x;

  x = this.x++;

  System.out.println(x);

  System.out.println(this.x);

 }

 

 //返回当前实例的引用

 private Test6 getSelf() {

  return this; 

 }

}

////////////////////////////////////////////////////////////////////////////////////////////////////////

public class Father {

 public String v="Father";

 public String x="输出了Father类的public成员变量x!!!";

 

 public Father() {

  System.out.println("Father构造方法被调用!");

 }

 

 public Father(String v){

  this.v="Father类的带参数构造方法!运行了.";

 }

 public void outinfo(){

  System.out.println("Father的outinfo方法被调用");

 } 

 public static void main(String[] args) {

  // TODO 自动生成方法存根

 }

}

 

package org.leizhimin;

public class Son extends Father{

 public String v="Son";

 

 public Son() {

  super();      //调用超类的构造方法,只能放到第一行.

  System.out.println("Son无参数构造方法被调用!");

  //super();      //错误的,必须放到构造方法体的最前面.

 } 

 

 public Son(String str){

  super(str);

  System.out.println("Son带参数构造方法被调用!");

 }

 //覆盖了超类成员方法outinfo()

 public void outinfo(){ 

  System.out.println("Son的outinfo()方法被调用");

 } 

 

 public void test(){

  

  String v="哈哈哈哈!";   //局部变量v覆盖了成员变量v和超类变量v

  

  System.out.println("------1-----");

  System.out.println(v);   //输出局部变量v

  System.out.println(this.v);  //输出(子类)成员变量v

  System.out.println(super.v); //输出超类成员变量v 

  

  System.out.println("------2-----");

  System.out.println(x);   //输出超类成员变量v,子类继承而来

  System.out.println(super.x); //输出超类成员变量v

  

  System.out.println("------3-----");

  outinfo();   //调用子类的outinfo()方法

  this.outinfo();  //调用子类的outinfo()方法

  super.outinfo(); //调用父类的outinfo()方法

 } 

 

 public static void main(String[] args) {

  new Son().test();

  

 }

}

静态导入:

一般导入一个类都是impornt....啥啥的,但是当只导入该类的静态方法时,就可以用到这个

imporint static xxx.*; 也可以指定只导入某个静态方法,把这个.*换成你需要的方法名就ok了.

这么做的目的为了当多次调用这个类的这个方法时,可以简化你对调用这个方法的操作。

////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////

foreach循环在不需要索引的情况下去遍历集合是最佳的遍历方式,

foreach不会考虑循环对象的长度,它会一次性遍历完

public class ForeachTest

{

    public static void main(String[] args)

    {

        int[] arr = {1, 2, 3, 4, 5};

        

        System.out.println("----------旧方式遍历------------");

        //旧式方式        

        for(int i=0; i<arr.length; i++)

        {

            System.out.println(arr[i]);

        }

        

        System.out.println("---------新方式遍历-------------");

        

        //新式写法,增强的for循环

        for(int element:arr)

        {

            System.out.println(element);

        }

        

        System.out.println("---------遍历二维数组-------------");

        

        //遍历二维数组

        

        int[][] arr2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} ;

        

        for(int[] row : arr2)

        {

            for(int element : row)

            {

                System.out.println(element);

            }

        }

        

        //以三种方式遍历集合List

        

        List<String> list = new ArrayList<String>();

        

        list.add("a");

        list.add("b");

        list.add("c");

        

        System.out.println("----------方式1-----------");

        //第一种方式,普通for循环

        for(int i = 0; i < list.size(); i++)

        {

            System.out.println(list.get(i));

            

        }

        

        System.out.println("----------方式2-----------");

        //第二种方式,使用迭代器

        for(Iterator<String> iter = list.iterator(); iter.hasNext();)

        {

            System.out.println(iter.next());

        }

        System.out.println("----------方式3-----------");

        //第三种方式,使用增强型的for循环

        for(String str: list)

        {

            System.out.println(str);

            

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: