Java第四节课总结

动手动脑1:如果类提供了一个自定义的构造方法,将导致系统不再提供默认构造方法。Foo obj1=new Foo()在此处调用应增加参数。

动手动脑2:静态初始化块只执行一次。创建子类型的对象时,也会导致父类型的静态初始化块的执行。

Integer的“诡异”特性”:

 Integer类只对-128~127之间的对象做了缓存,(Integer)100 == (Integer)100两边装箱后,实际指向堆内存中同一个对象,(Integer)129 == (Integer)129,装箱为引用类型后,没有做缓存,指向堆内存中不同对象,所以比较结果为false。                                                                                                  

课堂练习: 新课程添加()        

package denglujiemian;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
public class classinformation extends JFrame implements ActionListener {
 private static final long serialVersionUID = 1L;
 public static String sos[][] = new String[1000][3];
 private JLabel address=new JLabel("上课地点");
 private JButton ADD=new JButton("添加");
 private JLabel CNAME=new JLabel("课程名称");
 private JLabel TEA=new JLabel("课程教师");
 private JTextField jtxtName =new JTextField(); 
 private JTextField jtxt =new JTextField();
 private JTextField jtxttea= new JTextField(); 
 private JPanel jp=new JPanel();//面板
 private int f2=0;
 private static int sum=0;
 public void name() {
  CNAME.setBounds(20, 10, 80, 26);
  jp.add(CNAME);
 }
 public void teacher() {
  TEA.setBounds(20, 60, 80, 26);
  jp.add(TEA);
 }
 public void add() {
  ADD.setBounds(60, 180, 200,26); 
  jp.add(ADD);
  ADD.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    int f=0;
    int f5=0;
    char ch [] = new char[2];
    String n=jtxtName.getText();String p=jtxttea.getText();String d=jtxt.getText();String t = "";
    if(d.length()>=2) {
     ch[0]=d.charAt(0);
     ch[1]=d.charAt(1);
     t = new String (ch);
    }
    for(int i=0;i<sum;i++) {
     if(n.equals(sos[i][0])) {
      f5=1;
      JOptionPane.showMessageDialog(null,"课程重复");
     }
    }
    if(f5==1) {}
    else {
     f++;
    }
    if(p.equals("王建民")||p.equals("刘立嘉")||p.equals("刘丹")||p.equals("王辉")||p.equals("杨子光")) {
     f++;
    }
    else {
     JOptionPane.showMessageDialog(null,"没有这个教师");
    }
    if(t.equals("基教")||t.equals("一教")||t.equals("二教")||t.equals("三教")) {
     f++;
    }
    else {
     JOptionPane.showMessageDialog(null,"地点错误");
    }
    if(f==3) {
     sos[sum][0]=n;
     sos[sum][1]=p;
     sos[sum][2]=d;
     sum++;
     try {
      writeFile ();
     } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
     }
     JOptionPane.showMessageDialog(null,"添加成功");
    }
   }
   
  });
 }
 public void RegistrationE() {
  ADD.setVisible(false);
  this.setBounds(1000, 200, 300, 250);//设置界面出现在屏幕上的位置 
 }
 public classinformation(){  
  jp.setLayout(null); //满换行  
  jp.setBackground(Color.ORANGE);
  address.setBounds(20, 110, 80, 26);//设置文字30据左,120上下    
  jp.add(address);//显示    
  teacher();name();  
  add();  
  jtxtName.setBounds(80,10,180,30);//设置文本框80左右10上下(位置)180长30宽 
  jp.add(jtxtName); 
  jtxtName.addActionListener(this);    
  jtxt.setBounds(80,110,180,30); 
  jp.add(jtxt); 
  jtxt.addActionListener(this);    
  jtxttea.setBounds(80,60,180,30); 
  jp.add(jtxttea); 
  
  jtxttea.addActionListener(this);  
   
  this.add(jp); 
  this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);  
  this.setResizable(false); 
  this.setBounds(800, 200, 300, 250);//设置界面出现在屏幕上的位置 
  this.setVisible(true); 
 } 
 
 public void Empty() {
  jtxt.setText("");  
  jtxtName.setText(""); 
  jtxttea.setText("");
 }
 public void actionPerformed(ActionEvent e){ 
  
  if(e.getSource()==jtxtName){ 
   jtxttea.requestFocus(); 
   } 
 } 
 public static void writeFile () throws IOException {
        FileWriter fw_o=new FileWriter("C:\\Users\\26624\\Desktop\\classinformation.txt");
        BufferedWriter bw=new BufferedWriter(fw_o);
        String s=new String();
        int i=0;
        for(i=0;i<sum;i++) {
         System.out.println((i+1)+": "+sos[i][0]+"  "+sos[i][1]+"  "+sos[i][2]);
            bw.write((i+1)+": "+sos[i][0]+"  "+sos[i][1]+"  "+sos[i][2]);
            bw.newLine();
        }
        bw.close();
    }
 public static void main(String[] args) {
  classinformation l1=new classinformation();
  l1.setTitle("课程添加"); 
  
  }
 }

猜你喜欢

转载自www.cnblogs.com/vvxvv/p/11701421.html
今日推荐