| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2236 人关注过本帖
标题:入门必看的5个JAVA经典实例
只看楼主 加入收藏
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
结帖率:90.32%
收藏
 问题点数:0 回复次数:15 
入门必看的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

import

import

import

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

import

import

import

import

import

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 
2009-12-28 11:30
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:0 
支持下

C#超级群 74862681,欢迎大家的到来!
2009-12-28 15:01
xwlking
Rank: 4
来 自:湖南衡阳
等 级:业余侠客
威 望:1
帖 子:97
专家分:279
注 册:2009-11-1
收藏
得分:0 
顶!!!!!!!!

人总需相信自己,人总需依靠自己. ---永远的Beyond---
2009-12-28 18:35
zhuyunshen
Rank: 6Rank: 6
等 级:侠之大者
威 望:2
帖 子:212
专家分:455
注 册:2009-11-22
收藏
得分:0 
还好
2009-12-31 16:43
恕欲静
Rank: 1
等 级:新手上路
帖 子:2
专家分:3
注 册:2009-12-31
收藏
得分:0 
新年好.

我选择沉默?
是沉默选择了我!
2009-12-31 16:58
xiaoxiaoyun
Rank: 2
等 级:论坛游民
帖 子:8
专家分:15
注 册:2009-11-10
收藏
得分:0 
看了一下,支持你一下,学习了谢谢.
2010-01-01 15:02
xwlking
Rank: 4
来 自:湖南衡阳
等 级:业余侠客
威 望:1
帖 子:97
专家分:279
注 册:2009-11-1
收藏
得分:0 
很好

人总需相信自己,人总需依靠自己. ---永远的Beyond---
2010-01-04 22:23
自JAVA风暴
Rank: 1
来 自:山东
等 级:新手上路
帖 子:8
专家分:5
注 册:2009-11-10
收藏
得分:0 
不错
2010-01-05 16:48
muyu7823
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-1-7
收藏
得分:0 
谢谢分享
2010-01-07 18:08
b1412
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-2-28
收藏
得分:0 
学习一下了来
2010-01-09 21:17
快速回复:入门必看的5个JAVA经典实例
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044228 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved