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();
}
}
}
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();
}
}
//銀行卡類
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();
}
}
這種情形大多是源文件里面還有其他類定義或者內部類定義,然后編譯時會有xxx.class,xxx$1.class。其他情形暫未碰到。
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://m.kartarina.com/article36/hegjsg.html
成都網站建設公司_創新互聯,為您提供手機網站建設、響應式網站、企業網站制作、電子商務、云服務器、做網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯