java代码

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

初始化

class Tag {

Tag(int marker) {

System.out.println("Tag(" + marker + ")");

}

}

class Card {//无论属性写在类的哪个部分,都首先初始化属性 Tag t1 = new Tag(1); // Before constructor

Card() {

System.out.println("Card()");

t3 = new Tag(33); // Re-initialize t3

}

Tag t2 = new Tag(2); // After constructor

void f() {

System.out.println("f()");

}

Tag t3 = new Tag(3); // At end

}

public class Init {

public static void main(String[] args) {

Card t = new Card();

t.f(); // Shows that construction is done

}

}

结果:Tag(1)

Tag(2)

Tag(3)

Card()

Tag(33)

f()

静态属性初始化

//先初始化静态属性,且只初始化一次

class Bowl {

Bowl(int marker) {

System.out.println("Bowl(" + marker + ")");

}

void f(int marker) {

System.out.println("f(" + marker + ")");

}

}

class Table {

static Bowl b1 = new Bowl(1);

Table() {

System.out.println("Table()");

b2.f(1);

}

void f2(int marker) {

System.out.println("f2(" + marker + ")");

}

static Bowl b2 = new Bowl(2);

}

public class SecondInit {

Table t4 = new Table();

static Table t2 = new Table();

static Table t1 = new Table();

public static void main(String[] args) {

SecondInit si = new SecondInit();

}

}

结果:Bowl(1)

Bowl(2)

Table()

f(1)

Table()

f(1)

Table()

f(1)

IO嵌套写法:

import java.io.*;

public class BufferedStream{

public static void main(String args[]){

try{

File inFile=new File("file1.txt");

File outFile=new File("file2.txt");

FileInputStream fis=new FileInputStream(inFile);

BufferedInputStream bis = new BufferedInputStream(fis);

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFile),20);

int c;

int i=1;

while((c=bis.read())!=-1){

bos.write(c);

System.in.read();

System.out.println("写入Buffer第"+i++);

}

bis.close(); bos.close();

}catch(FileNotFoundException e) {

System.out.println("FileStreamsTest: "+e);

}catch(IOException e) {

System.err.println("FileStreamsTest: "+e);

}

}

}

FileOutputStream fos=new FileOutputStream("file2.txt");

BufferedOutputStream bos = new BufferedOutputStream(fos);

DataOutputStream dos=new DataOutputStream (bos); //嵌套写法

import java.io.*;

import .*;

import java.util.*;

public class NetServer1

{

public static final int PORT =8080;

public static void main(String[] args) throws IOException

{

InetAddress addr = InetAddress.getByName("localhost");

ServerSocket s =new ServerSocket(PORT,10,addr);

System.out.println("虚拟Web服务器启动: "+s);

try{

Socket socket =s.accept();

try{

System.out.println("接受客户端连接请求: "+socket);

BufferedReader in = new BufferedReader(

new InputStreamReader(

socket.getInputStream())) ;

PrintWriter out = new PrintWriter(

new BufferedWriter(

new OutputStreamWriter(

socket.getOutputStream())),true);

String str = in.readLine();

byte[] input = new byte[20];

int i = 0;

System.out.println("收到"+(++i)+"条: "+str);

while(str!=null&&!str.equals("END")){

System.in.read(input);

str = in.readLine();

System.out.println("收到"+(++i)+"条: "+str);

}

out.println("客户端传送信息服务器已经接收完毕");

System.out.println("此次服务完毕,开始下轮监听");

相关文档
最新文档