欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

5個(gè)JAVA入門必看的經(jīng)典實(shí)例

 更新時(shí)間:2017年10月18日 10:37:36   投稿:lijiao  
這篇文章主要為大家詳細(xì)介紹了5個(gè)JAVA入門必看的經(jīng)典實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

入門必看的5個(gè)JAVA經(jīng)典實(shí)例,供大家參考,具體內(nèi)容如下

1.一個(gè)飼養(yǎng)員給動(dòng)物喂食物的例子體現(xiàn)JAVA中的面向?qū)ο笏枷?接口(抽象類)的用處

package com.softeem.demo;
/**
*@author leno
*動(dòng)物的接口
*/

interface Animal {
 public void eat(Food food);

}

/**
*@author leno
*一種動(dòng)物類:貓
*/
class Cat implements Animal {
 public void eat(Food food) {
  System.out.println("小貓吃" + food.getName());

 }
}
/**
*@author leno
*一種動(dòng)物類:狗
*/

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
*飼養(yǎng)員類
*
*/
class Feeder {

 /**
  *飼養(yǎng)員給某種動(dòng)物喂某種食物
  *@param animal
  *@param food
  */

 public void feed(Animal animal, Food food) {
  animal.eat(food);
 }

}
/**
*@author leno
*測(cè)試飼養(yǎng)員給動(dòng)物喂食物
*/

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.做一個(gè)單子模式的類,只加載一次屬性文件

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 單子模式,保證在整個(gè)應(yīng)用期間只加載一次配置屬性文件
*/

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中的多線程示例銀行取款問(wèn)題

package com.softeem.demo;

/**
*@author leno
*賬戶類
*默認(rèn)有余額,可以取款
*/

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("對(duì)不起,余額不足!");
  }
 }
}
/**
*@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中的多線程示例生產(chǎn)者和消費(fèi)者問(wèn)題

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 = "產(chǎn)品" + i;
   stack.push(product);
   System.out.println("生產(chǎn)了: " + 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("消費(fèi)了: " + 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.編程實(shí)現(xiàn)序列化的Student(sno,sname)對(duì)象在網(wǎng)絡(luò)上的傳輸

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 "學(xué)號(hào):" + 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 = "客戶端程序收到服務(wù)器端程序傳輸過(guò)來(lái)的學(xué)生對(duì)象>> " + 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();

 }

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot中@Import注解的使用方式

    SpringBoot中@Import注解的使用方式

    這篇文章主要介紹了SpringBoot中@Import注解的使用方式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05
  • springboot使用DynamicDataSource動(dòng)態(tài)切換數(shù)據(jù)源的實(shí)現(xiàn)過(guò)程

    springboot使用DynamicDataSource動(dòng)態(tài)切換數(shù)據(jù)源的實(shí)現(xiàn)過(guò)程

    這篇文章主要給大家介紹了關(guān)于springboot使用DynamicDataSource動(dòng)態(tài)切換數(shù)據(jù)源的實(shí)現(xiàn)過(guò)程,Spring Boot應(yīng)用中可以配置多個(gè)數(shù)據(jù)源,并根據(jù)注解靈活指定當(dāng)前使用的數(shù)據(jù)源,需要的朋友可以參考下
    2023-08-08
  • Java實(shí)現(xiàn)多對(duì)多網(wǎng)絡(luò)通訊的流程

    Java實(shí)現(xiàn)多對(duì)多網(wǎng)絡(luò)通訊的流程

    這篇文章主要介紹了Java實(shí)現(xiàn)多對(duì)多網(wǎng)絡(luò)通訊的流程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • 學(xué)生視角帶你了解Java內(nèi)部類

    學(xué)生視角帶你了解Java內(nèi)部類

    說(shuō)起內(nèi)部類這個(gè)詞,想必很多人都不陌生,但是又會(huì)覺(jué)得不熟悉。原因是平時(shí)編寫代碼時(shí)可能用到的場(chǎng)景不多,用得最多的是在有事件監(jiān)聽(tīng)的情況下,并且即使用到也很少去總結(jié)內(nèi)部類的用法。今天我們就來(lái)一探究竟
    2022-03-03
  • Spring框架 引入@Resource注解報(bào)空指針的解決

    Spring框架 引入@Resource注解報(bào)空指針的解決

    這篇文章主要介紹了Spring框架 引入@Resource注解報(bào)空指針的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • redis中存儲(chǔ)list<map>,list<entity>的處理

    redis中存儲(chǔ)list<map>,list<entity>的處理

    本文主要介紹了redis中存儲(chǔ)list<map>,list<entity>的處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • Maven scala和java混合打包方式

    Maven scala和java混合打包方式

    這篇文章主要介紹了Maven scala和java混合打包方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • feign調(diào)用返回object類型轉(zhuǎn)換方式

    feign調(diào)用返回object類型轉(zhuǎn)換方式

    這篇文章主要介紹了feign調(diào)用返回object類型轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • springboot實(shí)現(xiàn)返回文件流

    springboot實(shí)現(xiàn)返回文件流

    這篇文章主要介紹了springboot實(shí)現(xiàn)返回文件流方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 使用dubbo+zookeeper+spring boot構(gòu)建服務(wù)的方法詳解

    使用dubbo+zookeeper+spring boot構(gòu)建服務(wù)的方法詳解

    這篇文章主要給大家介紹了關(guān)于如何使用dubbo+zookeeper+spring boot構(gòu)建服務(wù)的相關(guān)資料,文中通過(guò)示例代碼及圖片介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05

最新評(píng)論