




已阅读5页,还剩46页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 接口名 成员变量; 方法声明; ,class 类名 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 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 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 将多个接口合并为一个新的接口,interface 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; 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 ActionCharacter 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 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 RainbowColors 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.out.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 classes and interfaces providing access protection and namespace management. import,6.2 包 (package),10,/Graphic.java file public abstract class Graphic . . . /Circle.java file public class Circle extends Graphic implements Draggable . . . /Rectangle.java file public class Rectangle extends Graphic implements Draggable . . . /Draggable.java file public interface Draggable . . . ,容易地决定那些类和接口是相互关联的 知道从哪里找到提供图形功能的类和接口 由于包建立了一个新的名字空间,所以你定义的类不会同其他包中的类名有冲突 可以容许在同一个包中无访问限制,同时可对在本包外的访问进行限制,11,包的创建 package graphics; public class Circle extends Graphic implements Draggable . . . 包的命名 层次结构对应实际的目录结构 com.sun. org.w3c. org.jalpha.,6.2.1 包的创建,12,包的使用 仅仅公共的(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(); 引入(import)整个包成员 import graphics.*; Circle myCir = new Circle();,6.2.3 包的引入,14,如何防止名字冲突 /graphics.Circle.class package graphics; public class Circle . . . /mygraphics.Circle.class 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 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:src javac orgjalphaHelloWorld.java class文件 d:srcorgjalphaHelloWorld.class 执行(在根目录) cd d:src java org.jalpha.HelloWorld 执行(在其他目录)d: java classpath d:src org.jalpha.HelloWorld,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:src1;d:src2;. org.aloha.Test 执行(在其他目录) d: java classpath d:src1;d:src2;d:src org.aloha.Test,17,6.3 常用工具类,Java 2 Platform Packages JavaTM 2 Platform, Standard Edition, v 1.4.1 API Specification Java语言基础类、Java类库 定义描述见Java2SDK文档 135个包(package) java.applet; java.awt; java.awt.event java.io; java.lang; java.sql; java.text; java.util javax.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类 System类 Math类 基本数据类型的包装类 字符串操作类 String类 StringBuffer类 StringTokenizer类(java.util包) Runtime类,19,6.3.2 java.lang.Object类,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 Object clone() throws CloneNotSupportedException public boolean equals(Object obj) protected void finalize() throws Throwable public final Class getClass() public int hashCode() public final void notify() public final void notifyAll() public String toString() public final void wait() throws InterruptedException public final void wait(long timeout) throws InterruptedException public final void wait(long timeout, int nanos) throws InterruptedException,20,6.3.3 java.lang.System类,静态变量 public static final InputStream in (标准输入流) public static final PrintStream out (标准输出流) public static final PrintStream err (标准错误输出流) 静态方法 public static void 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, January 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 java.lang.Math类,静态变量 public static final double E public static final double PI 静态方法 public static double abs(double a) public 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,常数定义,byte largestByte = Byte.MAX_VALUE; /127 short largestShort = Short.MAX_VALUE; /32767 int largestInteger = Integer.MAX_VALUE; /2147483647 long largestLong = Long.MAX_VALUE; /9223372036854775807 float largestFloat = Float.MAX_VALUE; /3.40282e+38 double largestDouble = Double.MAX_VALUE; /1.79769e+308,25,基本类型和字符串(String)之间的转换 Integer类举例 字符串转换为整数 public static int parseInt(String s) throws NumberFormatException public 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(“2147483647“, 10) parseInt(“-2147483648“, 10) parseInt(“2147483648“, 10) parseInt(“99“, 8) parseInt(“Kona“, 10) parseInt(“Kona“, 27),returns 0 returns 473 returns 0 returns -255 returns 102 returns 2147483647 returns -2147483648 throws a NumberFormatException throws a NumberFormatException throws a NumberFormatException returns 411787,26,基本类型和字符串(String)之间的转换 Integer类举例 整数转换为字符串 public static String toString(int i) public static String toString(int i, int radix) public static String toBinaryString(int i) public static String toHexString(int i) public static String toOctalString(int i),int i = 123; String s1 = Integer.toString(i); String s2 = Integer.toString(i, 10); String s3 = Integer.toString(i, 2); String s4 = Integer.toString(i, 8); String s5 = Integer.toString(i, 16); String s6 = Integer.toBinaryString(i); String s7 = Integer.toHexString(i); String s8 = Integer.toOctalString(i);,123 123 1111011 173 7b 1111011 7b 173,27,6.3.6 字符串操作类,三个类 java.lang.String类 java.lang.StringBuffer类 java.util.StringTokenizer类 不同的应用场合,28,java.lang.String类字符串/字符序列 构造方法 public String() public String(byte bytes) public String(byte bytes, int offset, int length) public String(byte bytes, String charsetName) public String(char value) public String(char value, int offset, int count) public String(String original),29,java.lang.String类字符串/字符序列 构造方法的使用,String s = new String(); char c = a, b, c; String s = new String(c); char c = 语, 言; String s = new String(c);,byte b = 97, 98, 99; String s = new String(b); String s = “abc”; String s = “语言”;,30,java.lang.String类字符串/字符序列 判断字符串相等的方法 public boolean equals(Object anObject) 判断是否是同一个对象,是ture;然后anObject是否为一个字符串对象,否false;判断是否内容相同 public boolean equalsIgnoreCase(String anotherString) 判断逻辑与equals相同,仅仅在判断内容上不考虑大小写 public int compareTo(Object o) 若o不是字符串对象,则抛错;若是则调用下面的函数 public int compareTo(String anotherString) 判断两个字符串的内容是否相同,是0;否不等于0的整数 public int compareToIgnoreCase(String str) 基本功能同上,仅仅在判断时不考虑大小写,31,java.lang.String类字符串/字符序列 判断字符串相等的方法,String s1 = “java语言“; String s2 = “JavA语言“; System.out.println(s1.equals(s2); System.out.println(s1.equalsIgnoreCase(s2); System.out.println(pareTo(s2); System.out.println(pareToIgnoreCase(s2);,运行结果: false true 32 0,32,java.lang.String类字符串/字符序列 获取长度 public int length() 字符串的长度,即包含多少个字符 获取特定子串(substring)和字符 public String substring(int beginIndex) public String substring(int beginIndex, int endIndex) beginIndex: 起始索引位置(包含) endIndex: 结束索引位置(不包含) public char charAt(int index),33,java.lang.String类字符串/字符序列 方法举例,String s1 = “java语言“; String s2 = “JavA语言“; System.out.println(s1.length(); System.out.println(s2.length(); System.out.println(s1.substring(0, 4); System.out.println(s1.substring(4); System.out.println(s2.substring(0, 4); System.out.println(s2.substring(4); System.out.println(s1.charAt(0);,运行结果: 6 6 java 语言 JavA 语言 j,34,java.lang.String类字符串/字符序列 字符串前缀(prefix)/后缀(suffix)的判断 public boolean startsWith(String prefix) 判断字符串是否以一特定的字符串开头 public boolean startsWith(String prefix, int toffset) public boolean endsWith(String suffix) 判断字符串是否以一特定的字符串开头,System.out.println(“java语言“.startsWith(“java“); System.out.println(“java语言“.startsWith(“ava“, 1); System.out.println(“java语言“.endsWith(“语言“);,35,java.lang.String类字符串/字符序列 查询特定字符/字符串的位置 public int indexOf(int ch) 该字符在字符串中第一次出现位置的索引值;否则返回-1 public int indexOf(int ch, int fromIndex) public int indexOf(String str) public int indexOf(String str, int fromIndex) public int lastIndexOf(int ch) public int lastIndexOf(int ch, int fromIndex) public int lastIndexOf(String str) public int lastIndexOf(String str, int fromIndex),36,java.lang.String类字符串/字符序列 方法举例,String s = “java语言”; System.out.println(s.indexOf(a); System.out.println(s.indexOf(a, 2); System.out.println(s.indexOf(“a”); System.out.println(s.indexOf(“语言”); System.out.println(s.lastIndexOf(a); System.out.println(s.lastIndexOf(v, 1); System.out.println(s.lastIndexOf(“语言”); System.out.println(s.lastIndexOf(“v”, 2);,运行结果: 1 3 1 4 3 -1 4 2,37,java.lang.String类字符串/字符序列 字符串转变为数组 public byte getBytes() 将字符串转变为一个字节数组 public byte getBytes(String charsetName) throws UnsupportedEncodingException 按特定的字符编码格式将字符串转变为一个字节数组 public char toCharArray() 将字符串转变为一个字符数组,38,java.lang.String类字符串/字符序列 方法举例,String s = “java语言“; char c = s.toCharArray(); System.out.println(c.length); byte b = s.getBytes(); System.out.println(b.length); b = s.getBytes(“ISO8859-1“); System.out.println(b.length);,运行结果: 6 8 6,中文Windows操作系统: 默认字符集 GB2312 其他系统: 默认字符集 ISO-8859-1,39,java.lang.String类字符串/字符序列 字符串 public String split(String regex),40,java.lang.String类字符串/字符序列 其他方法 public String concat(String str) 连接字符串 “cares“.concat(“s“) returns “caress“ “to“.concat(“get“).concat(“her“) returns “together“ public String replace(char oldChar, char newChar) 在字符串中进行字符替换 “mesquite in your cellar“.replace(e, o) returns “mosquito in your collar” “JonL“.replace(q, x) returns “JonL“ (no change),41,java.lang.String类字符串/字符序列 其他方法 public String trim() 将字符串头尾的空格字符删除 public String toLowerCase() 字符串中字符转为小写 public String toUpperCase() 字符串中字符转为大写 一些静态方法 public static String valueOf(boolean b) public static String valueOf(char c) public static String valueOf(int i) public static String valueOf(long l) public static String valueOf(float f) public static String valueOf(double d),42,java.lang.String类字符串/字符序列 Quiz,String s1 = new String(“java”); String s2 = new String(“java”); System.out.println(s1 = s2); System.out.println(s1.equals(s2); System.out.println(pareTo(s2);,运行结果: false true 0 true true 0 false true 0,String s3 = “java”; String s4 = “java”; System.out.println(s3 = s4); System.out.println(s3.equals(s4); System.out.println(pareTo(s4);,System.out.println(s2 = s4); System.out.println(s2.equals(s4); System.out.println(pareTo(s4);,结论: String s1 = “abc”; 字符串常量对象 (immutable) String s2 = “abc”; 不同常量对象共享同一对象 其他字符串对象则可理解为对字符串常量对象进行了 一层包装,System.out.println(ern() = s4);,true,43,java.lang.String类字符串/字符序列 Quiz,package testPackage; class Test public static void main(String args) String hello = “Hello“, lo = “lo“; System.out.println(hello = “Hello“); System.out.println(Other.hello = hello); System.out.println(other.Other.hello = hello); System.out.println(hello = (“Hel“+“lo“); System.out.println(hello = (“Hel“+lo); System.out.println(hello = (“Hel“+lo).intern(); class Other static String hello = “Hello“; ,运行结果: true true true true false true,package other; public class Other static String hello = “Hello“; ,结论: Strings computed by constant expressions are computed at compile time and then treated as if they were literals. Strings computed at run time are newly created and therefore distinct.,44,java.lang.StringBuffer类 一个可变(mutable)/动态的字符序列 构造方法 public StringBuffer() public StringBuffer(String str) 主要方法 添加(append)和插入(insert, 指定位置) public StringBuffer append(boolean b) public StringBuffer insert(int offset, boolean b) boolean, char, char, double, float, int, long, String 转换为字符串 - public String toString(),45,java.lang.StringBuffer类方法举例,String s = “java语言“; StringBuffer buffer = new StringBuffer(s); buffer.append(“easy”); buffer.insert(6, “ is “); s = buffer.toString(); System.o
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 西方古代的管理思想
- 培训机构销售经理岗位职责与技能
- 城市交通规划合同管理成本控制咨询重点基础知识点
- 筑牢消防安全防线:多场景消防培训实践
- 地下体育场馆规划重点基础知识点
- 国防安全意识
- 转让赠予买卖合同协议
- 《我国军队军衔制度概述》课件
- 民房工伤协议书
- 水电验收协议书
- 物品出入库登记表
- GB/T 11253-2019碳素结构钢冷轧钢板及钢带
- GB/T 10125-2012人造气氛腐蚀试验盐雾试验
- 大学生手机市场的调查报告
- 商务标评审表
- 2021版《安全生产法》培训课件
- 大学语文说课课件
- 大连理工大学画法几何自学片段课件
- 慢性心功能不全护理查房
- 双新转常规申请表
- 相关方需求和期望及风险和机遇措施表
评论
0/150
提交评论