网站优化的,施秉网站建设,个体户年报网上申报流程,抖音搜索排名86. Java代码查错#xff08;4#xff09;public class Something { public int addOne(final int x) { return x; }}此代码有错误吗#xff1f;答案: 错。int x被修饰成final#xff0c;意味着x不能在addOne method中被修改。87. Java代码查错#xff08;54public class Something { public int addOne(final int x) { return x; }}此代码有错误吗答案: 错。int x被修饰成final意味着x不能在addOne method中被修改。87. Java代码查错5public class Something { public static void main(String[] args) { Other o new Other(); new Something().addOne(o); } public void addOne(final Other o) { o.i; }}class Other { public int i;}此代码有错吗?答案: 正确。在addOne method中参数o被修饰成final。如果在addOne method里我们修改了o的reference(比如: o new Other();)那么如同上例这题也是错的。但这里修改的是o的member vairable(成员变量)而o的reference并没有改变。88. Java代码查错6class Something { int i; public void doSomething() { System.out.println(i i); }} 此代码有错误吗答案: 正确。输出的是i 0。int i属於instant variable (实例变量或叫成员变量)。instant variable有default value。int的default value是0。89. Java代码查错7class Something { final int i; public void doSomething() { System.out.println(i i); }}此代码有错误吗答案: 错。final int i是个final的instant variable (实例变量或叫成员变量)。final的instant variable没有default value必须在constructor (构造器)结束之前被赋予一个明确的值。可以修改为final int i 0;。90. Java代码查错8public class Something { public static void main(String[] args) { Something s new Something(); System.out.println(s.doSomething() returns doSomething()); } public String doSomething() { return Do something ...; }}此代码有错误吗答案: 错。看上去在main里call doSomething没有什么问题毕竟两个methods都在同一个class里。但仔细看main是static的。static method不能直接call non-static methods。可改成System.out.println(s.doSomething() returns s.doSomething());。同理static method不能访问non-static instant variable。91. Java代码查错9此处Something类的文件名叫OtherThing.javaclass Something { private static void main(String[] something_to_do) { System.out.println(Do something ...); }}此代码有错误吗答案: 正确。从来没有人说过Java的Class名字必须和其文件名相同。但public class的名字必须和文件名相同。92. Java代码查错10interface A{ int x 0;}class B{ int x 1;}class C extends B implements A { public void pX(){ System.out.println(x); } public static void main(String[] args) { new C().pX(); }}
此代码有错误吗答案错误。在编译时会发生错误(错误描述不同的JVM有不同的信息意思就是未明确的x调用两个x都匹配就象在同时import java.util和java.sql两个包时直接声明Date一样。对于父类的变量,可以用super.x来明确而接口的属性默认隐含为 public static final.所以可以通过A.x来明确。93. Java代码查错11interface Playable { void play();}interface Bounceable { void play();}interface Rollable extends Playable, Bounceable { Ball ball new Ball(PingPang);}class Ball implements Rollable { private String name; public String getName() { return name; } public Ball(String name) { this.name name; } public void play() { ball new Ball(Football); System.out.println(ball.getName()); }}此代码有错误吗答案: 错。interface Rollable extends Playable, Bounceable没有问题。interface可继承多个interfaces所以这里没错。问题出在interface Rollable里的Ball ball new Ball(PingPang);。任何在interface里声明的interface variable (接口变量也可称成员变量)默认为public static final。也就是说Ball ball new Ball(PingPang);实际上是public static final Ball ball new Ball(PingPang);。在Ball类的Play()方法中ball new Ball(Football);改变了ball的reference而这里的ball来自Rollable interfaceRollable interface里的ball是public static final的final的object是不能被改变reference的。因此编译器将在ball new Ball(Football);这里显示有错。
94、编写一个程序将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中a.txt文件中的单词用回车符分隔b.txt文件中用回车或空格进行分隔。
答
package cn.itcast; import java.io.File;
import java.io.FileReader;
import java.io.FileWriter; public class MainClass{
public static void main(String[] args) throws Exception{
FileManager a new FileManager(a.txt,new char[]{\n});
FileManager b new FileManager(b.txt,new char[]{\n, });
FileWriter c new FileWriter(c.txt);
String aWord null;
String bWord null;
while((aWord a.nextWord()) !null ){
c.write(aWord \n);
bWord b.nextWord();
if(bWord ! null)
c.write(bWord \n);
} while((bWord b.nextWord()) ! null){
c.write(bWord \n);
}
c.close();
} } class FileManager{ String[] words null;
int pos 0;
public FileManager(String filename,char[] seperators) throws Exception{
File f new File(filename);
FileReader reader new FileReader(f);
char[] buf new char[(int)f.length()];
int len reader.read(buf);
String results new String(buf,0,len);
String regex null;
if(seperators.length 1 ){
regex seperators[0] | seperators[1];
}else{
regex seperators[0];
}
words results.split(regex);
} public String nextWord(){
if(pos words.length)
return null;
return words[pos];
} } 95、编写一个程序将d:\java目录下的所有.java文件复制到d:\jad目录下并将原来文件的扩展名从.java改为.jad。
大家正在做上面这道题网上迟到的朋友也请做做这道题找工作必须能编写这些简单问题的代码
答listFiles方法接受一个FileFilter对象这个FileFilter对象就是过虑的策略对象不同的人提供不同的FileFilter实现即提供了不同的过滤策略。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; public class Jad2Java { public static void main(String[] args) throws Exception {
File srcDir new File(java);
if(!(srcDir.exists() srcDir.isDirectory()))
throw new Exception(目录不存在);
File[] files srcDir.listFiles(
new FilenameFilter(){ public boolean accept(File dir, String name) {
return name.endsWith(.java);
} }
); System.out.println(files.length);
File destDir new File(jad);
if(!destDir.exists()) destDir.mkdir();
for(File f :files){
FileInputStream fis new FileInputStream(f);
String destFileName f.getName().replaceAll(\\.java$, .jad);
FileOutputStream fos new FileOutputStream(new File(destDir,destFileName));
copy(fis,fos);
fis.close();
fos.close();
}
} private static void copy(InputStream ips,OutputStream ops) throws Exception{
int len 0;
byte[] buf new byte[1024];
while((len ips.read(buf)) ! -1){
ops.write(buf,0,len);
} }
} 由本题总结的思想及策略模式的解析
1.
class jad2java{
1. 得到某个目录下的所有的java文件集合
1.1 得到目录 File srcDir new File(d:\\java);
1.2 得到目录下的所有java文件File[] files srcDir.listFiles(new MyFileFilter());
1.3 只想得到.java的文件 class MyFileFilter implememyts FileFilter{
public boolean accept(File pathname){
return pathname.getName().endsWith(.java)
}
} 2.将每个文件复制到另外一个目录并改扩展名
2.1 得到目标目录如果目标目录不存在则创建之
2.2 根据源文件名得到目标文件名注意要用正则表达式注意.的转义。
2.3 根据表示目录的File和目标文件名的字符串得到表示目标文件的File。
//要在硬盘中准确地创建出一个文件需要知道文件名和文件的目录。
2.4 将源文件的流拷贝成目标文件流拷贝方法独立成为一个方法方法的参数采用抽象流的形式。
//方法接受的参数类型尽量面向父类越抽象越好这样适应面更宽广。
} 分析listFiles方法内部的策略模式实现原理
File[] listFiles(FileFilter filter){
File[] files listFiles();
//Arraylist acceptedFilesList new ArrayList();
File[] acceptedFiles new File[files.length];
int pos 0;
for(File file: files){
boolean accepted filter.accept(file);
if(accepted){
//acceptedFilesList.add(file);
acceptedFiles[pos] file;
}
} Arrays.copyOf(acceptedFiles,pos);
//return (File[])accpetedFilesList.toArray(); } 文章转载自: http://www.morning.cpzkq.cn.gov.cn.cpzkq.cn http://www.morning.ywrt.cn.gov.cn.ywrt.cn http://www.morning.tntgc.cn.gov.cn.tntgc.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.nlzpj.cn.gov.cn.nlzpj.cn http://www.morning.nqypf.cn.gov.cn.nqypf.cn http://www.morning.llcgz.cn.gov.cn.llcgz.cn http://www.morning.xbdd.cn.gov.cn.xbdd.cn http://www.morning.jprrh.cn.gov.cn.jprrh.cn http://www.morning.nkyc.cn.gov.cn.nkyc.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.wnmdt.cn.gov.cn.wnmdt.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.cwknc.cn.gov.cn.cwknc.cn http://www.morning.rtjhw.cn.gov.cn.rtjhw.cn http://www.morning.dtzsm.cn.gov.cn.dtzsm.cn http://www.morning.kcdts.cn.gov.cn.kcdts.cn http://www.morning.sfzwm.cn.gov.cn.sfzwm.cn http://www.morning.tcylt.cn.gov.cn.tcylt.cn http://www.morning.xsetx.com.gov.cn.xsetx.com http://www.morning.gpxbc.cn.gov.cn.gpxbc.cn http://www.morning.tdxnz.cn.gov.cn.tdxnz.cn http://www.morning.tsynj.cn.gov.cn.tsynj.cn http://www.morning.qkzdc.cn.gov.cn.qkzdc.cn http://www.morning.rxtxf.cn.gov.cn.rxtxf.cn http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.wdwfm.cn.gov.cn.wdwfm.cn http://www.morning.ntkpc.cn.gov.cn.ntkpc.cn http://www.morning.qsfys.cn.gov.cn.qsfys.cn http://www.morning.qzpqp.cn.gov.cn.qzpqp.cn http://www.morning.kzrg.cn.gov.cn.kzrg.cn http://www.morning.hbxnb.cn.gov.cn.hbxnb.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.ngzkt.cn.gov.cn.ngzkt.cn http://www.morning.krqhw.cn.gov.cn.krqhw.cn http://www.morning.dlbpn.cn.gov.cn.dlbpn.cn http://www.morning.nkpls.cn.gov.cn.nkpls.cn http://www.morning.mtyhk.cn.gov.cn.mtyhk.cn http://www.morning.lgnz.cn.gov.cn.lgnz.cn http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.lmmh.cn.gov.cn.lmmh.cn http://www.morning.dpbgw.cn.gov.cn.dpbgw.cn http://www.morning.rahllp.com.gov.cn.rahllp.com http://www.morning.bsxws.cn.gov.cn.bsxws.cn http://www.morning.hcgbm.cn.gov.cn.hcgbm.cn http://www.morning.plznfnh.cn.gov.cn.plznfnh.cn http://www.morning.gycyt.cn.gov.cn.gycyt.cn http://www.morning.tnyanzou.com.gov.cn.tnyanzou.com http://www.morning.gktds.cn.gov.cn.gktds.cn http://www.morning.swkzr.cn.gov.cn.swkzr.cn http://www.morning.gpcy.cn.gov.cn.gpcy.cn http://www.morning.hqqpy.cn.gov.cn.hqqpy.cn http://www.morning.rcttz.cn.gov.cn.rcttz.cn http://www.morning.cgntj.cn.gov.cn.cgntj.cn http://www.morning.gcrlb.cn.gov.cn.gcrlb.cn http://www.morning.wjlbb.cn.gov.cn.wjlbb.cn http://www.morning.zbjfq.cn.gov.cn.zbjfq.cn http://www.morning.qlkzl.cn.gov.cn.qlkzl.cn http://www.morning.dthyq.cn.gov.cn.dthyq.cn http://www.morning.jfwrf.cn.gov.cn.jfwrf.cn http://www.morning.zpxwg.cn.gov.cn.zpxwg.cn http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.gbfuy28.cn.gov.cn.gbfuy28.cn http://www.morning.zqfz.cn.gov.cn.zqfz.cn http://www.morning.smspc.cn.gov.cn.smspc.cn http://www.morning.gxfzrb.com.gov.cn.gxfzrb.com http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.rhsr.cn.gov.cn.rhsr.cn http://www.morning.mkydt.cn.gov.cn.mkydt.cn http://www.morning.drggr.cn.gov.cn.drggr.cn http://www.morning.ssqrd.cn.gov.cn.ssqrd.cn http://www.morning.mlcwl.cn.gov.cn.mlcwl.cn http://www.morning.hylbz.cn.gov.cn.hylbz.cn http://www.morning.dmxzd.cn.gov.cn.dmxzd.cn http://www.morning.diuchai.com.gov.cn.diuchai.com http://www.morning.dqbpf.cn.gov.cn.dqbpf.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.fjtnh.cn.gov.cn.fjtnh.cn