package com.tarena.day10; public class FinalClassDemo { } final class Hoo{ } //class Joo extends Hoo{} //错误,final类不能被继承 //String Integer Math package com.tarena.day10; public class FinalMethodDemo { public final int add(int a) { return a += 1; } public static void main(String[] args) { } } class Moo extends FinalMethodDemo{ /* public int add(int a) {//错,final的方法不能被覆盖 // TODO Auto-generated method stub return 2; } */ } package com.tarena.day10; public class FinalValDemo { /** * @param args */ static int index; final int id;//只能初始化不能再修改。 public FinalValDemo(){ id =index++;//初始化id,不能在修改 } public static void main(String[] args) { final int a = 5; // a++; //错误,a的值不能再改变 System.out.println(add(a)); FinalValDemo obj = new FinalValDemo(); // obj.id = 5;//错误,不能再修改final 变量 FinalValDemo obj2 = new FinalValDemo(); System.out.println(obj2.id); } public static int add(final int a) { // return a+=1; //错误,a的值不能再修改。 return a+1; } } package com.tarena.day10; public class StaticDemo2 { int a = 1; public static void main(String[] args) { // System.out.println(a);//错误 // System.out.println(test());//错误 //没有对象就没有a,执行静态方法时还没有对象。 StaticDemo2 demo = new StaticDemo2(); System.out.println(demo.a); //静态方法只能直接访问静态资源(属性和方法) } int test(){ return 3; } } package com.tarena.day10; public class StaticDemo { public static void main(String[] args) { Foo.b = 3; Foo f1 = new Foo(1); Foo f2 = new Foo(2); System.out.println(f1.a+" "+f1.b);//1,3 System.out.println(f2.a+" "+f2.b);//2,3 System.out.println(Foo.b);//3 Koo k1 = new Koo(); Koo k2 = new Koo(); Koo k3 = new Koo(); System.out.println(k3.id+" "+Koo.index); } } class Koo{ int id; static int index = 0; public Koo(){ id = index++; } } class Foo{ public int a; public static int b = 2; public Foo(int a) { this.a = a; } } package com.tarena.day10; public class StaticSatementDemo { public static void main(String[] args) { System.out.println("声明g"); Goo g; System.out.println("访问g的静态属性"); Goo.a = 3; System.out.println("创建g的实例"); g = new Goo(); } } class Goo{ public static int a; public Goo() { System.out.println("构造方法"); } static {//静态代码块,在类的加载期间执行,只执行一遍 System.out.println("Goo 来也!"); } } package com.tarena.day10; public class ToStringDemo { /** * @param args */ public static void main(String[] args) { Yoo yoo = new Yoo(); Yoo yoo1 = new Yoo(); Yoo y = yoo1; System.out.println(yoo); System.out.println(yoo1); System.out.println(y); } } class Yoo extends Object{ public String toString() { return "我是Yoo"; } }