J2ee开发技术(英文)

上传人:宝**** 文档编号:242917850 上传时间:2024-09-11 格式:PPT 页数:208 大小:974KB
返回 下载 相关 举报
J2ee开发技术(英文)_第1页
第1页 / 共208页
J2ee开发技术(英文)_第2页
第2页 / 共208页
J2ee开发技术(英文)_第3页
第3页 / 共208页
点击查看更多>>
资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,山东大学计算机学院人机交互与虚拟现实实验室,*,J2ee开发技术,李学庆,liyoujn-,xqli,tel:,9/11/2024,1,山东大学计算机学院人机交互与虚拟现实实验室,Chapter 9,Holding,your,Objects,9/11/2024,2,山东大学计算机学院人机交互与虚拟现实实验室,Chapter 9:,Holding your Objects,This chapter explores in depth the container library that Java 2 supplies to hold objects while youre working with them: the simple arrays and more sophisticated containers (data structures) such as ArrayList and HashMap.,9/11/2024,3,山东大学计算机学院人机交互与虚拟现实实验室,Chapter 9:,Holding your Objects,Arrays,arrays of objects hold references,Weeble a; / Null reference,Weeble b = new Weeble5; / Null references,Weeble c = new Weeble4;,for(int i = 0; i c.length; i+) ci = new Weeble();,Weeble d = new Weeble(), new Weeble(), new Weeble() ;,a = new Weeble new Weeble(), new Weeble() ;,9/11/2024,4,山东大学计算机学院人机交互与虚拟现实实验室,Chapter 9:,Holding your Objects,Arrays,arrays of primitives hold the primitive values,int e; / Null reference,int g = new int4;,for(int i = 0; i g.length; i+) gi = i*i;,int h = 11, 47, 93 ;,e = h;,hide(new Weeble new Weeble(), new Weeble() ),Container of primitives,Container classes can hold only references to objects,Array can references or primitives,Integer, Double,Returning an array,String floatrSet(int n) .,9/11/2024,5,山东大学计算机学院人机交互与虚拟现实实验室,Chapter 9:,Holding your Objects,Arrays class,In Java.util,equals(),fill(arrayname,intfromIndex, inttoIndex,value),sort(),binarySearch();,asList()-return a List container,Copying an array,System.arraycopy(name1, start, name2,start, num);,Comparing arrays,Arrays.equals(a1,a2);(vs a1= a2),9/11/2024,6,山东大学计算机学院人机交互与虚拟现实实验室,Chapter 9:,Holding your Objects,Array element comparisons,Java.lang.Comparable interface,compareTo(),/c09:CompType.java,Sorting an array,Arrays.sort(a, Collections.reverseOrder() ;,Arrays.sort(a, new CompTypeComparator() ;,Searching a sorted array,int location = Arrays.binarySearch(a, r);,9/11/2024,7,山东大学计算机学院人机交互与虚拟现实实验室,public interface Generator ,Object next();, /:,/ Fill an array using a generator:,public static void,fill(Object a, Generator gen) ,fill(a, 0, a.length, gen);,public static void,fill(Object a, int from, int to,Generator gen),for(int i = from; i to; i+),ai = gen.next();,9/11/2024,8,山东大学计算机学院人机交互与虚拟现实实验室,/: c09:CompType.java Implementing Comparable in a class.,import com.bruceeckel.util.*;,import java.util.*;,public class CompType implements Comparable ,int i; int j;,public CompType(int n1, int n2) ,i = n1; j = n2; ,public String toString() ,return i = + i + , j = + j + ;,public int compareTo(Object rv) ,int rvi = (CompType)rv).i;,return (i rvi ? -1 : (i = rvi ? 0 : 1);,private static Random r = new Random();,private static int randInt() ,return Math.abs(r.nextInt() % 100;,9/11/2024,9,山东大学计算机学院人机交互与虚拟现实实验室,public static Generator generator() ,return new Generator() ,public Object next() ,return new CompType(randInt(),randInt();,;,public static void main(String args) ,CompType a = new CompType10;,Arrays2.fill(a, generator();,Arrays2.print(before sorting, a = , a);,Arrays.sort(a);,Arrays2.print(after sorting, a = , a);,9/11/2024,10,山东大学计算机学院人机交互与虚拟现实实验室,Chapter 9:,Holding your Objects,Introduction to containers,Collection,: a group of individual elements,List,must hold the elements in a particular sequence,Set,cannot have any duplicate elements Java.lang,Map,: a group of key-value object pairs,Printing containers,/c09:PrintingContains.java,Filling Containers,Collections.fill(name, object);,Container disadvantage:unknown type,Make a type-conscious(有意义的)Array,/c09:MouseList.java,Parameterized type,9/11/2024,11,山东大学计算机学院人机交互与虚拟现实实验室,/: c09:PrintingContainers.java,/ Containers print themselves automatically.,import java.util.*;,public class PrintingContainers ,static Collection fill(Collection c) ,c.add(dog); c.add(dog); c.add(cat);,return c;,static Map fill(Map m) ,m.put(dog, Bosco);,m.put(dog, Spot);,m.put(cat, Rags);,return m;,public static void main(String args) ,System.out.println(fill(new ArrayList();,System.out.println(fill(new HashSet();,System.out.println(fill(new HashMap();,9/11/2024,12,山东大学计算机学院人机交互与虚拟现实实验室,/: c09:MouseList.java A type-conscious ArrayList.,import java.util.*;,public class MouseList ,private ArrayList list = new ArrayList();,public void add(Mouse m) list.add(m);,public Mouse get(int index) ,return (Mouse)list.get(index); ,public int size() return list.size(); ,/: c09:MouseListTest.java,public class MouseListTest ,public static void main(String args) ,MouseList mice = new MouseList();,for(int i = 0; i 3; i+),mice.add(new Mouse(i);,for(int i = 0; i mice.size(); i+),MouseTrap.caughtYa(mice.get(i);,9/11/2024,13,山东大学计算机学院人机交互与虚拟现实实验室,Chapter 9:,Holding your Objects,Iterators,An iterator is an object , move through a sequence of objects , select each object,Get a,Iterator,using,iterator(),.,Get the next object with,next(),.,See if there,are,any more objects with,hasNext(),.,Remove the last element with,remove(),.,9/11/2024,14,山东大学计算机学院人机交互与虚拟现实实验室,Container taxonomy,(分类,),9/11/2024,15,山东大学计算机学院人机交互与虚拟现实实验室,Container functionality,Collection,boolean add(Object),boolean addAll(Collection),void clear(),boolean contains(Object),boolean containsAll(Collection),boolean isEmpty(),Iterator iterator(),boolean remove(Object),boolean removeAll(Collection),boolean retainAll(Collection) (只保留参数集合的元素),int size(),Object toArray(),9/11/2024,16,山东大学计算机学院人机交互与虚拟现实实验室,Container functionality,List,List,(interface),Order is the most important feature,insertion and removal of elements in the middle of a List.,produce a ListIterator,ArrayList,A List implemented with an array.,Allows rapid random access to elements,ListIterator- back-and-forth traversal of an ArrayList,not inserting and removing elements,9/11/2024,17,山东大学计算机学院人机交互与虚拟现实实验室,Container functionality,List,LinkedList,Provides optimal sequential access,addFirst(),addLast(),getFirst(),getLast(),removeFirst(), and,removeLast(),to allow it to be used as a stack, a queue, and a deque.,9/11/2024,18,山东大学计算机学院人机交互与虚拟现实实验室,Container functionality,Making a stack from a,LinkedList,public class StackL ,private LinkedList list = new LinkedList();,public void push(Object v) ,list.addFirst(v);,public Object top() return list.getFirst(); ,public Object pop() ,return list.removeFirst();,9/11/2024,19,山东大学计算机学院人机交互与虚拟现实实验室,Container functionality,Making a queue from a LinkedList,public class Queue ,private LinkedList list = new LinkedList();,public void put(Object v) list.addFirst(v); ,public Object get() ,return list.removeLast();,public boolean isEmpty() ,return list.isEmpty();,9/11/2024,20,山东大学计算机学院人机交互与虚拟现实实验室,Container functionality,Set functionality,Set (interface),Each element must be unique;,Objects must define equals(),does not guarantee in any particular order,HashSet,fast lookup,define hashCode(),TreeSet,ordered Set backed by a tree,9/11/2024,21,山东大学计算机学院人机交互与虚拟现实实验室,Container functionality,Map functionality,Map (interface),you can look up a value using a key.,HashMap,Implementation based on a hash table.,constant-time performance for inserting and locating pairs.,capacity,and,load factor,of the hash table.,TreeMap,Implementation based on a red-black tree.,TreeMap is in sorted order.,9/11/2024,22,山东大学计算机学院人机交互与虚拟现实实验室,Chapter 12,Run-Time,Type,Identification,9/11/2024,23,山东大学计算机学院人机交互与虚拟现实实验室,Chapter12:,Run-Time Type Identification,Java run-time type identification (RTTI) lets you find the exact type of an object,This chapter explains what RTTI is for, how to use it, and how to get rid of,(除去,),it when it doesnt belong there.,This chapter introduces the Java,reflection,mechanism.,9/11/2024,24,山东大学计算机学院人机交互与虚拟现实实验室,Chapter12:,Run-Time Type Identification,The need for RTTI,ArrayList s = new ArrayList();,s.add(new Circle(); s.add(new Square();,s.add(new Triangle();,Iterator e = s.iterator();,while(e.hasNext() (Shape)e.next().draw();,9/11/2024,25,山东大学计算机学院人机交互与虚拟现实实验室,Chapter12:,Run-Time Type Identification,The Class Object,To create a class object,Class.forName(Gum);,Gum.class,Boolean.TYPE(a reference of primitive),boolearn.class = Boolean.Type,char.class = Character.Type,Checking before a cast,The classic cast;,The,Class,object representing the type of your object.,instanceof,if(x instanceof Dog) (Dog)x).bark();,9/11/2024,26,山东大学计算机学院人机交互与虚拟现实实验室,Chapter12:,Run-Time Type Identification,The Class Object,Using class literals,A.class,.newInstance(),A dynamic instanceof,petTypesj.isInstance(o),instanceof vs. Class equivalence,Instanceof 和isInstance的结果一样,表示的含义:你是这个类或者这个类的子类,=不管继承关系,9/11/2024,27,山东大学计算机学院人机交互与虚拟现实实验室,/: c12:ToyTest.java Testing class Class.,interface HasBatteries ,interface Waterproof ,interface ShootsThings ,class Toy ,/ Comment out the following default,/ constructor to see,/ NoSuchMethodError from (*1*),Toy() ,Toy(int i) ,class FancyToy extends Toy,implements HasBatteries,Waterproof, ShootsThings ,FancyToy() super(1); ,9/11/2024,28,山东大学计算机学院人机交互与虚拟现实实验室,public class ToyTest ,public static void main(String args),throws Exception ,Class c = null;,try ,c = Class.forName(FancyToy);, catch(ClassNotFoundException e) ,System.err.println(Cant find FancyToy);,throw e;,printInfo(c);,Class faces = c.getInterfaces();,for(int i = 0; i faces.length; i+),printInfo(facesi);,Class cy = c.getSuperclass();,Object o = null;,9/11/2024,29,山东大学计算机学院人机交互与虚拟现实实验室,try ,/ Requires default constructor:,o = cy.newInstance(); / (*1*), catch(InstantiationException e) ,System.err.println(Cannot instantiate);,throw e;, catch(IllegalAccessException e) ,System.err.println(Cannot access);,throw e;,printInfo(o.getClass();,static void printInfo(Class cc) ,System.out.println(,Class name: + cc.getName() +, is interface? +,cc.isInterface() + );,9/11/2024,30,山东大学计算机学院人机交互与虚拟现实实验室,Class name: FancyToy is interface? false,Class name: HasBatteries is interface? true,Class name: Waterproof is interface? true,Class name: ShootsThings is interface? true,Class name: Toy is interface? false,Note:getClass get the Object of Class Type,9/11/2024,31,山东大学计算机学院人机交互与虚拟现实实验室,Chapter12:,Run-Time Type Identification,Reflection:run-time class information,The class,Class,supports of,reflection,java.lang.reflect,get Field(),get,Method(),get,Constructor(),A class method extractor,/c12:showMethods.java,9/11/2024,32,山东大学计算机学院人机交互与虚拟现实实验室,/: c12:ShowMethods.java,/ Using reflection to show all the methods of,/ a class, even if the methods are defined in,/ the base class.,import java.lang.reflect.*;,public class ShowMethods ,static final String usage =,usage: n +,ShowMethods qualified.class.namen +,To show all methods in class or: n +,ShowMethods qualified.class.name wordn +,To search for methods involving word;,public static void main(String args) ,if(args.length 1) ,System.out.println(usage);,System.exit(0);,9/11/2024,33,山东大学计算机学院人机交互与虚拟现实实验室,try Class c = Class.forName(args0);,Method m = c.getMethods();,Constructor ctor = c.getConstructors();,if(args.length = 1) ,for (int i = 0; i m.length; i+),System.out.println(mi);,for (int i = 0; i ctor.length; i+),System.out.println(ctori);, else ,for (int i = 0; i m.length; i+),if(mi.toString().indexOf(args1)!= -1),System.out.println(mi);,for (int i = 0; i ctor.length; i+),if(ctori.toString().indexOf(args1)!= -1),System.out.println(ctori);, catch(ClassNotFoundException e) ,System.err.println(No such class: + e);, ,9/11/2024,34,山东大学计算机学院人机交互与虚拟现实实验室,Java类库-,基本类库-1,Math类中常用的方法,类方法,参数类型,结果类型,功能,abs(a),int double,int double,返回a的绝对值,cos(a),double,double,返回a的余弦,a是弧度。,floor(a),double,int,返回小于或等于a的最大整数,max(a,b),int double,int double,返回 a、b中的最大值。,min(a,b),int double,int double,返回a、b中的最小值。,pow(a,b),double,double,返回以a为底以b为指数的幂,random(),double,返回一个0.0至1.0之间的随机数,round(a),float double,int,返回a的四舍五入的int值,sqrt(a),double,double,返回a的平方根,9/11/2024,35,山东大学计算机学院人机交互与虚拟现实实验室,Java类库-,基本类库-2,使用Math类方法的语法:,Math.(参数),使用PI和E的语法:,Math.PI,Math.E,把下面的数学公式写成Java中的表达式,:,Math.sin(Math.PI)/a*x+Math.abs(Math.cos(Math.PI*x/2),2*Math.PI*Math.sqrt(1/g),V0*t+1/2.*g*Math.pow(t,2),Math.sqrt(p*(p-a)*(p-b)*(p-c),9/11/2024,36,山东大学计算机学院人机交互与虚拟现实实验室,Java类库-,基本类库-3,基本数据类型: 与基本数据类型对应的类:,1. byte Byte,2. short Short,3. int Integer,4. long Long,5. float Float,6. double Double,7. char Character,8. boolean Boolean,用这些类声明的是对象,而这些对象不仅具有值的属性同时具有各种方法。,如:Integer I; String s1=“1024”,s2=“”;,Double D;,double d=6.8;int i;,D=new Double(d);,I=new Integer(512);,s2=D.toString();,d=Double.valueOf(s1).doubleValue();,i=Integer.ParseInt(s1);,Double,对象,double,型数值,创建对象并赋值,把字符串转换成数值,9/11/2024,37,山东大学计算机学院人机交互与虚拟现实实验室,Java类库-,基本类库-4,String,是,java.lang,包中的一个类.,因为 String 是一个类, 在Java 语言中处理字符串时,我们需要创建 String 的一个实例。像使用其他对象一样,需要定义和创建 String 类的实例. 例如,,String name1;,name1 = new String( “Latte” );,但是我们通常采用一种简单的表示方法,将,String,对象看作基本数据来使用,String,对象。例如,,String name1;,name1 = “Latte”;,两个语句是等价的,9/11/2024,38,山东大学计算机学院人机交互与虚拟现实实验室,Java类库-,基本类库-5,int javaCount = 0;,boolean repeat = true;,String word;,while ( repeat ) ,word = inputBox.getString(Next word:);,if ( word.equals(STOP) ) ,repeat = false;,else if ( word.equalsIgnoreCase(Java) ) ,javaCount+;,连续的读单词并且统计在输入过程中出现了多少次单词,Java,,这里忽略大小写,注意:这里是如何比较的 ,我们不能使用,=,操作符,。,9/11/2024,39,山东大学计算机学院人机交互与虚拟现实实验室,Java类库-,基本类库-6,方法,含义,length,获取字符串的长度 int n = str1.length();,charAt,获取单个字符 char x = str1.charAt(0);,compareTo,比较两个字符串,pareTo( str2 ),substring,从一个字符串中抽取一个字串,str1.substring( 1, 4 ),trim,去掉头部和尾部的空格,str1.trim( ),valueOf,将给定的基本数据类型转换成字符串,String.valueOf( 123.4565 ),startsWith,如果字符串以指定的字串开始,则返回真,str1.startsWith( str2 ),endsWith,如果字符串以指定的字串结束,则返回真,str1.endsWith( str2 ),String 常用方法:,9/11/2024,40,山东大学计算机学院人机交互与虚拟现实实验室,Java类库-,基本类库-7,StringBuffer 示例,char letter;,String inSentence = inputBox.getString(Enter a sentence:);,StringBuffer tempStringBuffer = new StringBuffer(inSentence);,int numberOfCharacters = tempStringBuffer.length();,for (int index = 0; index numberOfCharacters; index+) ,letter = tempStringBuffer.charAt(index);,if (letter = a | letter = A | letter = e | letter = E |,letter = i | letter = I | letter = o | letter = O |,letter = u | letter = U ) ,tempStringBuffer.setCharAt(index,X);,messageBox.show( tempStringBuffer );,9/11/2024,41,山东大学计算机学院人机交互与虚拟现实实验室,Java类库-,基本类库-8,方法,含义,length,获取字符串的长度 int n = str1.length();,charAt,获取单个字符 char x = str1.charAt(0);,append,附加新的串,可以是任意类型,自动转换,str1.append( str2 ) Str1,appedn(10);,insert,插入新的串,可以是任意的类型,自动转换 str1,.insert(5,”abc”);Str1.insert(4,10);,setCharAt,设置新的单个字符,str1.setCharAt(3,X);,reverse,将字符串反序,String.reverse();,setLength,设置字符串的长度,str1.setLength(100 ),toString,得到对应的字符串常量 s =,str1.toString(),StringBuffer 常用方法:,9/11/2024,42,山东大学计算机学院人机交互与虚拟现实实验室,Java类库-StringTokenizer (特征化),StringTokenizer(String str),初始化,StringTokenizer,对象,StringTokenizer (String str, String delim),初始化,StringTokenizer,对象,,delim指定特定的分割符,int countTokens,(),计算 nextToken 方法可以调用的次数,boolean has MoreTokens,(),是否还包含特征化的串,String nextToken,(),返回下一个特征串,9/11/2024,43,山东大学计算机学院人机交互与虚拟现实实验室,Chapter 13,Creating,Windows,and,Applets,9/11/2024,44,山东大学计算机学院人机交互与虚拟现实实验室,Chapter13:,Creating Windows and Applets,This chapter is an introduction to Swing and the creation of World Wide Web applets.,This is fundamental for the creation of Rapid-Application Development (RAD) program-building tools.,9/11/2024,45,山东大学计算机学院人机交互与虚拟现实实验室,Chapter13:,Creating Windows and Applets,The basic applet,Applet restrictions,An applet cant touch the local disk.,Applets can take longer to display,Applet advantages,There is no installation issue,You dont have to worry about bad code causing damage to someones system,Application frameworks,import javax.swing.*;,import java.awt.*;,public class Applet1 extends JApplet ,public void init() ,getContentPane().add(new JLabel(Applet!);,9/11/2024,46,山东大学计算机学院人机交互与虚拟现实实验室,Chapter13:,Creating Windows and Applets,Method,Operation,init(),Automatically called to perform first-time initialization of the applet, including component layout. Youll always override this method.,start(),Called every time the applet moves into sight on the Web browser to allow the applet to start up its normal operations (especially those that are shut off by,stop(),). Also called after,init(),.,stop(),Called every time the applet moves out of sight on the Web browser to allow the applet to shut off expensive operations. Also called right before,destroy(),.,destroy(),Called when the applet is being unloaded from the page to perform final release of resources when the applet is no longer used,9/11/2024,47,山东大学计算机学院人机交互与虚拟现实实验室,Chapter13:,Creating Windows and Applets,Running applets inside a Web browser,Using,Appletviewer,appletviewer Applet1b.java,Running applets from the command line,/: c13:Applet1c.java,A display framework,/combruceeckel:swing:console.java,Using the windows Explorer,9/11/2024,48,山东大学计算机学院人机交互与虚拟现实实验室,/: c13:Applet1c.java,import javax.swing.*;,import java.awt.*;,import com.bruceeckel.swing.*;,public class Applet1c extends JApplet ,public void init() ,getContentPane().add(new JLabel(Applet!); ,public static void main(String args) ,JApplet applet = new Applet1c();,JFrame frame = new JFrame(Applet1c);,Console.setupClosing(frame);,frame.getContentPane().add(applet);,frame.setSize(100,50);,applet.init(); applet.start();,frame.setVisible(true);,9/11/2024,49,山东大学计算机学院人机交互与虚拟现实实验室,/: com:bruceeckel:swing:Console.java,/ Tool for running Swing demos from the,/ console, both applets and JFrames.,package com.bruceeckel.swing;,import javax.swing.*;,import java.awt.event.*;,public class Console ,/ Create a title string from the class name:,public static String title(Object o) ,String t = o.getClass().toString();,/ Remove the word class:,if(t.indexOf(class) != -1),t = t.substring(6);,return t;,9/11/2024,50,山东大学计算机学院人机交互与虚拟现实实验室,public static void setupClosing(JFrame frame) ,frame.addWindowListener(new WindowAdapter() ,public void windowClosing(WindowEvent e) ,System.exit(0);,);,/ The improved solution in JDK 1.3:,/ frame.setDefaultCloseOperation(,/ EXIT_ON_CLOSE);,public static void,run(JFrame frame, int width, int height) ,setupClosing(frame);,frame.setSize(width, height);,frame.setVisible(true);,9/11/2024,51,山东大学计算机学院人机交互与虚拟现实实验室,public static void,run(JApplet applet, int width, int height) ,JFrame frame = new JFrame(title(applet);,setupClosing(frame);,frame.getContentPane().add(applet);,frame.setSize(width, height);,applet.init(); applet.start();,frame.setVisible(true);,public static void,run(JPanel panel, int width, int height) ,JFrame frame = new JFrame(title(panel);,setupClosing(frame);,frame.getContentPane().add(panel);,frame.setSize(width, height);,frame.setVisible(true);,9/11/2024
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 管理文书 > 各类标准


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!