21xrx.com
2025-03-16 08:03:21 Sunday
文章检索 我的文章 写文章
Java 线下面试:参考实操项目
2023-06-15 11:26:26 深夜i     9     0
Java线下面试 实操项目 基础语法 集合框架 多线程

Java开发者在面试过程中往往需要展示对语言特性和API的理解以及对代码的熟练度。本文将提供一些参考实操项目以准备Java线下面试。

1. 基础语法

在Java线下面试中,基本语法问题经常会被问到。下面是一些基础语法的代码示例:

// 定义类
public class Person {
  // 成员变量
  String name;
  int age;
  // 构造方法
  public Person(String name, int age)
    this.name = name;
    this.age = age;
  
  // 成员方法
  public void sayHello() {
    System.out.println("Hello, my name is " + this.name + ", I'm " + this.age + " years old.");
  }
}
// 运行代码
public class Main {
  public static void main(String[] args) {
    Person person = new Person("张三", 18);
    person.sayHello(); // 输出:Hello, my name is 张三, I'm 18 years old.
  }
}

2. 集合框架

Java中的集合框架也是面试重点。下面是一些常用集合类型的示例代码:

// 声明List对象
List
  list = new ArrayList<>();
 
// 添加元素
list.add("Java");
list.add("Python");
list.add("C++");
// 遍历List
for (String item : list) {
  System.out.println(item);
}
// 声明Map对象
Map
  map = new HashMap<>();
 
// 添加键值对
map.put("Java", 1);
map.put("Python", 2);
map.put("C++", 3);
// 遍历Map
for (String key : map.keySet()) {
  System.out.println(key + " : " + map.get(key));
}

3. 多线程

在Java中,多线程的知识也是必不可少的。下面是一个多线程示例:

// 自定义Thread子类
class MyThread extends Thread {
  @Override
  public void run() {
    System.out.println("MyThread running...");
  }
}
// 自定义Runnable实现类
class MyRunnable implements Runnable {
  @Override
  public void run() {
    System.out.println("MyRunnable running...");
  }
}
// 运行多线程代码
public static void main(String[] args) {
  // 方式一:继承Thread类
  MyThread myThread = new MyThread();
  myThread.start();
  // 方式二:实现Runnable接口
  MyRunnable myRunnable = new MyRunnable();
  Thread thread = new Thread(myRunnable);
  thread.start();
}

  
  

评论区

    相似文章
请求出错了