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();
}
}