java经典问题代码,java中常见的问题
java问题,求写出代码
import java.io.*;
在通州等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、网站设计、外贸营销网站建设 网站设计制作按需求定制设计,公司网站建设,企业网站建设,品牌网站设计,全网整合营销推广,外贸营销网站建设,通州网站建设费用合理。
import java.util.Scanner;
public class Convert {
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println("请输入要转换文件所在的位置:");
String inputPath=scanner.nextLine();
System.out.println("请输入转换后文件的路径包括文件名:");
String outputPath=scanner.nextLine();
File inputFile=new File(inputPath);
if(!inputFile.exists()){
System.out.println("要转换的文件不存在!");
scanner.close();
return;
}
FileReader fr=null;
BufferedReader br=null;
FileWriter fw=null;
PrintWriter bw=null;
try{
fr=new FileReader(inputFile);
br=new BufferedReader(fr);
fw=new FileWriter(outputPath);
bw=new PrintWriter(fw);
while(true){
String data=null;
data=br.readLine();
//文件读完了,退出循环
if(data==null)
break;
bw.println(data.toUpperCase());
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(bw!=null)
bw.close();
if(fw!=null)
try {fw.close();} catch (IOException e) {}
if(br!=null)
try {br.close();} catch (IOException e) {}
if(fr!=null)
try {fr.close();} catch (IOException e) {}
scanner.close();
}
}
}
java线程的经典代码
package threadgroup;
class ThreadDemo3 extends Thread {
private String name;
private int delay;
public ThreadDemo3(String sname, int i_delay) {
name = sname;
delay = i_delay;
}
public void run() {
try {
sleep(delay);
} catch (InterruptedException e) {
}
System.out.println("多线程测试!\n" + name + "\n" + delay);
}
}
public class testMyThread {
public static void main(String[] args) {
ThreadDemo3 th1,th2,th3;
th1 = new ThreadDemo3("线程1", (int) (Math.random() * 900));
th2 = new ThreadDemo3("线程2", (int) (Math.random() * 900));
th3 = new ThreadDemo3("线程3", (int) (Math.random() * 900));
th1.start();
th2.start();
th3.start();
}
}
package threadgroup;
public class threadDemo {
public static void main(String[] args) {
Thread t = Thread.currentThread();
t.setName("你好吗?");
System.out.println("正在进行的Thread是:" + t);
try {
for (int i = 0; i 5; i++) {
System.out.println("我不叫穆继超" + i);
Thread.sleep(3000);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("Thread has wrong" + e.getMessage());
}
}
}
package threadgroup;
public class threadDemo2 implements Runnable {
public threadDemo2() {
Thread t1 = Thread.currentThread();
t1.setName("第一个主进程");
System.out.println("正在运行" + t1);
Thread t2 = new Thread(this, "");
System.out.println("在创建一个进程");
t2.start();
try {
System.out.println("使他进入第一个睡眠状态");
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第一个进程");
}
public void run() {
try {
for (int i = 0; i 5; i++) {
System.out.println("进程" + i);
Thread.sleep(3000);
}
} catch (InterruptedException e) {
// TODO: handle exception
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第二个进程");
}
public static void main(String[] args) {
new threadDemo2();
}
}
Java多线程经典问题!求代码!最好给我思路
//银行卡类
public class BanCard {
private Double money = 5000d;
public synchronized void drawMoney(double howMoney,String threadName){
try {
System.out.println(threadName+"进入取钱操作!");
Thread.sleep(2000);//为了提前是一次只有一个线程进入此方法,进行了睡眠2秒
if(howMoneymoney){
System.out.println(threadName+"余额不足!");
return;
}
this.money-=howMoney;
System.out.println(threadName+"-原始余额:"+this.money+",取钱"+howMoney+"后,还剩余额"+this.money);
System.out.println(threadName+"结束取钱操作!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//线程类
public class ThreadDemo implements Runnable {
private BanCard banCard = new BanCard();
private double howMoney=0d;
public void run() {
banCard.drawMoney(this.howMoney,Thread.currentThread().getName());
}
public double getHowMoney() {
return howMoney;
}
public void setHowMoney(double howMoney) {
this.howMoney = howMoney;
}
}
//main所在类
public class Test1 {
public static void main(String[] args) {
ThreadDemo threadDemo = new ThreadDemo();
threadDemo.setHowMoney(3000d);//取款3000
Thread thread1 = new Thread(threadDemo);
thread1.start();
threadDemo.setHowMoney(4000d);//取款4000
Thread thread2 = new Thread(threadDemo);
thread2.start();
}
}
一个很小的java问题 代码如下: enum Fruit { 苹果,梨,香蕉,西瓜,芒果 }
这种情形大多是源文件里面还有其他类定义或者内部类定义,然后编译时会有xxx.class,xxx$1.class。其他情形暂未碰到。
JAVA多态经典例题
System.out.println("1--" + a1.show(b));
a1是A类引用指向A类对象,不存在多态,一定调用A类方法。A类方法有两个show(D)和show(A),b是B类引用无法转换为D类引用,但可以转换为A类引用,因此调用show(A),输出A and A。
System.out.println("2--" + a1.show(c));
输出A and A,原因同上。
System.out.println("3--" + a1.show(d));
调用show(D),输出A and D。
System.out.println("4--" + a2.show(b));
a2是A类引用指向B类对象,可能存在多态。b是B类引用无法转换为D类引用,但可以转换为A类引用,因此调用show(A),而B类重写了show(A),因此调用的是重写后的show(A),输出B and A。
System.out.println("5--" + a2.show(c));
同上,C类引用无法转换为D类引用,但可以转换为A类引用,因此调用show(A),输出B and A。
System.out.println("6--" + a2.show(d));
调用show(D),show(D)又调用父类即A类的show(D),输出A and D
System.out.println("7--" + b.show(b));
b是B类引用指向B类对象,不存在多态,一定调用B类方法。B类一共有三个方法:重写自A类的show(A)和show(D),以及新定义的show(B)。show(b)调用show(B)方法,输出B and B
System.out.println("8--" + b.show(c));
C类继承自B类,也调用show(B)方法,输出B and B
System.out.println("9--" + b.show(d));
调用show(D),show(D)又调用父类即A类的show(D),输出A and D
当前题目:java经典问题代码,java中常见的问题
文章网址:http://ybzwz.com/article/hegjsg.html