java code public class Test { public Test(String iceboy) { System.out.PRintln(iceboy); } public void Test(String iceboy) { System.out.println(iceboy); } public static void main(String[] args) { Test a = new Test("abc");//输出“abc” a.Test("iceboy");//输出“iceboy” } }
Java code public class Test { public static void main(String[] args) { ThreadClass t = new ThreadClass(); t.start(); System.out.println("end");//输出“end” } } class ThreadClass extends Thread //Thread类已经实现了空的run()方法。 { } 第二种方法:实现Runnable接口 public class Test { public static void main(String[] args) { ThreadClass t = new ThreadClass(); Thread thread = new Thread(t); thread.start(); System.out.println("end"); } } class ThreadClass implements Runnable { public void run() //必须有此方法否则编译报错。它是Runnable接口中的抽象方法。 { System.out.println("Threads"); } } 4.局部内部类是否可以访问非final变量? 答案:不能访问局部的,可以访问成员变量(全局的)。 class Out { private String name = "out.name"; void print() { final String work = "out.local.work";//若不是final的则不能被Animal 使用. int age=10; class Animal //定义一个局部内部类.只能在print()方法中使用. //局部类中不能使用外部的非final的局部变量.全局的可以. { public void eat() { System.out.println(work);//ok //age=20;error not final System.out.println(name);//ok. } } Animal local = new Animal(); local.eat(); } }
public class TestThreads { private int j=1; //加线程 private class Inc implements Runnable { public void run() { for(int i = 0;i < 10;i++) { inc(); } } } //减线程 private class Dec implements Runnable { public void run() { for(int i = 0;i < 10;i++) { dec(); } } } //加1 private synchronized void inc() { j++; System.out.println(Thread.currentThread().getName()+"-inc:"+j); } //减1 private synchronized void dec() { j--; System.out.println(Thread.currentThread().getName()+"-dec:"+j); } //测试程序 public static void main(String[] args) { TestThreads test = new TestThreads(); //创建两个线程类 Thread thread = null; Inc inc = test.new Inc(); Dec dec = test.new Dec(); //启动4个线程 for(int i = 0;i < 2;i++) { thread = new Thread(inc); thread.start(); thread = new Thread(dec); thread.start(); } } }
25.数组转换问题。 Object[] object = new Person[2]; Person [] person = new Person [3]; person = (Person [])object;//可以转换 int[] i = new int[2]; long[] l = new int[3]; i = (long[])l;//不可以转换 26.用socket通讯写出客户端和服务器端的通讯,要求客户发送数据后能够回显相同的数据。 Server.java:源代码
Java code import java.net.*; import java.io.*;
class Server { public Server() { BufferedReader br = null; PrintWriter pw = null; try { ServerSocket server = new ServerSocket(8888);//建立服务器端 Socket socket = server.accept();//监听客户端 //得到该连接的输入流 br = new BufferedReader(new InputStreamReader(socket.getInputStream())); //得到该连接的输出流 pw = new PrintWriter(socket.getOutputStream(),true); //先读后写 String data = br.readLine(); System.out.println(data);//输出到控制台 pw.println(data);//转发给客户端 }catch(Exception e) { e.printStackTrace(); } finally { try { //关闭读写流 br.close(); pw.close(); }catch(Exception e) {} } } public static void main(String[] args) { Server server = new Server(); } }
Client.java:源代码
Java code
import java.net.*; import java.io.*; class Client { public Client() { BufferedReader br = null; PrintWriter pw = null; try { Socket socket = new Socket("localhost",8888);//与服务器建立连接,服务器要先启 //得到Socket的输入与输出流 br = new BufferedReader(new InputStreamReader(socket.getInputStream())); pw = new PrintWriter(socket.getOutputStream(),true); //先写后读 pw.println("Client:你好!"); String data = null; while(true) { data = br.readLine(); if(data!=null) break; } System.out.println(data); }catch(Exception e) { e.printStackTrace(); } finally { try { br.close(); pw.close(); }catch(Exception e) {} } } public static void main(String[] args) { Client c = new Client(); } }