java代码大全及详解教程,JAVA简单的代码求解

1,JAVA简单的代码求解首先得弄明白java对象初始化及创建过程,类加载完成后首先初始化的是静态成员以及静态代码块(如果有父类,则先初始化父类 的) , 所以static int x2 = prt("Beetle.x2")在类加载完成之后会首先执行,而后从程序入口main方法(也是静态方法)执行prt("Beetle constructor"); 然后执行到Beetle b = new Beetle(;时开始创建对象,在创建这个对象的时候首先会在堆上为这个Beetle对象分配足够的存储空间,同时初始化其非静态成员(字段) , 再调用构造方法生成对象,即执行int k = prt("Beetle.k");然后调用构造方法Beetle(),完成对象的创建 。static 会在构造方法调用前实例化
2 , 急简单JAVA编程代码public class Testpublic static void main(String[] args)MyRectangle rec = new MyRectangle(3, 5);MyRectangle square = new MySquare(4);System.out.println(rec.toString()); System.out.println(square.toString()); }}class MyRectangle protected double width; protected double length;public MyRectangle(double length, double width) this.width = width; this.length = length; } public double getLength()return length; } public double getWidth()return width; } public void setWidth(double width)this.width = width; }public double getArea() return this.width * this.length; }public String toString() return "长方形的长为:" + length + ", 宽: " + width + ", 面积为:" + getArea(); }}class MySquare extends MyRectangle public MySquare(double length) super(length, length); }public double getArea() return Math.pow(super.width, 2); } public String toString() return "正方形边长为: " + super.length + ", 面积为: " + getArea(); }}----------测试长方形的长为:3.0, 宽: 5.0, 面积为:15.0正方形边长为: 4.0, 面积为: 16.0
3,JAVA程序讲解 public void actionPerformed(ActionEvent e){if(e.getSource()==删除||e.getSource()==工号){String number="";number=工号.getText();if(number.length()>0){try {inOne=new FileInputStream(file);inTwo=new ObjectInputStream(inOne);基本信息表=(Hashtable)inTwo.readObject();inOne.close();inTwo.close();}catch(Exception ee){}if(基本信息表.containsKey(number)){Employee emp=(Employee)基本信息表.get(number);姓名.setText(emp.getName());部门.setText(emp.getdept());工龄.setText(emp.getworktime());工资.setText(emp.getworkdate());if(emp.getSex().equals("男")){男.setSelected(true);}else{女.setSelected(true);【java代码大全及详解教程,JAVA简单的代码求解】
4,java原代码注释 越详细越好public class AddBook extends JFrame {//创建一个窗体类JPanel contentPane;//创建一个面板引用JLabel jLabel1 = new JLabel();//实例化一个标签JLabel lblBookISBN = new JLabel();//JLabel lblBookName = new JLabel();//JLabel lblBookAuthor = new JLabel();//JLabel lblBookType = new JLabel();//JLabel lblBookState = new JLabel();//public JTextField txtBookISBN = new JTextField();//实例化一个文本框JTextField txtBookName = new JTextField();//JTextField txtBookAuthor = new JTextField();//JTextField txtBookType = new JTextField();//JTextField txtBookState = new JTextField();//JButton btnOK = new JButton();//实例化一个按钮JButton btnCancel = new JButton();//public AddBook() {try {setDefaultCloseOperation(EXIT_ON_CLOSE);//设置关闭窗体时进行的操作jbInit();//调用初始化方法} catch (Exception exception) {exception.printStackTrace();//打出异常堆栈轨迹}} 上面是一堆控件定义 。JLabel的 是标签 , JTextField 是文本框,JButton 是按钮 。// 异常处理try {// 设定窗口关闭时退出程序setDefaultCloseOperation(EXIT_ON_CLOSE);// 调用jbinit()方法jbInit();} catch (Exception exception) {// 异常信息输出exception.printStackTrace();}5,java线程的经典代码package threadgroup;class ThreadDemo3 extends Threadprivate String name; private int delay; public ThreadDemo3(String sname, int i_delay)name = sname; delay = i_delay; } public void run()trysleep(delay); } catch (InterruptedException e)} System.out.println("多线程测试!\n" + name + "\n" + delay); } } public class testMyThreadpublic static void main(String[] args)ThreadDemo3 th1,th2,th3; th1 = new ThreadDemo3("线程1", (int) (Math.random() * 900)); th2 = new ThreadDemo3("线程2", (int) (Math.random() * 900)); th3 = new ThreadDemo3("线程3", (int) (Math.random() * 900)); th1.start(); th2.start(); th3.start(); } } package threadgroup;public class threadDemopublic static void main(String[] args)Thread t = Thread.currentThread(); t.setName("你好吗?"); System.out.println("正在进行的Thread是:" + t); tryfor (int i = 0; i < 5; i++)System.out.println("我不叫穆继超" + i); Thread.sleep(3000); } } catch (Exception e)// TODO: handle exception System.out.println("Thread has wrong" + e.getMessage()); } }}package threadgroup;public class threadDemo2 implements Runnablepublic threadDemo2()Thread t1 = Thread.currentThread(); t1.setName("第一个主进程"); System.out.println("正在运行" + t1); Thread t2 = new Thread(this, ""); System.out.println("在创建一个进程"); t2.start(); trySystem.out.println("使他进入第一个睡眠状态"); Thread.sleep(2000); } catch (InterruptedException e)System.out.println("Thread has wrong" + e.getMessage()); } System.out.println("退出第一个进程"); } public void run()tryfor (int i = 0; i < 5; i++)System.out.println("进程" + i); Thread.sleep(3000); } } catch (InterruptedException e)// TODO: handle exception System.out.println("Thread has wrong" + e.getMessage()); } System.out.println("退出第二个进程"); } public static void main(String[] args)new threadDemo2(); }}

    推荐阅读