您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 入门必看的5个JAVA经典实例
入门必看的5个JAVA经典实例1.一个饲养员给动物喂食物的例子体现JAVA中的面向对象思想,接口(抽象类)的用处packagecom.softeem.demo;/***@authorleno*动物的接口*/interfaceAnimal{publicvoideat(Foodfood);}/***@authorleno*一种动物类:猫*/classCatimplementsAnimal{publicvoideat(Foodfood){System.out.println(小猫吃+food.getName());}}/***@authorleno*一种动物类:狗*/classDogimplementsAnimal{publicvoideat(Foodfood){System.out.println(小狗啃+food.getName());}}/***@authorleno*食物抽象类*/abstractclassFood{protectedStringname;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}}/***@authorleno*一种食物类:鱼*/classFishextendsFood{publicFish(Stringname){this.name=name;}}/***@authorleno*一种食物类:骨头*/classBoneextendsFood{publicBone(Stringname){this.name=name;}}/***@authorleno*饲养员类**/classFeeder{/***饲养员给某种动物喂某种食物*@paramanimal*@paramfood*/publicvoidfeed(Animalanimal,Foodfood){animal.eat(food);}}/***@authorleno*测试饲养员给动物喂食物*/publicclassTestFeeder{publicstaticvoidmain(String[]args){Feederfeeder=newFeeder();Animalanimal=newDog();Foodfood=newBone(肉骨头);feeder.feed(animal,food);//给狗喂肉骨头animal=newCat();food=newFish(鱼);feeder.feed(animal,food);//给猫喂鱼}}2.做一个单子模式的类,只加载一次属性文件packagecom.softeem.demo;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.io.InputStream;importjava.util.Properties;/***@authorleno单子模式,保证在整个应用期间只加载一次配置属性文件*/publicclassSingleton{privatestaticSingletoninstance;privatestaticfinalStringCONFIG_FILE_PATH=E:\\config.properties;privatePropertiesconfig;privateSingleton(){config=newProperties();InputStreamis;try{is=newFileInputStream(CONFIG_FILE_PATH);config.load(is);is.close();}catch(FileNotFoundExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}publicstaticSingletongetInstance(){if(instance==null){instance=newSingleton();}returninstance;}publicPropertiesgetConfig(){returnconfig;}publicvoidsetConfig(Propertiesconfig){this.config=config;}}3.用JAVA中的多线程示例银行取款问题packagecom.softeem.demo;/***@authorleno*账户类*默认有余额,可以取款*/classAccount{privatefloatbalance=1000;publicfloatgetBalance(){returnbalance;}publicvoidsetBalance(floatbalance){this.balance=balance;}/***取款的方法需要同步*@parammoney*/publicsynchronizedvoidwithdrawals(floatmoney){if(balance=money){System.out.println(被取走+money+元!);try{Thread.sleep(1000);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}balance-=money;}else{System.out.println(对不起,余额不足!);}}}/***@authorleno*银行卡*/classTestAccount1extendsThread{privateAccountaccount;publicTestAccount1(Accountaccount){this.account=account;}@Overridepublicvoidrun(){account.withdrawals(800);System.out.println(余额为:+account.getBalance()+元!);}}/***@authorleno*存折*/classTestAccount2extendsThread{privateAccountaccount;publicTestAccount2(Accountaccount){this.account=account;}@Overridepublicvoidrun(){account.withdrawals(700);System.out.println(余额为:+account.getBalance()+元!);}}publicclassTest{publicstaticvoidmain(String[]args){Accountaccount=newAccount();TestAccount1testAccount1=newTestAccount1(account);testAccount1.start();TestAccount2testAccount2=newTestAccount2(account);testAccount2.start();}}4.用JAVA中的多线程示例生产者和消费者问题packagecom.softeem.demo;classProducerimplementsRunnable{privateSyncStackstack;publicProducer(SyncStackstack){this.stack=stack;}publicvoidrun(){for(inti=0;istack.getProducts().length;i++){Stringproduct=产品+i;stack.push(product);System.out.println(生产了:+product);try{Thread.sleep(200);}catch(InterruptedExceptione){e.printStackTrace();}}}}classConsumerimplementsRunnable{privateSyncStackstack;publicConsumer(SyncStackstack){this.stack=stack;}publicvoidrun(){for(inti=0;istack.getProducts().length;i++){Stringproduct=stack.pop();System.out.println(消费了:+product);try{Thread.sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();}}}}classSyncStack{privateString[]products=newString[10];privateintindex;publicsynchronizedvoidpush(Stringproduct){if(index==product.length()){try{wait();}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}notify();products[index]=product;index++;}publicsynchronizedStringpop(){if(index==0){try{wait();}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}notify();index--;Stringproduct=products[index];returnproduct;}publicString[]getProducts(){returnproducts;}}publicclassTestProducerConsumer{publicstaticvoidmain(String[]args){SyncStackstack=newSyncStack();Producerp=newProducer(stack);Consumerc=newConsumer(stack);newThread(p).start();newThread(c).start();}}5.编程实现序列化的Student(sno,sname)对象在网络上的传输packagecom.softeem.demo;importjava.io.IOException;importjava.io.ObjectInputStream;importjava.io.ObjectOutputStream;importjava.io.Serializable;importjava.net.ServerSocket;importjava.net.Socket;classStudentimplementsSerializable{privateintsno;privateStringsname;publicStudent(intsno,Stringsname){this.sno=sno;this.sname=sname;}publicintgetSno(){returnsno;}publicvoidsetSno(intsno){this.sno=sno;}publicStringgetSname(){returnsname;}publicvoidsetSname(Stringsname){this.sname=sname;}@Over
本文标题:入门必看的5个JAVA经典实例
链接地址:https://www.777doc.com/doc-5912221 .html