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

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值