/** * 3*. 从输入流中拷贝内容到输入中 * @throws IOException */ public void copyStream(InputStream is, OutputStream os) throws IOException { // q个读过q程可以参阅 readToBuffer 中的注释 String line; BufferedReader reader = new BufferedReader(new InputStreamReader(is)); PrintWriter writer = new PrintWriter(new OutputStreamWriter(os)); line = reader.readLine(); while (line != null) { writer.println(line); line = reader.readLine(); } writer.flush(); // 最后确定要把输出流中的东西都写出去?nbsp; // q里不关?writer 是因?os 是从外面传进来的 // 既然不是从这里打开的,也就不从q里关闭 // 如果关闭?writerQ封装在里面?os 也就被关?nbsp; }
/** * 3. 调用 copyStream(InputStream, OutputStream) Ҏ拯文本文g */ public void copyTextFile(String inFilename, String outFilename) throws IOException { // 先根据输?输出文g生成相应的输?输出?nbsp; InputStream is = new FileInputStream(inFilename); OutputStream os = new FileOutputStream(outFilename); copyStream(is, os); // ?copyStream 拯内容 is.close(); // is 是在q里打开的,所以需要关?nbsp; os.close(); // os 是在q里打开的,所以需要关?nbsp; }
public static void main(String[] args) throws IOException { int sw = 1; // 三种试的选择开?nbsp; AccessTextFile test = new AccessTextFile();
switch (sw) { case 1: // 试?nbsp; { InputStream is = new FileInputStream("E:\\test.txt"); StringBuffer buffer = new StringBuffer(); test.readToBuffer(buffer, is); System.out.println(buffer); // 读?buffer 中的内容写出?nbsp; is.close(); break; } case 2: // 试?nbsp; { StringBuffer buffer = new StringBuffer("Only a test\n"); test.writeFromBuffer(buffer, System.out); break; } case 3: // 试拯 { test.copyTextFile("E:\\test.txt", "E:\\r.txt"); } break; } }
}
==============
//q个是用reader的,q个不会出现异常
import java.io.*; import java.util.*;
public class Test { public static void main(String[] args) throws Exception { File file = new File("Test.java"); BufferedReader reader = new BufferedReader(new FileReader(file)); String line = null;
public class Polymorphism{ public static void main(String[] args) { A b = new B(); b.fb(); } }
class A { public A(){
} public void fa() { System.out.println("CLASS A :Function fa Runing......"); }
public void fb() { System.out.println("CLASS A :Function fb Runing......"); fa(); System.out.println("CLASS A :Function fb Stop......"); } }
class B extends A { public B(){ } public void fa() { System.out.println("CLASS B :Function fa Runing......"); }
public void fb() { System.out.println("CLASS B :Function fb Runing......"); super.fb(); System.out.println("CLASS B :Function fb Stop......"); } }
下面是它的运行结果:
CLASS B :Function fb Runing...... CLASS A :Function fb Runing...... CLASS B :Function fa Runing...... CLASS A :Function fb Stop...... CLASS B :Function fb Stop......
怎么P猜对l果了吗Q如果结果跟你想象的一模一P那么恭喜你,你对多态已l有初步了解了,L在语法层ơ上是比较熟悉了。但是,千万不要“z洋得意”Q你可否解析l果Z么会是这样吗Q我们可以先来梳理一下程序流E: 1、运行main函数Q创建B对象(A b = new B(),完全可以替代B b=new B();)Q调用B的方法fbQ于是打印出"CLASS B :Function fb Runing......"Q都在情理之中?br style="line-height: normal" /> 2、执行super.fb()Q调用父cA的方法fbQ首先打印出"CLASS A :Function fb Runing......"Q预料之?br style="line-height: normal" /> 3、执行方法fa()Q打印出"CLASS B :Function fa Runing......"Q呃Q奇怪了Qؓ什么不是执行A的方法fa(),而是子类B中的fa()呢?当前被执行的是类A的方法,那么虚拟机找到的应该是AcȝMethod TableQ找到的应该是AcȝҎfa()啊?难解?br style="line-height: normal" /> 4、打?CLASS A :Function fb Stop......"Q返?br style="line-height: normal" /> 5、打?strong style="line-height: normal">"CLASS A :Function fb Stop....."Q返回,E序l束?br style="line-height: normal" /> 现在问题清楚了,是虚拟机在执行cAҎ的时候查扄Method Table竟然是子cB的。ؓ什么呢Q其实,只要我们清楚javaҎ调用的方式,q个问题p刃而解了。在Java虚拟ZQ每启动一个新的线E,虚拟机都会ؓ它分配一个Java栈,而每当线E调用一个javaҎӞ虚拟机就会在该线E的java栈中压入一个新帧,用以存储参数Q局部变量等数据。我们将q个正在被执行的ҎUCؓ该线E的当前ҎQ其相应的栈帧ؓ当前帧?br style="line-height: normal" /> 好了Q当我们调用一个方法时Q我们需要往当前帧中压入哪些参数呢?单,Ҏ的参数列表中不是都说得清清楚楚的吗?嗯,对于C语言来说Q这个说法是正确的,但是对于诸如C++QJavaQPython{面向对象语a来说Q却是不对的?span style="background-color: yellow">大家q记得那?this"指针吗?Q?/span>不错Q?strong style="line-height: normal">在Java中,所有的实例ҎQInstance MethodQ调用的时候都会把当前对象压入当前帧中QJava虚拟机正是通过q个参数来决定当前所使用的类Q通过判断该对象的cdQ?/strong>?br style="line-height: normal" /> 在上面的例子中,main中调用b.fb()Ӟ压入的当前对象自然是Bcd象,我们Cؓb。在B的fb()中调用super.fb()Ӟ压入的就是刚刚压入的对象Q也是b了。同P在A的fb中调用fa()Ӟ压入的也是b。因此,在?nbsp;invokevirtual指o调用fa()Ӟ扄是B的方法表Q当前对象b的类型ؓBQ,也就执行了类B的fa了?br style="line-height: normal" /> q种现象在构造函C特别常见Q因为构造函C会隐含用调用父cȝ构造函数的Q如果在父类的构造函C调用了实例方法(?nbsp;A的faQ,而在子类中又覆盖了这个实例方法(?nbsp;B的faQ,那么得到的结果往往不是我们所要的。因此,我们最好不要在构造函C使用多态方法,不然QDebug会很痛苦的:Q?/p>
]]>Java中全局变量的实?/title>http://www.shnenglu.com/luyulaile/archive/2010/12/21/137139.htmlluisluisTue, 21 Dec 2010 12:03:00 GMThttp://www.shnenglu.com/luyulaile/archive/2010/12/21/137139.htmlhttp://www.shnenglu.com/luyulaile/comments/137139.htmlhttp://www.shnenglu.com/luyulaile/archive/2010/12/21/137139.html#Feedback0http://www.shnenglu.com/luyulaile/comments/commentRss/137139.htmlhttp://www.shnenglu.com/luyulaile/services/trackbacks/137139.htmlimport java.util.*; import java.math.*; publicclass Negotiation { publicstatic final int NUM_MAXTIME=10; /**//**最大轮?/span>*/ publicstatic final int MAX_TURN=50; publicstatic final int NUM_ISSUE=3; /**//**最大对手数*/ publicstatic final int MAX_PARTNER=10; /**//**谈判q程 * */ void negotiationProcess() { } }
]]>[转蝲]Vector遍历http://www.shnenglu.com/luyulaile/archive/2010/01/26/106444.htmlluisluisTue, 26 Jan 2010 01:08:00 GMThttp://www.shnenglu.com/luyulaile/archive/2010/01/26/106444.htmlhttp://www.shnenglu.com/luyulaile/comments/106444.htmlhttp://www.shnenglu.com/luyulaile/archive/2010/01/26/106444.html#Feedback0http://www.shnenglu.com/luyulaile/comments/commentRss/106444.htmlhttp://www.shnenglu.com/luyulaile/services/trackbacks/106444.htmlimport java.util.Iterator; import java.util.Vector;
public class VectorIterator implements Iterator{
private Vector v;
private int currentIndex=0;
public VectorIterator(){
}
public VectorIterator(Vector v){ this.v=v; }
public boolean hasNext() { if(this.currentIndex<this.v.size()){ System.out.println("current index is : "+this.currentIndex); return true; }else{ System.out.println("out of the bound "); } return false; }
public Object next() { return this.v.get(this.currentIndex++); }
public void remove() { this.v.remove(this.currentIndex); }
public void testMethod(){ v.add("a"); v.add("b"); v.add("c");
System.out.println("\n原来的vecotr"); for(int i=0;i<v.size();i++){ System.out.println("\n"+" index is: "+ i + " element is: " +v.get(i)); }
v.remove(0);
System.out.println("\nL一个vector中第一个元?); for(int i=0;i<v.size();i++){ System.out.println("\n"+" index is: "+ i + " element is: " +v.get(i)); } }
public static void main(String[] args) { test t = new test(); t.testMethod(); System.out.println("Hello World!"); } } -----------------排序E序------------------------------ import java.util.*;
class MyCompare implements Comparator //实现ComparatorQ定义自q比较Ҏ { public int compare(Object o1, Object o2) { Elem e1=(Elem)o1; Elem e2=(Elem)o2;
public class Vector1 { public static void main(String[] args) { List v = new Vector(); v.add(new Elem(1)); v.add(new Elem(22)); v.add(new Elem(3)); v.add(new Elem(14)); Comparator ct = new MyCompare(); Collections.sort(v, ct); for (int i = 0; i < v.size(); i++) System.out.println(((Elem) v.get(i)).get());