package Pack1;import java.awt.*;import java.applet.*;class ca { static int n = 20; final int nn; // 声明nn,但没有赋初值 final int k = 40;// 声明k并赋初值40 ca() { nn = ++n; }// 在构造方法中给nn赋值}public class c4_1 extends Applet { public void paint(Graphics g) { ca m1 = new ca(); // 创建对象m1,使其静态数据成员nn的值为21 ca m2 = new ca(); // 创建对象m2,使其静态数据成员nn的值为22 //m1.nn = 90; // 这是一个错误的赋值语句,因为nn是标识符常量 g.drawString("m2.nn=" + m2.nn, 20, 30); g.drawString("m2.k=" + m2.k, 20, 50); g.drawString("m1.nn=" + m1.nn, 20, 70); g.drawString("m1.k=" + m1.k, 20, 90); }}
package Pack1;public class c4_2 { public static void main(String[] args) { // TODO Auto-generated method stub int a = 2, b = 3, z1, z2; a1 p1 = new a1(); z1 = p1.add(a, b); // 子类可以引用父类的final方法 z2 = p1.mul(a, b); System.out.println("z1=" + z1); System.out.println("z2=" + z2); }}class a1{ final int add(int x,int y){ return(x+y); } int mul(int a, int b) { int z = 0; z = add(1, 7) + a * b; return z; }}