日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java基础教程案例_Java入门的五个经典案例

發布時間:2023/12/31 java 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java基础教程案例_Java入门的五个经典案例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這篇文章主要為大家詳細介紹了5個JAVA入門必看的經典實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下

入門必看的5個JAVA經典實例,供大家參考,具體內容如下

1.一個飼養員給動物喂食物的例子體現JAVA中的面向對象思想,接口(抽象類)的用處

package com.softeem.demo;

/**

*@author leno

*動物的接口

*/

interface Animal {

public void eat(Food food);

}

/**

*@author leno

*一種動物類:貓

*/

class Cat implements Animal {

public void eat(Food food) {

System.out.println("小貓吃" + food.getName());

}

}

/**

*@author leno

*一種動物類:狗

*/

class Dog implements Animal {

public void eat(Food food) {

System.out.println("小狗啃" + food.getName());

}

}

/**

*@author leno

*食物抽象類

*/

abstract class Food {

protected String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

/**

*@author leno

*一種食物類:魚

*/

class Fish extends Food {

public Fish(String name) {

this.name = name;

}

}

/**

*@author leno

*一種食物類:骨頭

*/

class Bone extends Food {

public Bone(String name) {

this.name = name;

}

}

/**

*@author leno

*飼養員類

*

*/

class Feeder {

/**

*飼養員給某種動物喂某種食物

*@param animal

*@param food

*/

public void feed(Animal animal, Food food) {

animal.eat(food);

}

}

/**

*@author leno

*測試飼養員給動物喂食物

*/

public class TestFeeder {

public static void main(String[] args) {

Feeder feeder = new Feeder();

Animal animal = new Dog();

Food food = new Bone("肉骨頭");

feeder.feed(animal, food); //給狗喂肉骨頭

animal = new Cat();

food = new Fish("魚");

feeder.feed(animal, food); //給貓喂魚

}

}

2.做一個單子模式的類,只加載一次屬性文件

package com.softeem.demo;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

/**

* @authorleno 單子模式,保證在整個應用期間只加載一次配置屬性文件

*/

public class Singleton {

private static Singleton instance;

private static final String CONFIG_FILE_PATH = "E:\\config.properties";

private Properties config;

private Singleton() {

config = new Properties();

InputStream is;

try {

is = new FileInputStream(CONFIG_FILE_PATH);

config.load(is);

is.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static Singleton getInstance() {

if (instance == null) {

instance = new Singleton();

}

return instance;

}

public Properties getConfig() {

return config;

}

public void setConfig(Properties config) {

this.config = config;

}

}

3.用JAVA中的多線程示例銀行取款問題

package com.softeem.demo;

/**

*@author leno

*賬戶類

*默認有余額,可以取款

*/

class Account {

private float balance = 1000;

public float getBalance() {

return balance;

}

public void setBalance(float balance) {

this.balance = balance;

}

/**

*取款的方法需要同步

*@param money

*/

public synchronized void withdrawals(float money) {

if (balance >= money) {

System.out.println("被取走" + money + "元!");

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

balance -= money;

} else {

System.out.println("對不起,余額不足!");

}

}

}

/**

*@author leno

*銀行卡

*/

class TestAccount1 extends Thread {

private Account account;

public TestAccount1(Account account) {

this.account = account;

}

@Override

public void run() {

account.withdrawals(800);

System.out.println("余額為:" + account.getBalance() + "元!");

}

}

/**

*@authorleno

*存折

*/

class TestAccount2 extends Thread {

private Account account;

public TestAccount2(Account account) {

this.account = account;

}

@Override

public void run() {

account.withdrawals(700);

System.out.println("余額為:" + account.getBalance() + "元!");

}

}

public class Test {

public static void main(String[] args) {

Account account = new Account();

TestAccount1 testAccount1 = new TestAccount1(account);

testAccount1.start();

TestAccount2 testAccount2 = new TestAccount2(account);

testAccount2.start();

}

}

4.用JAVA中的多線程示例生產者和消費者問題

package com.softeem.demo;

class Producer implements Runnable {

private SyncStack stack;

public Producer(SyncStack stack) {

this.stack = stack;

}

public void run() {

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

String product = "產品" + i;

stack.push(product);

System.out.println("生產了: " + product);

try {

Thread.sleep(200);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

class Consumer implements Runnable {

private SyncStack stack;

public Consumer(SyncStack stack) {

this.stack = stack;

}

public void run() {

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

String product = stack.pop();

System.out.println("消費了: " + product);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

class SyncStack {

private String[] products = new String[10];

private int index;

public synchronized void push(String product) {

if (index == product.length()) {

try {

wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

notify();

products[index] = product;

index++;

}

public synchronized String pop() {

if (index == 0) {

try {

wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

notify();

index--;

String product = products[index];

return product;

}

public String[] getProducts() {

return products;

}

}

public class TestProducerConsumer {

public static void main(String[] args) {

SyncStack stack = new SyncStack();

Producer p = new Producer(stack);

Consumer c = new Consumer(stack);

new Thread(p).start();

new Thread(c).start();

}

}

5.編程實現序列化的Student(sno,sname)對象在網絡上的傳輸

package com.softeem.demo;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

import java.net.ServerSocket;

import java.net.Socket;

class Student implements Serializable {

private int sno;

private String sname;

public Student(int sno, String sname) {

this.sno = sno;

this.sname = sname;

}

public int getSno() {

return sno;

}

public void setSno(int sno) {

this.sno = sno;

}

public String getSname() {

return sname;

}

public void setSname(String sname) {

this.sname = sname;

}

@Override

public String toString() {

return "學號:" + sno + ";姓名:" + sname;

}

}

class MyClient extends Thread {

@Override

public void run() {

try {

Socket s = new Socket("localhost", 9999);

ObjectInputStream ois = new ObjectInputStream(s.getInputStream());

Student stu = (Student) ois.readObject();

String msg = "客戶端程序收到服務器端程序傳輸過來的學生對象>> " + stu;

System.out.println(msg);

ois.close();

s.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

class MyServer extends Thread {

@Override

public void run() {

try {

ServerSocket ss = new ServerSocket(9999);

Socket s = ss.accept();

ObjectOutputStream ops = new ObjectOutputStream(s.getOutputStream());

Student stu = new Student(1, "趙本山");

ops.writeObject(stu);

ops.close();

s.close();

ss.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public class TestTransfer {

public static void main(String[] args) {

new MyServer().start();

new MyClient().start();

}

}

總結

以上是生活随笔為你收集整理的java基础教程案例_Java入门的五个经典案例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。