Java第六课 数组的复制与排序,Runtime类与Process类,Class类与反射API,Runtime类与单例设计模式。

来源:互联网 发布:毕业分布图 知乎 编辑:程序博客网 时间:2024/06/13 07:33

Java的常用包

java.applet:包含一些用于创建Java小应用程序的类。
java.awt:包含一些用于编写与平台无关的图形界面(GUI)应用程序的类。
java.io:包含一些用作输入输出(I/O)处理的类。
java.lang:包含一些Java语言的基本类与核心类,如String、Math、Integer、System和Runtime,提供常用的功能,这个包中的所有类是被隐式导入的。
java.net:包含用于建立网络连接的类,与java.io同时使用完成与网络有关的读写。
java.util:包含一些实用工具类和数据结构类。


“==”和“equals”的用法

在Java中,boolean、byte、short、int、long、char、float、double这八种是基本数据类型,其余的都是引用类型。
“==”是比较两个变量的值是否相等,“equals”是比较两个对象变量所代表的对象的内容是否相等。


String和StringBuffer

String str=“abc”;
    int i=3;
    float f=4.5f;
    char ch='a';
    boolean b=true;
    System.out.println(str + i + f + ch + b);
针对String的“+”和“+=”,是Java中唯一被重载的操作符;在Java中,不允许程序员重载操作符。

String类对象一个常量对象。
    String str=“abc”;
    str=“def”;
在处理大量字符串的程序中,我们通常用StringBuffer来替代String。


函数的调用

在Java中,传参时,都是以传值的方式进行。
对于基本数据类型,传递的是数据的拷贝;对于引用类型,传递的引用的拷贝。

对象的克隆(clone)

为了获取对象的一份拷贝,我们可以利用Object类的clone()方法。
在派生类中覆盖基类的clone()方法,并声明为public。
在派生类的clone()方法中,调用super.clone()。
在派生类中实现Cloneable接口。

为什么我们在派生类中覆盖Object的clone()方法时,一定要调用super.clone()呢?在运行时刻,Object中的clone()识别出你要复制的是哪一个对象,然后为此对象分配空间,并进行对象的复制,将原始对象的内容一一复制到新对象的存储空间中。


数组的相关操作

在Java中,所有的数组都有一个缺省的属性length,用于获取数组中元素的个数。
数组的复制:System.arraycopy()。
数组的排序:Arrays.sort()。
在已排序的数组中查找某个元素:Arrays.binarySearch()。


Class类

在Java中,每个class都有一个相应的Class对象。也就是说,当我们编写一个类,编译完成后,在生成的.class文件中,就会产生一个Class对象,用于表示这个类的类型信息。
获取Class实例的三种方式:
(1)利用对象调用getClass()方法获取该对象的Class实例;
(2)使用Class类的静态方法forName(),用类的名字获取一个Class实例;
(3)运用.class的方式来获取Class实例,对于基本数据类型的封装类,还可以采用.TYPE来获取相对应的基本数据类型的Class实例。

在运行期间,如果我们要产生某个类的对象,Java虚拟机(JVM)会检查该类型的Class对象是否已被加载。如果没有被加载,JVM会根据类的名称找到.class文件并加载它。一旦某个类型的Class对象已被加载到内存,就可以用它来产生该类型的所有对象。
newInstance() 调用类中缺省的构造方法。

Runtime类和Process类

每一个Java程序都有一个Runtime类的单一实例。
通过Runtime.getRuntime()获取Runtime类的实例。
Runtime类是使用单例模式的一个例子。


设计模式

在我们进行程序设计时,逐渐形成了一些典型问题和问题的解决方案,这就是软件模式。
每一个模式描述了一个在我们程序设计中经常发生的问题,以及该问题的解决方案。
当我们碰到模式所描述的问题,就可以直接用相应的解决方法去解决这个问题,这就是设计模式。

单例(Singleton)模式
   (1)一个类只有一个实例,而且自行实例化并向整个系统提供这个实例,这个类称为单例类。
   (2)单例类的一个最重要的特点是类的构造方法是私有的,从而避免了外部利用构造方法直接创建多个实例。


import java.util.Arrays;class ArrayTest{public static void main(String[] args){/*int[] num1=new int[]{1,2,3};int[] num2=new int[10];System.arraycopy(num1,1,num2,8,2);for(int i=0;i<num2.length;i++){System.out.println(num2[i]);}*//*Point[] pts1=new Point[]{new Point(1,1),new Point(2,2),new Point(3,3)};Point[] pts2=new Point[3];System.arraycopy(pts1,0,pts2,0,pts1.length);for(int i=0;i<pts2.length;i++){System.out.println("x="+pts2[i].x+","+"y="+pts2[i].y);}pts2[1].x=5;pts2[1].y=5;System.out.println("x="+pts1[1].x+","+"y="+pts1[1].y);*//*int[] num=new int[]{3,1,2};Arrays.sort(num);for(int i=0;i<num.length;i++){System.out.println(num[i]);}int index=Arrays.binarySearch(num,3);System.out.println("index="+index);System.out.println("element="+num[index]);*/Student[] ss=new Student[]{new Student(1,"zhangsan"),   new Student(2,"lisi"),   new Student(3,"wangwu"),   new Student(3,"mybole")};Arrays.sort(ss);for(int i=0;i<ss.length;i++){System.out.println(ss[i]);}int index=Arrays.binarySearch(ss,new Student(2,"lisi"));System.out.println("index="+index);System.out.println(ss[index]);}}class Student implements Comparable{int num;String name;Student(int num,String name){this.num=num;this.name=name;}public String toString(){return "number="+num+","+"name="+name;}public int compareTo(Object o){Student s=(Student)o;//return num>s.num ? 1 : (num==s.num ? 0 : -1);int result=num>s.num ? 1 : (num==s.num ? 0 : -1);if(0==result){result=name.compareTo(s.name);}return result;}}class Point{int x,y;Point(int x,int y){this.x=x;this.y=y;}}

import java.lang.reflect.*;class ClassTest{public static void main(String[] args){/*Point pt=new Point();Class c1=pt.getClass();System.out.println(c1.getName());try{Class c2=Class.forName("Point");System.out.println(c2.getName());}catch(Exception e){e.printStackTrace();}Class c3=Point.class;System.out.println(c3.getName());Class c4=int.class;System.out.println(c4.getName());Class c5=Integer.TYPE;System.out.println(c5.getName());Class c6=Integer.class;System.out.println(c6.getName());*//*System.out.println("before new Point()");new Point();System.out.println("after new Point()");try{Class.forName("Line");}catch(Exception e){e.printStackTrace();}*//*if(args.length!=1){return;}try{Class c=Class.forName(args[0]);Point pt=(Point)c.newInstance();pt.output();}catch(Exception e){e.printStackTrace();}*/if(args.length!=1){return;}try{Class c=Class.forName(args[0]);Constructor[] cons=c.getDeclaredConstructors();/*for(int i=0;i<cons.length;i++){System.out.println(cons[i]);}Method[] ms=c.getDeclaredMethods();for(int i=0;i<ms.length;i++){System.out.println(ms[i]);}*/Class[] params=cons[0].getParameterTypes();Object[] paramValues=new Object[params.length];for(int i=0;i<params.length;i++){if(params[i].isPrimitive()){paramValues[i]=new Integer(i+3);}}Object o=cons[0].newInstance(paramValues);Method[] ms=c.getDeclaredMethods();ms[0].invoke(o,null);}catch(Exception e){e.printStackTrace();}}}class Point{static{System.out.println("Loading Point");}int x,y;void output(){System.out.println("x="+x+","+"y="+y);}Point(int x,int y){this.x=x;this.y=y;}}class Line{static{System.out.println("Loading Line");}}

import java.io.*;class RuntimeTest{public static void main(String[] args){Runtime rt=Runtime.getRuntime();System.out.println(rt.freeMemory());System.out.println(rt.totalMemory());try{//rt.exec("notepad");Process p=rt.exec("javac ArrayTest.java");InputStream is=p.getInputStream();int data;while((data=is.read())!=-1){System.out.print((char)data);}}catch(Exception e){e.printStackTrace();}}}

class StringTest{public static void change(int x,int y){x=x+y;y=x-y;x=x-y;}public static void change(int[] num){num[0]=num[0]+num[1];num[1]=num[0]-num[1];num[0]=num[0]-num[1];}public static void change(Point pt){pt.x=pt.x+pt.y;pt.y=pt.x-pt.y;pt.x=pt.x-pt.y;}public static void main(String[] args){/*int x=3;int y=4;change(x,y);System.out.println("x="+x+","+"y="+y);*//*int[] num=new int[]{3,4};change(num);System.out.println("x="+num[0]+","+"y="+num[1]);*//*Point pt=new Point();pt.x=3;pt.y=4;change(pt);//System.out.println("x="+pt.x+","+"y="+pt.y);System.out.println(pt);*///System.out.println(args[0]);/*if(args.length>0){for(int i=0;i<args.length;i++){System.out.println(args[i]);}}*//*String str1=new String("abc");String str2=new String("abc");String str3=str1;*//*if(str1==str2){System.out.println("str1==str2");}else{System.out.println("str1!=str2");}*//*if(str1==str3){System.out.println("str1==str3");}else{System.out.println("str1!=str3");}*//*if(str1.equals(str2)){System.out.println("str1 equals str2");}else{System.out.println("str1 not equals str2");}*//*int i=3;float f=1.5f;char ch='f';boolean b=false;//System.out.println(str1+i+f+ch+b);StringBuffer sb=new StringBuffer();sb.append(str1).append(i).append(f).append(ch).append(b);//System.out.println(sb.toString());sb.delete(4,8);sb.insert(4,"mybole");System.out.println(sb);*//*int[] num=new int[3];for(int i=0;i<num.length;i++){System.out.println(num[i]);}num=null;*//*Student[] students;students=new Student[3];for(int i=0;i<students.length;i++){System.out.println(students[i]);}*/Professor p=new Professor("wangwu",50);Student s1=new Student("zhangsan",18,p);Student s2=(Student)s1.clone();/*s2.name="lisi";s2.age=20;*/s2.p.name="lisi";s2.p.age=30;//System.out.println("name="+s1.name+","+"age="+s1.age);System.out.println("name="+s1.p.name+","+"age="+s1.p.age);}}class Professor implements Cloneable{String name;int age;Professor(String name,int age){this.name=name;this.age=age;}public Object clone(){Object o=null;try{o=super.clone();}catch(CloneNotSupportedException e){System.out.println(e.toString());}return o;}}class Student implements Cloneable{String name;int age;Professor p;Student(String name,int age,Professor p){this.name=name;this.age=age;this.p=p;}public Object clone(){//Object o=null;Student o=null;try{o=(Student)super.clone();}catch(CloneNotSupportedException e){System.out.println(e.toString());}o.p=(Professor)p.clone();return o;}}class Point{int x,y;public String toString(){return "x="+x+","+"y="+y;}}

class Test{public static void main(String[] args){int i=3;Integer in=new Integer(i);int j=in.intValue();System.out.println("j="+j);String str=in.toString();System.out.println("str="+str);String str2="134";System.out.println(Integer.valueOf(str2));}}