昨天斌哥讲了java语法中的一些关键字,相当一部分的关键字我们已经掌握了,昨天他给我们重点讲了final和static 关键字。
final关键字意思是最终。也就是不可修改的。用在类前面就是不可继承的最终类。如
public final 类名 class(){}
用在方法面前就是子类不能再改写的方法。如 public final 函数名(参数){}
用在基本数据类型就是不可以再更改。如 final int a =10 ;
final可以用于保护数据不被修改。
static 关键字意思是静态的。
在类的public方法前面加上static可以在类的外部无需创建对象即可调用该函数。
在类的基本属性前面加上static 可以使该变量变成公共变量,所有的对象都可以操作该变量,并且该变量值以最后一次修改为准。
如:
public class Test { public static void main(String[] args) { Student stu1 = new Student(); stu1.setName("张三"); Student stu2 = new Student(); stu2.setName("李四"); stu1.study(); stu2.study(); } }输出结果为:李四正在学习
static 也可以在类的里加上static{} ,表示静态块。
如:
public class Test { static{ System.out.println("static模块执行"); } public static void main(String[] args) { System.out.println("main函数执行"); } }结果是: static模块执行
有static关键字的方法和属性都在其他方法属性运行前运行,所以有时候static要调用其他普通方法属性时会报错。
如:
public class Test { public static void main(String[] args) { student(); } public void student(){ } }
不能再static方法里调用不是static的方法
昨天还学了随机数生成:Random rand = new Random();
rand.nextInt(x);
可以生成0到x-1之间的数字.
我做了个抽奖系统,用集合的HashSet容器存放避免抽奖重复。
public class ChouJiang extends JFrame{ <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>JLabel label1 = new JLabel("人数"); <span style="white-space:pre"> </span>JLabel label2 = new JLabel("获奖人数"); <span style="white-space:pre"> </span>JTextField s1 = new JTextField(8); <span style="white-space:pre"> </span>JTextField s2 = new JTextField(8); <span style="white-space:pre"> </span>JButton jb = new JButton("抽奖"); <span style="white-space:pre"> </span>JTextArea area = new JTextArea(10,30); <span style="white-space:pre"> </span>JScrollPane hk = new JScrollPane(area); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>public static void main(String[] args) { <span style="white-space:pre"> </span>ChouJiang test = new ChouJiang(); <span style="white-space:pre"> </span>test.initFrame(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void initFrame(){ <span style="white-space:pre"> </span>setTitle("抽奖"); <span style="white-space:pre"> </span>setSize(400, 400); <span style="white-space:pre"> </span>setLocationRelativeTo(null); <span style="white-space:pre"> </span>setDefaultCloseOperation(3); <span style="white-space:pre"> </span>setLayout(new FlowLayout()); <span style="white-space:pre"> </span>this.add(label1); <span style="white-space:pre"> </span>this.add(s1); <span style="white-space:pre"> </span>this.add(label2); <span style="white-space:pre"> </span>this.add(s2); <span style="white-space:pre"> </span>this.add(jb); <span style="white-space:pre"> </span>this.add(hk); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>setVisible(true); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>jb.addActionListener(new ActionListener(){ <span style="white-space:pre"> </span>public void actionPerformed(ActionEvent arg0) { <span style="white-space:pre"> </span>int num = Integer.parseInt(s1.getText()); <span style="white-space:pre"> </span>int count = Integer.parseInt(s2.getText()); <span style="white-space:pre"> </span>Set set = new HashSet(); <span style="white-space:pre"> </span>for( ; count != set.size(); ){ <span style="white-space:pre"> </span>int j = suiji(num); <span style="white-space:pre"> </span>j++ ; <span style="white-space:pre"> </span>set.add(j); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>Iterator it = set.iterator(); <span style="white-space:pre"> </span>int k =0 ; <span style="white-space:pre"> </span>while(it.hasNext()){ <span style="white-space:pre"> </span>area.append(it.next().toString()+" "); <span style="white-space:pre"> </span>k++; <span style="white-space:pre"> </span>if(k==5){ <span style="white-space:pre"> </span>area.append("\n"); <span style="white-space:pre"> </span>k = 0; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public int suiji(int x){ <span style="white-space:pre"> </span>Random rand = new Random(); <span style="white-space:pre"> </span>return rand.nextInt(x); <span style="white-space:pre"> </span>} }