java考前一天

来源:互联网 发布:win10软件不兼容 编辑:程序博客网 时间:2024/06/05 20:47

根据我们的复习资料写的


单体程序设计


public class Singleton {int data = 0;private static Singleton single = null;private Singleton() {}; //类的构造方法不能具有静态属性public static synchronized Singleton getInstance() {if (single == null) {single = new Singleton();}return single;}}


ImageIO读写操作


import java.awt.image.BufferedImage;import java.io.File;import java.net.URL;import javax.imageio.ImageIO;public class Demo {public static void main(String[] args) {try {File file = new File("src/img/music.png");//file.createNewFile(); //若原有文件存在,该方法不起作用URL url = new URL("https://github.com/zzuwenjie/coding-17/"+ "blob/master/pictures/music.png?raw=true");BufferedImage image = ImageIO.read(url);ImageIO.write(image, "png", file);}catch (Exception e) {System.err.println("error");}}}


匿名内部类


class B {      public void method() {          System.out.println("调用B的方法");      }  }    public class A {      public static void main(String[] args) {          B obj = new B() {              public void method() {                  System.out.println("调用B的匿名内部类的方法");              }          };          obj.method(); //输出:调用B的匿名内部类的方法          //System.out.println("主方法调用结束");      }  }  


多线程


public class MyThread implements Runnable {      private int threadID;      private static int data;      public MyThread(int id) {          threadID = id;      }      @Override      public void run() {           // TODO Auto-generated method stub          try {              while (true) {                  synchronized( Class.forName("MyThread") ) {                      if (threadID % 2 == 0) {                          System.out.println("线程1:\t" + --data);                          Thread.sleep(300); //终端输出较慢,所以要给其足够时间输出信息                      }                      else {                          System.out.print("线程2:\t" + data++); //对data加1之前                          System.out.println("\t" + data);                          Thread.sleep(300);                      }                                     }              }          }          catch (Exception e) {              System.err.println("error");          };      }            public static void main(String[] args) {          Thread t1 = new Thread(new MyThread(1));          Thread t2 = new Thread(new MyThread(2));          t1.start();          t2.start();      }        }  


动态多态性(覆盖override)


//动态多态性应用  abstract interface Tuxing {      public double area();  }    class Triangle implements Tuxing {      double a, b, c;            public Triangle(double aa, double bb, double cc) {          a = aa;          b = bb;          c = cc;      }            @Override      public double area() {          // TODO Auto-generated method stub          double p = (a + b + c) / 2.0;          return Math.sqrt(p * (p - a) * (p - b) * (p - c));      }           }    class Rectangle implements Tuxing {      double width, height;            public Rectangle (double w, double h) {          width = w;          height = h;      }               @Override      public double area() {          // TODO Auto-generated method stub          return width * height;      }        }    public class Polymorphism {      public static void main(String[] args) {          Tuxing g3 = new Triangle(3, 4, 5);          Tuxing g4 = new Rectangle(3, 3);          System.out.println(g3.area());            System.out.print(g4.area());      }     }  


TCP

import java.io.DataOutputStream;import java.net.ServerSocket;import java.net.Socket;public class MyServer {private static long cnt = 0;@SuppressWarnings("resource")public static void main(String[] args) {try {ServerSocket sever = null;sever = new ServerSocket(5000);//注册监听的端口while(true) {System.out.println("server wait. connecting...");Socket socket = sever.accept();//返回一个套接字System.out.println(cnt++ + "\t" + socket.getRemoteSocketAddress());DataOutputStream dataOut = new DataOutputStream(socket.getOutputStream());dataOut.writeUTF("服务器向客服端问好! hello");dataOut.close();socket.close();//接口extends接口,类extends类//implements用于实现}}catch (Exception e) {System.out.println("error");}}}


import java.io.DataInputStream;import java.net.Socket;public class MyClient {public static void main(String[] args) {// TODO Auto-generated method stubSocket socket = null;int port = 5000;try {if (args.length >= 1) {if (args.length >= 2) {try {port = Integer.parseInt(args[1]);}catch (Exception e) {;}}socket = new Socket(args[0], port);}else {socket = new Socket("localhost", port);//socket = new Socket("115.159.225.36", port);//socket = new Socket("10.108.213.181", port);}DataInputStream dataIn = new DataInputStream(socket.getInputStream());System.out.println("received: " + dataIn.readUTF());socket.close();}catch (Exception e) {System.out.println("an error has happened");}}}


InetAddress

import java.net.InetAddress;public class MyInetAddress {public static void main(String[] args) {String addrStr = "wenjie.club";InetAddress ipAddr = null;try {ipAddr = InetAddress.getByName(addrStr);System.out.println("host Addresss: " + ipAddr.getHostAddress());System.out.println("host Name: " + ipAddr.getHostName());}catch (Exception e) {System.out.println("error");}}}



第八章图形操作

import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPasswordField;import javax.swing.JTextField;public class Eight {public static void main(String[] args) {JFrame app = new JFrame("JFrame");app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);app.setLayout(new FlowLayout());int width = 800, height = 600;JTextField field = new JTextField("please input", 30);field.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {//System.out.println(e.getActionCommand());}});JTextField passwdField = new JPasswordField("password", 30);passwdField.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {System.out.println(e.getActionCommand());}});JButton btn = new JButton("yes");btn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {//System.out.println(e.getActionCommand());}});app.add(field);app.add(passwdField);app.add(btn);app.setSize(width, height);app.setVisible(true);}}