Java基础3

文件结构

泛型与异常

泛型

异常

Class7b / com.test1 / Test.java

  1. /*  
  2.  * 功能:泛型的必要性  
  3.  */  
  4.   
  5. package com.test1;   
  6.   
  7. import java.util.ArrayList;   
  8.   
  9. public class Test {   
  10.   
  11.     /**  
  12.      * @param args  
  13.      */  
  14.     public static void main(String[] args) {   
  15.         //泛型   
  16.         //System.out.println("a="+(1>>>2));   
  17.         ArrayList<Dog> al=new ArrayList<Dog>();//将ArrayList转成对Dog的泛型   
  18.         //创建一只狗   
  19.         Dog dog1=new Dog();   
  20.         //放入到集合   
  21.         al.add(dog1);   
  22.         //取出   
  23.         //Dog temp=(Dog)al.get(0);   
  24.         //Cat temp2=(Cat)al.get(0);//类型转换错误   
  25.         Dog temp=al.get(0);//因为前定义了ArrayList对Dog泛型,所以不报错   
  26.     }   
  27.   
  28. }   
  29.   
  30. class Dog   
  31. {   
  32.     private String name;   
  33.     private int age;   
  34.     public String getName() {   
  35.         return name;   
  36.     }   
  37.     public void setName(String name) {   
  38.         this.name = name;   
  39.     }   
  40.     public int getAge() {   
  41.         return age;   
  42.     }   
  43.     public void setAge(int age) {   
  44.         this.age = age;   
  45.     }   
  46.        
  47. }   
  48.   
  49. class Cat   
  50. {   
  51.     private String color;   
  52.     private int age;   
  53.     public String getColor() {   
  54.         return color;   
  55.     }   
  56.     public void setColor(String color) {   
  57.         this.color = color;   
  58.     }   
  59.     public int getAge() {   
  60.         return age;   
  61.     }   
  62.     public void setAge(int age) {   
  63.         this.age = age;   
  64.     }   

Class7b / com.test1 / Test2.java

  1. /*  
  2.  * java的反射机制,泛型的经典应用  
  3.  */  
  4.   
  5. package com.test1;   
  6.   
  7. import java.lang.reflect.Method;   
  8.   
  9. public class Test2 {   
  10.   
  11.     /**  
  12.      * @param args  
  13.      */  
  14.     public static void main(String[] args) {   
  15.         //    
  16.         Gen<String> gen1=new Gen<String>("aaa");   
  17.         gen1.showTypeName(gen1);   
  18.         Gen<Integer> gen2=new Gen<Integer>(1);   
  19.         gen2.showTypeName(gen2);   
  20.            
  21.         Gen<Bird> gen3=new Gen<Bird>(new Bird());   
  22.         gen3.showTypeName(gen3);   
  23.     }   
  24.   
  25. }   
  26.   
  27. //定义一个类   
  28. class Gen<T> //定义的一种未知类型T,即泛型   
  29. {   
  30.     private T o;//用泛型定义一个变量(或对象)   
  31.     //构造函数   
  32.     public Gen(T a)   
  33.     {   
  34.         o=a;   
  35.     }   
  36.        
  37.     //得到T的类型名称   
  38.     public void showTypeName(Gen<T> obj)//参数是泛型   
  39.     {   
  40.         System.out.println("类型是:"+o.getClass().getName());   
  41.         //通过反射机制,可以得到T这个类型的很多信息   
  42.         Method[] m=o.getClass().getDeclaredMethods();//得到某个类型的函数   
  43.         //打印   
  44.         for(int i=0;i<m.length;i++)   
  45.         {   
  46.             System.out.println(m[i].getName());   
  47.         }   
  48.     }   
  49. }   
  50.   
  51. //定义一个Bird   
  52. class Bird   
  53. {   
  54.     public void test1()   
  55.     {   
  56.         System.out.println("aa");   
  57.     }   
  58.     public void count(int a,int b)   
  59.     {   
  60.         System.out.println(a+b);   
  61.     }   
  62. }   
  63.   
  64. /*  
  65.  *泛型的优点  
  66.  *1.类型安全  
  67.  *2.向后兼容  
  68.  *3. 层次清晰  
  69.  *4.性能较高,用Gj编写的代码可以为java编译器和虚拟机带来更多的类型信息。  
  70.  *  
  71.  *泛型主要解决安全和代码重用的问题  
  72.  *在没有泛型之前,通过对类型Object的引用来实现参数的"任意化","任意化"  
  73.  *带来的缺点是要做显式的类型转换,而这种转换是要求开发者对实际参数类型  
  74.  *可以预知的情况下进行的。对于强制类型转换错误的情况,编译器可能不提示  
  75.  *错误 ,在运行时才出现异常,这是一个安全隐患。  
  76.  *泛型的好处是在编译的时候检查类型安全,并且所有的强制转换都是自动和隐  
  77.  *式的,提高代码的重用率。  
  78. */

Class7b / com.test1 / Test3.java

  1. /*  
  2.  * 异常  
  3.  */  
  4.   
  5. package com.test1;   
  6.   
  7. import java.io.FileNotFoundException;   
  8. import java.io.FileReader;   
  9. import java.io.IOException;   
  10. import java.net.Socket;   
  11. import java.net.UnknownHostException;   
  12.   
  13. public class Test3 {   
  14.   
  15.     /**  
  16.      * @param args  
  17.      */  
  18.     public static void main(String[] args) {   
  19.         //检查异常    
  20.         //1.打开文件   
  21.         FileReader fr=null;   
  22.         try {   
  23.             fr=new FileReader("d:\\1.txt");   
  24.             //在出现异常的地方,就终止执行代码,然后进入到catch   
  25.             System.out.println("继续!");   
  26.             Socket s=new Socket("192.168.111.111",78);   
  27.         } catch (Exception e1) {//采用Exception可以捕获所有异常,因为它是父类   
  28.             //把异常的信息输出,利于排除bug   
  29.             System.out.println("\nException 内部");   
  30.             System.out.println("message:="+e1.getMessage());   
  31.             //System.exit(-1); //退出JVM   
  32.             e1.printStackTrace();   
  33.             //处理   
  34.         }   
  35.         finally //这个语句块,不管有没有异常都会执行   
  36.         {   
  37.             System.out.println("\n进入finally!");   
  38.             //一般说,把需要关闭的资源[文件,连接,内存....]   
  39.             if(fr!=null)   
  40.             {   
  41.                 try {   
  42.                     fr.close();   
  43.                 } catch (Exception e) {   
  44.                     e.printStackTrace();   
  45.                 }   
  46.             }   
  47.         }   
  48.            
  49.         //2.连接一个192.168.12.12 ip的端口号4567   
  50.         try {   
  51.             Socket s=new Socket("192.168.1.23",78);   
  52.         } catch (UnknownHostException e) {   
  53.             System.out.println("\nUnknownHostException 内部");   
  54.             e.printStackTrace();   
  55.         } catch (IOException e) {   
  56.             //如果有多个catch语句,则进入匹配异常的那个   
  57.             System.out.println("\nIOException 内部");   
  58.             e.printStackTrace();   
  59.         }   
  60.            
  61.         //3.运行异常   
  62.         try  
  63.         {   
  64.             int a=4/0;   
  65.         }catch(ArithmeticException e)   
  66.         {   
  67.             e.printStackTrace();   
  68.         }   
  69.         System.out.println("ok1");   
  70.     }   
  71. }

Class7b / com.test1 / Test4.java

  1. package com.test1;   
  2.   
  3. import java.io.FileNotFoundException;   
  4. import java.io.FileReader;   
  5.   
  6. public class Test4 {   
  7.   
  8.     /**  
  9.      * @param args  
  10.      * @throws Exception   
  11.      */  
  12.     public static void main(String[] args) throws Exception {   
  13.         //异常抛给JVM去处理   
  14.         //创建一个Father   
  15.         Father father=new Father();   
  16.         father.test1();   
  17.     }   
  18.   
  19. }   
  20.   
  21. class Father   
  22. {   
  23.     private Son son=null;   
  24.     public Father()   
  25.     {   
  26.         son=new Son();   
  27.     }   
  28.     public void test1() throws Exception   
  29.     {   
  30.         //异常抛给调用者(main函数)去处理   
  31.         System.out.println("1");   
  32.         /*  
  33.         try {  
  34.             son.test2();  
  35.         } catch (Exception e) {  
  36.             System.out.println("\n父亲在处理!");  
  37.             e.printStackTrace();  
  38.         }  
  39.         */  
  40.         son.test2();   
  41.     }   
  42. }   
  43.   
  44. class Son   
  45. {   
  46.     public void test2() throws FileNotFoundException   
  47.     {   
  48.         //异常抛给调用者(Father.test1函数)去处理   
  49.         FileReader fr=null;   
  50.         fr=new FileReader("d:\\dd.txt");   
  51.     }   

两个练习

HomeWork / com.test1 / Test1.java

  1. /*  
  2.  * 跳水比赛,8个评委打分。运动员的成绩是8个成绩取掉一个最高分,  
  3.  * 去掉一个最低分,剩下的6个分数的平均分就是最后得分。使用一维  
  4.  * 数组实现打分功能。  
  5.  * 找出最佳与最差评委  
  6.  */  
  7.   
  8. package com.test1;   
  9.   
  10. import java.io.BufferedReader;   
  11. import java.io.IOException;   
  12. import java.io.InputStreamReader;   
  13.   
  14. public class Test1 {   
  15.   
  16.     /**  
  17.      * @param args  
  18.      */  
  19.     public static void main(String[] args) {   
  20.         //   
  21.         Judge judge=new Judge();   
  22.         System.out.println("平均分是:"+judge.lastFen());   
  23.         System.out.println("最差的裁判是:"+(judge.getWorst()+1));   
  24.     }   
  25.   
  26. }   
  27.   
  28. class Judge   
  29. {   
  30.     //定义可以存放8个小数的数组   
  31.     float fens[]=null;   
  32.     int size=3;   
  33.     //构造函数   
  34.     public Judge()   
  35.     {   
  36.         fens=new float[size];   
  37.         //初始化   
  38.         InputStreamReader isr=new InputStreamReader(System.in);//读取控制台数据存到输入流   
  39.         BufferedReader br=new BufferedReader(isr);//将数据从输入流存到Buf流中   
  40.         try {   
  41.             for(int i=0;i<fens.length;i++)   
  42.             {   
  43.                 System.out.println("请输入第"+(i+1)+"裁判的分数:");   
  44.                 fens[i]=Float.parseFloat(br.readLine());   
  45.             }   
  46.         } catch (Exception e) {   
  47.             e.printStackTrace();   
  48.         }   
  49.         finally //关闭IO流   
  50.         {   
  51.             try {   
  52.                 br.close();   
  53.             } catch (IOException e) {   
  54.                 e.printStackTrace();   
  55.             }   
  56.         }   
  57.     }   
  58.   
  59.     //1.去掉最低分(目的找到最低分的下标)   
  60.     public int getLowFenIndex()   
  61.     {   
  62.         //选择法   
  63.         //认为第一个就是最低   
  64.         float minFen=fens[0];   
  65.         int minIndex=0;   
  66.         for(int i=1;i<fens.length;i++)   
  67.         {   
  68.             if(minFen>fens[i]);   
  69.             {   
  70.                 //修改最低分   
  71.                 minFen=fens[i];   
  72.                 minIndex=i;   
  73.             }   
  74.         }   
  75.         return minIndex;   
  76.     }   
  77.     //2.去掉最高分(目的找到最高分的下标)   
  78.     //2.去掉最高分(目的找到最高分的下标)   
  79.     public int getHighFenIndex()   
  80.     {   
  81.         //选择法   
  82.         //认为第一个就是最高   
  83.         float maxFen=fens[0];   
  84.         int maxIndex=0;   
  85.         for(int i=1;i<fens.length;i++)   
  86.         {   
  87.             if(maxFen<fens[i])   
  88.             {   
  89.                 //修改最高分   
  90.                 maxFen=fens[i];   
  91.                 maxIndex=i;   
  92.             }   
  93.         }   
  94.         return maxIndex;   
  95.     }   
  96.     //3.得到平均分   
  97.     //3.得到运动员的最后得分   
  98.     public float lastFen()   
  99.     {   
  100.         float allFen=0;   
  101.         int minIndex=this.getLowFenIndex();   
  102.         int maxIndex=this.getHighFenIndex();   
  103.         for(int i=0;i<fens.length;i++)   
  104.         {   
  105.             if(i!=minIndex&&i!=maxIndex)   
  106.             {   
  107.                 allFen+=fens[i];   
  108.             }   
  109.         }   
  110.         return allFen/(fens.length-2);   
  111.     }   
  112.     //4.得到最差评委   
  113.     public int getWorst()   
  114.     {   
  115.         //假设第一个评委是最差的   
  116.         int worstIndex=0;   
  117.         float tempCai=0f;   
  118.         float cai=Math.abs(fens[0]-lastFen());   
  119.         for(int i=1;i<fens.length;i++)   
  120.         {   
  121.             tempCai=Math.abs(fens[0]-lastFen());   
  122.             if(cai<tempCai)   
  123.             {   
  124.                 worstIndex=i;   
  125.                 cai=tempCai;   
  126.             }   
  127.         }   
  128.         return worstIndex;   
  129.     }   

HomeWork / com.test1 / Test2.java

  1. /*  
  2.  * 三个同学考试,共考三门课:语文、数学、英语。使用二维整数   
  3.  * 数组存放三个同学的学号和所有科目的考试成绩。如下表:  
  4.  * 学号       语文      数学      英语  
  5.  * 1002     78      92      76  
  6.  * 1003     67      88      80  
  7.  * 1007     90      95      80  
  8.  */  
  9.   
  10. package com.test1;   
  11.   
  12. public class Test2 {   
  13.   
  14.     /**  
  15.      * @param args  
  16.      */  
  17.     public static void main(String[] args) {   
  18.         //定义一个二维数组   
  19.         int[][] stus=   
  20.         {{1002,78,23,56},{1003,23,89,34},{1007,78,89,90}};   
  21.         for(int i=0;i<3;i++)   
  22.         {   
  23.             for(int j=0;j<4;j++)   
  24.             {   
  25.                 if(i==0&&j==0)   
  26.                 {   
  27.                     System.out.println("学号\t语文\t数学\t英语\t");   
  28.                 }   
  29.                 System.out.print(stus[i][j]+"\t");   
  30.             }   
  31.             System.out.print("\n");   
  32.         }   
  33.     }   
  34. }
原文地址:https://www.cnblogs.com/luowei010101/p/2074481.html