第6讲面向对象特征解析课件

上传人:txadgkn****dgknqu... 文档编号:242769377 上传时间:2024-09-03 格式:PPT 页数:36 大小:284.81KB
返回 下载 相关 举报
第6讲面向对象特征解析课件_第1页
第1页 / 共36页
第6讲面向对象特征解析课件_第2页
第2页 / 共36页
第6讲面向对象特征解析课件_第3页
第3页 / 共36页
点击查看更多>>
资源描述
,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,第6讲:面向对象特征总结与实例,1 接口与抽象类,2 异常类,3 类与类型,4 基础数据结构,第6讲:面向对象特征总结与实例1 接口与抽象类,1,1接口与抽象类1.1接口语法,public interface,接口名,extends,父接口名列表,/,接口体,;,/,常量域声明,public static final,域类型,域名,=,常量值,;,/,抽象方法声明,public abstract native,返回值,方法名,(,参数列表,) throw,异常列表,;,从上面的语法规定可以看出,定义接口与定义类非常相似,实际上完全可以把接口理解成为一种特殊的类,接口是由常量和抽象方法组成的特殊类。,1接口与抽象类1.1接口语法public inter,2,1.2接口声明,(1)接口中的属性都是用 final修饰的常量,在这个类中,所有的成员函数都是抽象的,也就是说它们都只有说明没有定义;,(2)接口中的方法都是用abstract修饰的抽象方法,在接口中只能给出这些抽象方法的方法名、返回值和参数列表,而不能定义方法体,即仅仅规定了一组信息交换、传输和处理的“接口”。,1.2接口声明(1)接口中的属性都是用 final修饰的常量,3,1.3接口实现,在类的声明部分,用,implements,关键字声明该类将要实现哪些接口,如果实现某接口的类不是,abstract,的抽象类,则在类的定义部分必须实现指定接口的所有抽象方法,即为所有抽象方法定义方法体,而且方法头部分应该与接口中的定义完全一致,即有完全相同的返回值和参数列表,如果实现某接口的类是,abstract,的抽象类,则它可以不实现该接口所有的方法,一个类在实现某接口的抽象方法时,必须使用完全相同的方法头,接口的抽象方法,其访问限制符都已指定是,public,,所以类在实现方法时,必须显式地使用,public,修饰符,1.3接口实现在类的声明部分,用implements关键字声,4,2 异常类,异常:正常程序所不能处理或者无法处理的情况。,原因:1、避免程序繁琐与复杂,2、当前层次处理不恰当,引入异常机制的目的:,1、使异常处理简化、统一,2、保留异常处理的灵活性,2 异常类异常:正常程序所不能处理或者无法处理的情况。,5,2.1异常类,通常用类Exception及其子类来描述异常的特征。,按照编译时是否能够监测,分为:,Checked Exception,Unchecked Exception两种,其中非监测异常又分为RuntimeException、Error。通常Error是致命性的,无法由程序来处理,例如VirtualMachineError.,2.1异常类通常用类Exception及其子类来描述异常的特,6,自定义异常类,本例中,当a的值小于10或大于100时,将产生异常,。,class MyException1 extends Exception,int num;,MyException1(int a),num=a;,public String toString(),return num+100!rn值必须小于100;,自定义异常类,7,2.2异常产生与声明,声明抛出异常是一个子句,只能加在方法头部的后边。,语法格式如下:,throws ,如:public int read() throws IOException .,真正抛出异常的动作是由抛出异常语句来完成的。,格式如下:,throw ;,其中:必须是Throwable类或其子类的对象。,如:throw new Exception(这是一个异常);,下面的语句在编译时将会产生语法错误:,throw new String(能抛出吗?);,这是因为String类不是Throwable类的子类。,2.2异常产生与声明声明抛出异常是一个子句,只能加在方法头部,8,2.2异常产生与声明,从键盘读入汉字,打印出其机内码。注意不是UNICODE码。若按了a键,则立即抛出异常。,import java.io.*;,public class Ex_Exception3, public static void main(String args), int c;,try,while ( ( c = System.in.read()!=-1 ), if( c=a ),throw new Exception(键a坏了!);,System.out.println(c);,catch(IOException e),System.out.println(e);,catch(Exception e),System.out.println(e);,2.2异常产生与声明从键盘读入汉字,打印出其机内码。注意不是,9,2.3异常捕获与处理,Java中使用try-catch-finally语句来捕获并处理异常,try-catch-finally语句的语法格式如下:,try,/可能会产生异常的程序代码,catch(Exception_1 e1) /处理异常Exception_1的代码 ,catch(Exception_2 e2) /处理异常Exception_2的代码 ,.,catch(Exception_n en) /处理异常Exception_n的代码 , finally/通常是释放资源的程序代码 ,整个语句由try块、catch块和可以缺省finally块三部分组成。,2.3异常捕获与处理Java中使用try-catch-fin,10,2.3异常捕获与处理,当产生异常时,程序从上往下依次判该异常是不是catch(Exception_x e)块中Exception_x类或其子类的对象。,try-catch-finally语句的嵌套。,try-catch-finally语句的try块、catch块、finally块中的程序代码都可以嵌套另外的try-catch-finally语句,且嵌套层次数任意。,2.3异常捕获与处理当产生异常时,程序从上往下依次判该异常是,11,2.4 异常处理总结,对Error类或其子类的对象,程序中不必进行处理,对RuntimeException类或其子类,程序中可以不必进行处理,除此之外的异常,程序员都应该在程序中进行处理。要么用try-catch-finally进行捕获处理,要么明确表示不处理从而声明抛出异常,要么先捕获处理然后再次抛出。,Java的异常处理机制(try-catch-finally语句、throws 子句、throw 语句)带来Java程序代码结构上的改变,不能滥用异常机制。简单的出错判断建议用if语句,不要过分细分异常,2.4 异常处理总结对Error类或其子类的对象,程序中不必,12,3 类与类型,类经过编译后是二进制的class文件,在程序运行时被装入。,Java的类是动态装入的。,Class类封装了关于类的系列操作,3 类与类型类经过编译后是二进制的class文件,在程序运行,13,3.1Class类,每个类或者接口都由一个Class对象来管理,Object类中的方法getClass(),public final,Class,getClass(),Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.,Returns:,the object of type,Class,that represents the runtime class of the object.,3.1Class类每个类或者接口都由一个Class对象来管理,14,java.lang Class Class,java.lang.Object,java.lang.Class,All Implemented Interfaces:,Serializable,from F:J2SEdocsapijavalangClass.html,java.lang Class Class from F:,15,public final class Class,extends,Object,implements,Serializable,Instances of the class,Class,represent classes and interfaces in a running Java application. Every array also belongs to a class that is reflected as a,Class,object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (,boolean,byte,char,short,int,long,float, and,double,), and the keyword,void,are also represented as,Class,objects.,Class,has no public constructor. Instead,Class,objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the,defineClass,method in the class loader.,The following example uses a,Class,object to print the class name of an object:,void printClassName(Object obj) System.out.println(The class of + obj + is + obj.getClass().getName(); ,It is also possible to get the,Class,object for a named type (or for void) using a class literal (JLS Section,15.8.2,). For example:,System.out.println(The name of class Foo is: +Foo.class.getName();,Since:,JDK1.0,See Also:,ClassLoader.defineClass(byte, int, int),Serialized Form,from F:J2SEdocsapijavalangClass.html,public final class Class,16,3.2类装载,缺省装载机制:,Java运行系统根据需要加载相应的类。当前引用的类编译后的字节码还未被加载时,通常使用“类路径”(CLASSPATH)搜索字节码,并自动加载。,3.2类装载缺省装载机制:,17,2.2类加载,手动加载机制,通过使用一个ClassLoader对象来加载指定的类。,static,Class,forName,(,String,className),Returns the Class object associated with the class or interface with the given string name.,Object,newInstance,(),Creates a new instance of the class represented by this Class object,2.2类加载手动加载机制staticClass forN,18,定义加载器,class NetworkClassLoader extends ClassLoader,String host;,int port;,public Class findClass(String name), byte b = loadClassData(name);,return defineClass(name, b, 0, b.length); ,private byte loadClassData(String name), / load the class data from the connection . ,.加载类,ClassLoader loader= new NetworkClassLoader(host,port);,Object main= loader.loadClass(Main, true).newInstance(); .,定义加载器,19,2.3包装类,Object,Boolean Character Number Class,Byte Short Integer Long Float Double,class java.lang.,Math,2.3包装类,20,Boolean,java.lang Class Boolean,java.lang.Object,java.lang.Boolean,All Implemented Interfaces:,Serializable,static,Boolean,valueOf,(booleanb),Returns a Boolean instance representing the specified boolean value.,static,Boolean,valueOf,(,String,s),Returns a,Boolean,with a value represented by the specified String.,Booleanjava.lang Class Boolea,21,Number,java.lang Class Number,java.lang.Object,java.lang.Number,All Implemented Interfaces:,Serializable,Direct Known Subclasses:,BigDecimal,BigInteger,Byte,Double,Float,Integer,Long,Short,Numberjava.lang Class Number,22,Number,public abstract class Number,extends,Object,implements,Serializable,The abstract class,Number,is the superclass of classes,BigDecimal,BigInteger,Byte,Double,Float,Integer,Long, and,Short,.,Subclasses of,Number,must provide methods to convert the represented numeric value to,byte,double,float,int,long, and,short,.,Numberpublic abstract class Nu,23,Number,byte,byteValue,(),Returns the value of the specified number as a,byte,.,abstract double,doubleValue,(),Returns the value of the specified number as a,double,.,abstract float,floatValue,(),Returns the value of the specified number as a,float,.,abstract int,intValue,(),Returns the value of the specified number as an,int,.,abstract long,longValue,(),Returns the value of the specified number as a,long,.,short,shortValue,(),Returns the value of the specified number as a,short,.,NumberbytebyteValue(),24,Float,java.lang Class Float,java.lang.Object,java.lang.Number,java.lang.Float,All Implemented Interfaces:,Comparable,Serializable,Floatjava.lang Class Float,25,Float,public final class Float,extends,Number,implements,Comparable,The,Float,class wraps a value of primitive type,float,in an object. An object of type,Float,contains a single field whose type is,float,.,In addition, this class provides several methods for converting a,float,to a,String,and a,String,to a,float, as well as other constants and methods useful when dealing with a,float,.,Floatpublic final class Float,26,Float,static float,MAX_VALUE,A constant holding the largest positive finite value of type,float, (2-2,-23,)2,127,.,staticfloat,MIN_VALUE,A constant holding the smallest positive nonzero value of type,float, 2,-149,.,staticfloat,NaN,A constant holding a Not-a-Number (NaN) value of type,float,.,staticfloat,NEGATIVE_INFINITY,A constant holding the negative infinity of type,float,.,staticfloat,POSITIVE_INFINITY,A constant holding the positive infinity of type,float,.,static,Class,TYPE,The,Class,instance representing the primitive type,float,.,Floatstatic float MAX_VALUE,27,4 基础数据结构,链表LinkedList,堆栈Stack,向量Vector,树TreeSet,哈希表Hashtable,4 基础数据结构链表LinkedList,28,Iterator接口的作用,public Interface Iterator,Boolean,hasNext,(),Returns true if the iteration has more elements.,Object,next,(),Returns the next element in the iteration.,void,remove,(),Removes from the underlying collection the last element returned by the iterator (optional operation).,Iterator接口的作用public Interface,29,LinkedList示例,import java.util.*;class StudentString name ;int number;float score;Student(String name,int number,float score)this.name=name;this.number=number;this.score=score;public class LinkListThreepublic static void main(String args) LinkedList mylist=new LinkedList();Student stu_1=new Student(赵好民 ,9012,80.0f),stu_2=new Student(钱小青 ,9013,90.0f), stu_3=new Student(孙力枚 ,9014,78.0f),stu_4=new Student(周左右 ,9015,55.0f);mylist.add(stu_1); mylist.add(stu_2);mylist.add(stu_3);mylist.add(stu_4);Iterator iter=mylist.iterator();while(iter.hasNext() Student te=(Student)iter.next();System.out.println(te.name+ +te.number+ +te.score);,LinkedList示例 import java.ut,30,Stack示例,import java.util.*;class StackOne,public static void main(String args)Stack mystack=new Stack();mystack.push(new Integer(1); mystack.push(new Integer(2);mystack.push(new Integer(3); mystack.push(new Integer(4);mystack.push(new Integer(5); mystack.push(new Integer(6);while(!(mystack.empty(),Integer temp=(Integer)mystack.pop(); System.out.print( +temp.toString();,Stack示例 import java.util.*;,31,Vector示例,import java.util.*;class Examplepublic static void main(String args) Vector vector=new Vector(); Date date=new Date();vector.add(new Integer(1);vector.add(new Float(3.45f); vector.add(new Double(7.75);vector.add(new Boolean(true);vector.add(date);System.out.println(vector.size();Integer number1=(Integer)vector.get(0);System.out.println(向量的第1个元素: +number1.intValue();Float number2=(Float)vector.get(1);System.out.println(向量的第2个元素: +number2.floatValue();Double number3=(Double)vector.get(2);System.out.println(向量的第3个元素: +number3.doubleValue();Boolean number4=(Boolean)vector.get(3);System.out.println(向量的第4个元素: +number4.booleanValue();date=(Date)vector.lastElement();System.out.println(向量的第5个元素: +date.toString(); if(vector.contains(date)System.out.println(ok); ,Vector示例 import java.util.*,32,TreeSet示例,import java.util.*;class TreeOnepublic static void main(String args),TreeSet mytree=new TreeSet();mytree.add(boy);mytree.add(zoo);mytree.add(apple); mytree.add(girl);Iterator te=mytree.iterator();while(te.hasNext()System.out.println(+te.next();,输出结果是:appleboygirlzoo,TreeSet示例 import java.util.*,33,Hashtable示例,import java.util.*;class Student int english=0; String name,number;Student(String na,String nu,int e)english=e;name=na;number =nu;public class HT public static void main(String args) Hashtable hashtable=new Hashtable();hashtable.put(199901,new Student(199901,王小林,98);hashtable.put(199902,new Student(199902,能林茂,70);hashtable.put(199903,new Student(199903,多种林,93);hashtable.put(199905,new Student(199905,夹贸林,77);hashtable.put(199906,new Student(199906,噔林可,55);hashtable.put(199908,new Student(199908,纠林咯,76);Student stu=(Student)hashtable.get(199902);/检索一个元素。 System.out.println(stu.number+ +stu.name+ +stu.english);hashtable.remove(199906); /删除一个元素System.out.println(散列表中现在含有:+hashtable.size()+个元素);Enumeration enum=hashtable.elements(); while(enum.hasMoreElements() /遍历当前散列表。Student s=(Student)enum.nextElement();System.out.println(s.number+ +s.name+ +s.english);,Hashtable示例import java.util.*;,34,作业,设计一个能自动测试排序算法性能(比较次数compare_count、交换次数exchange_count、探测次数probe_count)的类体系。,要求:用一个类来描述一个排序算法,类中的sort方法通过调用比较、交换方法来实现数组排序。,作业设计一个能自动测试排序算法性能(比较次数compare_,35,作业详细说明,(1)写一个final参数类M,包括比较次数、交换次数、探测次数属性,并重写构造器和toString方法。,(2)写一个抽象类A,其中包括要排序的数据。提供三个final方法,分别完成比较、探测、交换操作的同时,正确改变私有的M类对象成员的相关属性。并提供一个虚方法doSort,同时提供一个final方法sort(先设置M对象初值,然后调用doSort方法,返回M对象引用),(3)写三个采用不同方法排序的A类的派生类A1,A2,A3,(4)写一个测试类作为主类,分别生成A1,A2,A3的对象并调用sort方法,显示三个方法在排序时候的性能参数。,(5)谈谈这种类设计的合理性以及可以改进之处。,(6)*,试验设计一个可计时接口来改进本程序,并谈谈你的想法。,要求交书面作业。,且必须老师再机房检查代码后才有效。,作业详细说明(1)写一个final参数类M,包括比较次数、交,36,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > PPT模板库


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

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


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