《面向对象特征》PPT课件.ppt
《《面向对象特征》PPT课件.ppt》由会员分享,可在线阅读,更多相关《《面向对象特征》PPT课件.ppt(51页珍藏版)》请在三一办公上搜索。
1、1,第6讲 面向对象特征(2),武汉大学国际软件学院,2,接口是对abstract类的进一步扩展接口中的方法都是未实现的(类似于抽象方法),目的是在实现接口的类之间建立一种协议接口中的变量都是常量定义一个类符合某个或一组接口,利用implements,6.1 接口(interface),An interface is a named collection of method definitions(without implementations).An interface can also declare constants.,public interface 接口名 成员变量;方法声明;,c
2、lass 类名 implements 接口1,接口2,3,接口名修饰public:无任何访问限制无修饰:仅限于本包中接口变量默认都是“public static final”public interface Months int JANUARY=1,FEBRUARY=2,MARCH=3,APRIL=4,MAY=5,JUNE=6,JULY=7,AUGUST=8,SEPTEMBER=9,OCTOBER=10,NOVEMBER=11,DECEMBER=12;,6.1.1 接口名修饰,4,6.1.2 接口方法,接口方法无修饰但在实现接口方法的类中,修饰符为public,interface Figure
3、 double half=0.5,pi=3.14159;void parameter();void area();,class Triangle implements Figure double b,h;Triangle(double u,double v)b=u;h=v;public void parameter()System.out.println(b+“+h);public void area()System.out.println(half*h*b);,class Circle implements Figure double x,y,r;Circle(double u,double
4、 v,double m)x=u;y=v;r=m;public void parameter()System.out.println(x+“+y+“+r);public void area()System.out.println(pi*r*r);,Triangle t=new Triangle(2,3);Circle c=new Circle(4,5,6);Figure f=t,c;for(int i=0;i f.length;i+)fi.parameter();fi.area();,5,6.1.3 接口的继承 extends,接口的继承 extends将多个接口合并为一个新的接口,interf
5、ace DC int add(int x,int y);interface DB extends DC int sub(int x,int y);interface DA extends DB int mul(int x,int y);,interface DY int div(int x,int y);interface DX extends DY int mod(int x,int y);,class DD implements DA,DX public int add(int x,int y)return x+y;public int sub(int x,int y)return x-y
6、;public int mul(int x,int y)return x*y;public int div(int x,int y)return x/y;public int mod(int x,int y)return x%y;,6,6.1.4 接口多重继承,利用接口实现多重继承(Multiple inheritance)class extends 父类 implements 接口,interface CanFight void fight();interface CanSwim void swim();interface CanFly void fly();,class ActionCha
7、racter public void fight(),class Hero extends ActionCharacter implements CanFight,CanSwim,CanFly public void swim()public void fly(),7,6.1.5 接口合并时的命名冲突,interface A1 void f();interface A2 int f(int i);interface A3 int f();,class C public int f()return 4;,class C1 implments A1,A2 public void f()public
8、 int f(int i)return 5;,class C2 extends C implments A2 public int f(int i)return 5;,class C3 extends C implments A3 public int f()return 5;,class C4 extends C implements A1,/overload,/overload,/implements and overriding,8,6.1.6 接口继承中的命名冲突,interface BaseColors int RED=1,GREEN=2,BLUE=4;interface Rainb
9、owColors extends BaseColors int YELLOW=3,ORANGE=5,VIOLET=6;interface PrintColors extends BaseColors int YELLOW=8,CYAN=16,MAGENTA=32;interface LotsOfColors extends RainbowColors,PrintColors int FUCHSIA=17,CHARTREUSE=RED+90;,class DD implements LotsOfColors public static void main(String args)System.o
10、ut.println(YELLOW);,class DD public static void main(String args)System.out.println(LotsOfColors.YELLOW);,reference to YELLOW is ambiguous,both variable YELLOW in RainbowColors and variable YELLOW in PrintColors match,9,为什么需要包?使Java类更容易发现和使用防止命名冲突进行访问控制 层次结构,特定的功能A package is a collection of related
11、 classes and interfaces providing access protection and namespace management.import,6.2 包(package),10,/Graphic.java filepublic abstract class Graphic./Circle.java filepublic class Circle extends Graphic implements Draggable./Rectangle.java filepublic class Rectangle extends Graphic implements Dragga
12、ble./Draggable.java filepublic interface Draggable.,容易地决定那些类和接口是相互关联的知道从哪里找到提供图形功能的类和接口由于包建立了一个新的名字空间,所以你定义的类不会同其他包中的类名有冲突可以容许在同一个包中无访问限制,同时可对在本包外的访问进行限制,11,包的创建 package graphics;public class Circle extends Graphic implements Draggable.包的命名层次结构对应实际的目录结构com.sun.org.w3c.org.jalpha.,6.2.1 包的创建,12,包的使用仅
13、仅公共的(public)包成员(类、接口)可以在其所定义的包外被访问三种方式利用包成员的规范名(包名+类名)引入(import)包成员名引入(import)整个包成员,6.2.2 包的使用,13,例 package graphics;public class Circle extends Graphic implements Draggable.利用包成员的规范名(包名+类名)graphics.Circle myCir=new graphics.Circle();引入(import)包成员名import graphics.Circle;Circle myCir=new Circle();引入(
14、import)整个包成员import graphics.*;Circle myCir=new Circle();,6.2.3 包的引入,14,如何防止名字冲突package graphics;public class Circle./package mygraphics;public class Circle.,6.2.4 防止名字冲突,import graphics.*;import mygraphics.*;class Test/Circle c;,import graphics.*;import mygraphics.*;class Test graphics.Circle c=new
15、graphics.Circle();mygraphics.Circle c=new mygraphics.Circle();,15,6.2.5 包与Java文件的对应关系,package org.jalpha;public class HelloWorld.,根目录d:src源文件d:srcorgjalphaHelloWorld.java 编译cd d:srcjavac orgjalphaHelloWorld.javaclass文件d:srcorgjalphaHelloWorld.class 执行(在根目录)cd d:src java 执行(在其他目录)d:java classpath d:s
16、rc,16,6.2.5 包与Java文件的对应关系,package org.aloha;import org.jalpha.*;import org.w3c.*;public class Test.,class文件(org.jalpha)d:src1orgjalpha*.class class文件(org.w3c)d:src2orgw3c*.class Test.class文件的根目录 d:src执行(在根目录)d:src java classpath执行(在其他目录)d:java classpath,17,6.3 常用工具类,Java 2 Platform PackagesJavaTM 2
17、Platform,Standard Edition,v 1.4.1 API SpecificationJava语言基础类、Java类库定义描述见Java2SDK文档135个包(package)java.applet;java.awt;java.io;java.lang;java.sql;java.text;java.utiljavax.swing 2723个类(class)和接口(interface)实现d:j2sdk1.4.1_01jrelibrt.jar(22.4MB)jar tvf rt.jar|more,18,6.3.1 java.lang包,java.lang包Object类Syst
18、em类Math类基本数据类型的包装类字符串操作类String类StringBuffer类StringTokenizer类(java.util包)Runtime类,19,6.3.2 类,Class Object is the root of the class hierarchy.Every class has Object as a superclass.All objects,including arrays,implement the methods of this class.子类可以对Object类的方法进行重写,构造方法-public Object(),实例方法protected O
19、bject clone()throws CloneNotSupportedExceptionpublic boolean equals(Object obj)protected void finalize()throws Throwablepublic final Class getClass()public int hashCode()public final void notify()public final void notifyAll()public String toString()public final void wait()throws InterruptedException
20、public final void wait(long timeout)throws InterruptedExceptionpublic final void wait(long timeout,int nanos)throws InterruptedException,20,6.3.3 类,静态变量public static final InputStream in(标准输入流)public static final PrintStream out(标准输出流)public static final PrintStream err(标准错误输出流)静态方法public static voi
21、d arraycopy(Object src,int srcPos,Object dest,int destPos,int length)public static void exit(int status)public static void gc()public static long currentTimeMillis(),21,获取当前时间public static long currentTimeMillis()Returns:the difference,measured in milliseconds,between the current time and midnight,J
22、anuary 1,1970 UTC(Universal time coordinated).,public static void main(String args)long start=System.currentTimeMillis();long end=System.currentTimeMillis();System.out.println(end-start);,22,6.3.4 类,静态变量public static final double Epublic static final double PI静态方法public static double abs(double a)pu
23、blic static double ceil(double a)public static double floor(double a)public static double max(double a,double b),23,6.3.5 基本数据类型的包装类,基本数据类型:byte,short,int,long,float,double,boolean,char 对应的包装类:Byte,Short,Integer,Long,Float,Double,Boolean,Character作用包装类对象中包含有一个对应基本类型的值提供有基本类型和字符串(String)之间的转换函数定义有一些常
24、数和方法,24,常数定义,byte largestByte=Byte.MAX_VALUE;/127short largestShort=Short.MAX_VALUE;/32767int largestInteger=Integer.MAX_VALUE;/2147483647long largestLong=Long.MAX_VALUE;/9223372036854775807float largestFloat=Float.MAX_VALUE;/3.40282e+38double largestDouble=Double.MAX_VALUE;/1.79769e+308,25,基本类型和字符串
25、(String)之间的转换Integer类举例字符串转换为整数public static int parseInt(String s)throws NumberFormatExceptionpublic static int parseInt(String s,int radix)throws NumberFormatException,String s=“123”;int i=Integer.parseInt(s);,parseInt(0,10)parseInt(473,10)parseInt(-0,10)parseInt(-FF,16)parseInt(1100110,2)parseInt
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向对象特征 面向 对象 特征 PPT 课件

链接地址:https://www.31ppt.com/p-5619409.html