学习 java langspec-3.0. 规范后的代码

来源:互联网 发布:java 依赖包 编辑:程序博客网 时间:2024/06/05 03:14

 

package com;

public interface gg {public void ss();}package com;public abstract class tt {public void ss(){}}package com;public class ww extends tt implements gg {}
package com;public class ww {static   ww  w=new  ww();static {System.out.println("初始化");}  public    ww(){System.out.println("构造函数");}}package com.test;import com.ww;public class Client  {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubnew  ww();}}
package com;public class ww {public interface aa {void dd();}}package com.test;import com.ww;public class Client implements ww.aa {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub}public void dd() {// TODO Auto-generated method stub}}
package com;public class ww { publicww(){} protected static  void ss() {   }     interface  aa{void dd();}public static class  ss implements aa{public void dd() {// TODO Auto-generated method stub}}}package com;public class Client {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubww.ss s =new ww.ss();}}

 

 

 

 

package com;public class ww { publicww(){} protected static  void ss() {   }     interface  aa{void dd();}public  class  ss implements aa{public void dd() {// TODO Auto-generated method stub}}}package com;public class Client {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubww w = new ww();ww.ss s = w.new ss();}}

 

 

 

 

 

 

 

 

package com;public class ww {{System.out.println("初始化");}  public    ww(){System.out.println("构造函数");}}package com.test;import com.ww;public class Client  {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubnew  ww();}}

 

 

 

 

package com;public abstract class ww {static {System.out.println("类初始化");}{System.out.println("实例初始化");}public static void ss() {System.out.println("我是一个抽象类的静态方法");}public ww() {System.out.println("构造函数");}}package com.test;import com.ww;public class Client {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubww.ss();}}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

泛型的使用

  1. package com;
  2. public class WW<T> {
  3.     T t;
  4.     public T getT() {
  5.         return t;
  6.     }
  7.     public void setT(T t) {
  8.         this.t = t;
  9.     }
  10.     public T print() {
  11.         System.out.println(t.toString());
  12.         return t;
  13.     }
  14.     public static void main(String[] args) {
  15.         WW<String> w = new WW<String>();
  16.         w.setT("sss");
  17.         w.print();
  18.     }
  19.     
  20. }
  1. package com.org;
  2. public interface gg<T>{
  3.     public
  4.         
  5.   void  ss(T  t);
  6.     
  7. }
  8. package com;
  9. import com.org.gg;
  10. public class WW<T> implements gg<T> {
  11.     public void ss(T t) {
  12.     }
  13.     public static void main(String[] args) {
  14.     }
  15. }

 

 

 

 

 

  1. package com.test2;
  2. public class TT {
  3.     
  4.     public  TT(String  s)
  5.     {
  6.         
  7.         
  8.         System.out.println("super Constructor");
  9.     }
  10.     
  11.     
  12.     
  13. }
  14. package com;
  15. import com.test2.TT;
  16. public class WW extends TT {
  17.     
  18.     
  19.     public WW(String s) {
  20.         super(s);
  21.         // TODO Auto-generated constructor stub
  22.     }
  23.     public static void main(String[] args) {
  24.          new  WW("sss");
  25.     }
  26.     
  27. }

 

 

 

  1. package com.test2;
  2. public class TT {
  3.     
  4.     public   TT()
  5.     {
  6.         System.out.println("super Constructor");
  7.     }
  8.     
  9. }
  10. package com;
  11. import com.test2.TT;
  12. public class WW extends TT {
  13.     public static void main(String[] args) {
  14.         new WW();
  15.     }
  16. }

 

 

 

 

  1. package com.test2;
  2. public class TT {
  3.     static {
  4.         System.out.println("super static Initializer");
  5.     }
  6. }
  7. package com;
  8. import com.test2.TT;
  9. public class WW extends TT {
  10.     public static void main(String[] args) {
  11.         new WW();
  12.     }
  13. }

 

 

  1. package com.test2;
  2. public class tt {
  3.     public void m1() {
  4.         m2();
  5.     }
  6.     public void m2() {
  7.         System.out.println("super m2");
  8.     };
  9. }
  10. package com;
  11. import com.test2.tt;
  12. public class WW extends tt {
  13.     public static void main(String[] args) {
  14.         new WW().m1();
  15.     }
  16.     public void m2() {
  17.         System.out.println("sub m2");
  18.     }
  19. }

 

 

 

 

 

  1. package com.test2;
  2. public abstract class tt {
  3.     {
  4.         
  5.         System.out.println("sss");
  6.         
  7.     }
  8.     public  void  set()
  9.     {
  10.         
  11.         
  12.     }
  13. }
  14. package com;
  15. import com.test2.tt;
  16. public class WW  extends  tt {
  17.     
  18.     public static void main(String[] args) {
  19.         new WW();
  20.     }
  21. }

 

 

 

 

  1. package com;
  2. public class WW {
  3.     int s = gets();
  4.     public int gets() {
  5.         System.out.println("gets");
  6.         return 10;
  7.     }
  8.     public static void main(String[] args) {
  9.         new WW();
  10.     }
  11. }

 

 

 

 

  1. package com.org;
  2. public interface gg {
  3.     public int s = 100000;
  4. }
  5. package com.test2;
  6. public class TT {
  7.     public  String  s="100";
  8.     
  9. }
  10. package com;
  11. import com.org.gg;
  12. import com.test2.TT;
  13. public class WW extends TT implements gg {
  14.     public int s = 10;
  15.     public static void main(String[] args) {
  16.         WW w = new WW();
  17.         TT t = w;
  18.         gg g = w;
  19.         System.out.println(t.s);
  20.         System.out.println(w.s);
  21.         System.out.println(g.s);
  22.     }
  23. }

 

 

下面的程序为什么在编译时报错呢?

 

  1. package com.org;
  2. public interface gg {
  3.     public int s = 100000;
  4. }
  5. package com.test2;
  6. public class TT {
  7.     public  String  s="100";
  8.     
  9. }
  10. package com;
  11. import com.org.gg;
  12. import com.test2.TT;
  13. public class WW extends TT implements gg {
  14.     
  15.     public  void  s()
  16.     {
  17.         
  18.         
  19.         System.out.println(s);
  20.         
  21.     }
  22. }

 

 

 

 

 

 

  1. package com.test2;
  2. public class TT {
  3.     public  int s=100;
  4.     
  5. }
  6. package com;
  7. import com.test2.TT;
  8. public class WW extends TT {
  9.     public int s = 10;
  10.     public static void main(String[] args) {
  11.         TT t = new WW();
  12.         WW w = new WW();
  13.         System.out.println(t.s);
  14.         System.out.println(w.s);
  15.     }
  16. }

 

 

import  static的使用:

 

 

 

  1. package com.org;
  2. public interface gg {
  3.      int s = 100000;
  4. }
  5. package com;
  6. import static com.org.gg.s;
  7. public class WW {
  8.     public void s() {
  9.         System.out.println(s);
  10.     }
  11.     public static void main(String[] args) {
  12.         new WW().s();
  13.     }
  14. }

 

 

 

  1. package com.test2;
  2. public class TT {
  3.     
  4.     
  5.     public void ss(){
  6.         
  7.         System.out.println("super");
  8.     }
  9. }
  10. package com;
  11. import com.test2.TT;
  12. public class WW extends TT  {
  13.     public static void main(String[] args) {
  14.         WW  w=new WW();
  15.         w.ss();
  16.         TT  t=w;
  17.         t.ss();
  18.     }
  19.     public void ss(){
  20.         System.out.println("sub");
  21.         
  22.     }
  23. }

 

 

 

 

 

 

下面的这段代码编译出错

 

  1. package com.test2;
  2. public class TT {
  3.     
  4.     
  5.     public  void ss(){
  6.         
  7.         System.out.println("super");
  8.     }
  9. }
  10. package com;
  11. import com.test2.TT;
  12. public class WW extends TT {
  13.     public static void main(String[] args) {
  14.     }
  15.     protected void ss() {
  16.         System.out.println("sub");
  17.     }
  18. }

 

 

那下面的程序的输出会是什么呢?

 

 

 

 

 

 

 

 

 

 

原创粉丝点击