《入门必看的5个JAVA经典实例》由会员分享,可在线阅读,更多相关《入门必看的5个JAVA经典实例(34页珍藏版)》请在金锄头文库上搜索。
1、入门必看的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 im
2、plements 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(Strin
3、g 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 TestFee
4、der 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.FileInputStr
5、eam;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 Pr
6、operties 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 blocke.printStackTrace(); catch (IOException e) / TODO Auto-generated catch blocke.prin
7、tStackTrace(); 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*账户类*默认有余额,可以取款*
8、/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(
9、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
10、 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();