小编给大家分享一下JAVA入门经典实例有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!一个饲养员给动物喂食物的例子体现JAVA中的面向对象思想,接口(抽象类)
小编给大家分享一下JAVA入门经典实例有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
一个饲养员给动物喂食物的例子体现JAVA中的面向对象思想,接口(抽象类)的用处
package com.softeem.demo;interface Animal { public void eat(Food food);}class Cat implements Animal { public void eat(Food food) { System.out.println("小猫吃" + food.getName()); }}class Dog implements Animal { public void eat(Food food) { System.out.println("小狗啃" + food.getName()); }}abstract class Food { protected String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}class Fish extends Food { public Fish(String name) { this.name = name; }}class Bone extends Food { public Bone(String name) { this.name = name; }}class Feeder { public void feed(Animal animal, Food food) { animal.eat(food); }}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); //给猫喂鱼 }}
做一个单子模式的类,只加载一次属性文件
package com.softeem.demo;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.Properties;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; }}
用JAVA中的多线程示例银行取款问题
package com.softeem.demo;class Account { private float balance = 1000; public float getBalance() { return balance; } public void setBalance(float balance) { this.balance = balance; } 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("对不起,余额不足!"); } }}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() + "元!"); }}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(); }}
用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(); }}
编程实现序列化的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(); }}
以上是“JAVA入门经典实例有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!
--结束END--
本文标题: JAVA入门经典实例有哪些
本文链接: https://www.lsjlt.com/news/222386.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-23
2024-05-23
2024-05-23
2024-05-23
2024-05-23
2024-05-23
2024-05-23
2024-05-23
2024-05-23
2024-05-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
一口价域名售卖能注册吗?域名是网站的标识,简短且易于记忆,为在线用户提供了访问我们网站的简单路径。一口价是在域名交易中一种常见的模式,而这种通常是针对已经被注册的域名转售给其他人的一种方式。
一口价域名买卖的过程通常包括以下几个步骤:
1.寻找:买家需要在域名售卖平台上找到心仪的一口价域名。平台通常会为每个可售的域名提供详细的描述,包括价格、年龄、流
443px" 443px) https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294.jpg https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294-768x413.jpg 域名售卖 域名一口价售卖 游戏音频 赋值/切片 框架优势 评估指南 项目规模 安全指南 Osprey 游戏分析 游戏调试 游戏图形 游戏物理 开源库 魔方破解 游戏安全 反作弊 安全最佳实践 游戏逻辑 LaVie
0