一、基礎語法實例
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
這是一個典型的Java的Hello World程序,可以幫助初學者熟悉基本語法。其中,public表示訪問權限,class定義類名,main方法為程序入口,String[] args表示接收命令行參數。
二、流程控制實例
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入n:");
int n = sc.nextInt();
int f1 = 1, f2 = 1;
for (int i = 1; i <= n; i++) {
System.out.print(f1 + " ");
int temp = f1;
f1 = f2;
f2 = temp + f2;
}
}
}
這個程序可以輸出斐波那契數列,輸入一個正整數n,輸出前n項斐波那契數列。其中,利用Scanner類實現讀入用戶輸入,然後使用for循環遍歷輸出。
三、面向對象實例
3.1 類定義實例
public class Student {
String name;
int age;
String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public void display() {
System.out.println("姓名:" + name);
System.out.println("年齡:" + age);
System.out.println("性別:" + gender);
}
}
這個例子定義了一個Student類,其中包含三個成員變量:name,age和gender。它還有一個構造函數和一個顯示函數,構造函數用於初始化實例,顯示函數用於在屏幕上顯示學生的信息。
3.2 繼承實例
public class Animal {
public void move() {
System.out.println("動物可以移動");
}
}
public class Dog extends Animal {
public void move() {
System.out.println("狗可以跑和走");
}
}
這個例子定義了一個Animal類和一個Dog類,Dog類繼承自Animal類,重寫了move()方法。其中,Animal類的move()方法用於描述動物可以移動,而Dog類的move()方法則專門描述了狗的移動方式。
3.3 接口實例
public interface Sport {
void run();
void jump();
}
public class Athlete implements Sport {
public void run() {
System.out.println("運動員跑步");
}
public void jump() {
System.out.println("運動員跳高");
}
}
這個例子定義了一個Sport接口,其中包含兩個方法:run()和jump();另一個類Athlete實現了Sport接口,其中重載了run()和jump()方法。在Athlete類中,運動員可以使用run()來跑步,使用jump()來跳高。
四、多線程實例
4.1 創建線程實例
public class MyThread extends Thread {
public void run() {
System.out.println("線程開始運行:" + this.getName());
for (int i = 0; i < 5; i++) {
System.out.println(this.getName() + "運行:" + i);
try {
sleep((int) Math.random() * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("線程結束:" + this.getName());
}
}
public class TestThread {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
t1.start();
t2.start();
}
}
這個例子創建了一個MyThread繼承自Thread的類,並且重載了run()方法,然後創建了兩個MyThread實例,最後啟動這兩個線程。在程序運行時,兩個線程會同時執行並打印出不同的運行次數。
4.2 線程同步實例
public class Account {
private int balance = 100;
public synchronized void deposit(int amount) {
balance += amount;
System.out.println("存款" + amount + "\t餘額:" + balance);
}
public synchronized void withdraw(int amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("取款" + amount + "\t餘額:" + balance);
} else {
System.out.println("餘額不足");
}
}
}
public class DepositThread extends Thread {
private Account account;
public DepositThread(Account account) {
this.account = account;
}
public void run() {
for (int i = 0; i < 10; i++) {
account.deposit(10);
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class WithdrawThread extends Thread {
private Account account;
public WithdrawThread(Account account) {
this.account = account;
}
public void run() {
for (int i = 0; i < 10; i++) {
account.withdraw(10);
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class TestAccount {
public static void main(String[] args) {
Account account = new Account();
new DepositThread(account).start();
new WithdrawThread(account).start();
}
}
這個例子演示了如何使用synchronized關鍵字實現線程的同步。其中,Account類代表一個銀行賬戶,deposit()方法用於向賬戶中存入款項,withdraw()方法用於從賬戶中取款。DepositThread和WithdrawThread代表兩個線程,分別進行存款和取款操作,這兩個線程對應着同一個賬戶對象,保證了線程同步。
原創文章,作者:YHKRH,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/325179.html