1:单例模式
public class Singleton {
private Singleton(){}
private static Singleton instance = new Singleton();
public static Singleton getInstance(){
return instance;
}
}
2:内部类(当成方法分析)
非静态内部类不能有静态成员,静态内部类里可以有静态成员(这点不同于静态方法)。
静态内部类的非静态成员(或静态成员)可以方位外部类的静态变量,而不能访问非静态变量。
非静态内部类的非静态成员可以访问外部类的非静态成员
3:socket编程
server.java
package day3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private ServerSocket ss;
private Socket soc;
private BufferedReader bfr;
private PrintWriter pw;
public Server(){
try {
ss = new ServerSocket(9999);
System.out.println("server start");
while(true){
soc = ss.accept();
bfr = new BufferedReader(new InputStreamReader(soc.getInputStream())) ;
String message = bfr.readLine();
String address = soc.getInetAddress().getHostAddress();
int port = soc.getLocalPort();
System.out.println("a client from" + address + ", port is: " + port + "message is :"
+ message);
pw = new PrintWriter(soc.getOutputStream(),true);
pw.println("from server:your message has been receiver");//而非out.write(),否则发不出去
pw.close();
bfr.close();
soc.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server();
}
}
client.java:
package day3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
private Socket soc;
private BufferedReader bfr;
private PrintWriter pw;
public Client(){
try {
soc = new Socket("127.0.0.1", 9999);
BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入要发送给服务器的信息");
String message = line.readLine();
pw = new PrintWriter(soc.getOutputStream(),true);
System.out.println("你输入的信息时:" + message);
pw.println(message);//而非out.write(),否则发不出去
bfr = new BufferedReader( new InputStreamReader(soc.getInputStream()));
System.out.println(bfr.readLine());
pw.close();
bfr.close();
soc.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Client();
}
}
4:截取字符串
package day3;
import java.io.UnsupportedEncodingException;
public class CutString {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(cutString("中bcd国大g",8));
//System.out.println(""+isChinese('中'));
String str = "中bcd国";
System.out.println("length is: " + str.length() + "," +str.getBytes().length);
}
public static String cutString(String str,int count){
StringBuffer sb = new StringBuffer();
int i =0;
int bytecount = 0;
char c;
while(bytecount<count){
c = str.charAt(i);
if(isChinese(c)){
bytecount += 2;
}else{
bytecount++;
}
i++;
sb.append(c);
if(bytecount>count)
sb.deleteCharAt(sb.length()-1);
}
return sb.toString();
}
public static boolean isChinese(char c){
boolean flag = false;
try {
flag = String.valueOf(c).getBytes("gbk").length>1;//判断一个字符是中文字符,还是英文字符
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
}
5:四个线程操纵一个数
package day3;
public class ThreadTest {
private int j;
public synchronized void inc(){
j++;
System.out.println("in inc() ,j is: " + j );
}
public synchronized void des(){
j--;
System.out.println("in des(),j is: " + j);
}
public static void main(String[] args) {
ThreadTest tt = new ThreadTest();
Inc inc = tt.new Inc();
Des des = tt.new Des();
for (int i = 0; i < 2; i++) {
Thread t = new Thread(inc);
t.start();
t = new Thread(des);
t.start();
}
}
class Inc implements Runnable{
@Override
public void run() {
for(int i = 0;i<50;i++){
inc();
}
}
}
class Des implements Runnable{
@Override
public void run() {
for(int i = 0;i<50;i++){
des();
}
}
}
}