资源预览内容
第1页 / 共46页
第2页 / 共46页
第3页 / 共46页
第4页 / 共46页
第5页 / 共46页
第6页 / 共46页
第7页 / 共46页
第8页 / 共46页
第9页 / 共46页
第10页 / 共46页
亲,该文档总共46页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
面向对象编程,1,4.2.2 对象初始化-静态调用,public class ObjectCreation TestClass testClass = new TestClass(“fieldValue“); static TestClass testClass2 = new TestClass(“staticValue“); static testClass2.test(); public ObjectCreation() System.out.println(“ObjectCreation init“); public ObjectCreation(String name) System.out.println(“ObjectCreation “ + name + “ init“); ,面向对象编程,2,public static void main(String args) ObjectCreation objectCreation = new ObjectCreation(“object1“); ObjectCreation objectCreation2 = new ObjectCreation(“object2“); class TestClass public TestClass(String name) System.out.println(“TestClass “ + name + “ init“); void test() System.out.println(“makeInner() invoked“); ,面向对象编程,3,基础类和工具类,面向对象编程,5,主要内容,Java类库 语言基础类 向量类 工具类,面向对象编程,6,Java类库,Java的类库是系统提供的已实现的标准类的集合,是Java编程的API(Application Program Interface),它可以帮助开发者方便、快捷地开发Java程序。 这些系统定义好的类根据实现的功能不同,可以划分成不同的集合,每个集合是一个包,合称为类库。 Java的类库大部分是由它的发明者SUN公司 提供的,这些类库称为基础类库(JFC)。 API 应用程序编程接口 面向过程语言 函数库(子程序包) 面向对象语言 类库,面向对象编程,7,Java类库,类库的结构 java.lang 语言基础类库(System、Math、 Thread、基本数据类型类) java.util Java的工具类库(向量、栈、日期) java.io Java的标准输入输出类库 java.applet 用于实现Java Applet小程序的类库 java.awt 用于构建图形用户界面的类库 java.awt.event 界面用户交互控制和事件响应类 库 Java的用于实现网络功能的类库 使用JDK Document查看类库,面向对象编程,8,语言基础类库-Object类,Java程序中所有类的直接或间接父类,也是类库中所有类的的父类,所有的其他类都是从Object类派生。 构造方法:Object( ) 一般方法: Object clone( ) 生成当前对象的一个拷贝。 boolean equals(Object obj) 比较两个对象是否 相同。 Class getClass() 获取当前对象所属的类信息。 String toString() 用来返回当前对象本身的有关 信息。,面向对象编程,9,public class BasicObjectDemo public static void main(String args) A a1 = new A(); A a2 = new A(); A a3 = a1; B b = new B(); System.out.println(a1.equals(a2); System.out.println(a3.equals(a1); System.out.println(a1.hashCode(); System.out.println(a1.toString(); class A class B ,面向对象编程,10,实例,public class PhoneNumber private String region; private String number; public String toString() return “book.ch6.PhoneNumber“ + “region=“ + region + “ + “, number=“ + number + “ + “; ,面向对象编程,11,System类,属性 public static InputStream in public static PrintStream out public static PrintStream err 获取系统信息、完成系统操作的方法 public static long currentTimeMillis(); 获取自1970年1月1日零时至当前系统时刻的微秒数 972370687290 public static void exit(int status); 强制Java虚拟机退出运行状态,并把状态信息status 返回给运行虚拟机的操作系统。 System .exit(0); public static void gc(); 强制调用Java虚拟机的垃圾回收功能。,面向对象编程,12,语言基础类库:System类,System是一个功能强大的类,它提供了标准输入输出、运行时的系统信息等工具 系统功能类 获取系统标准输入/输出 System.in,System.out,System.err 获取系统信息 System.currentTimeMillis( ) 执行系统操作 System.exit(0); System.gc( );,面向对象编程,13,语言基础类库:数据类型类,基本数据类型 vs. 数据类型类 boolean vs Boolean, char vs Character等 数据类型类 规定了数据类型的最大值、最小值 构造函数:如new Integer(10); 完成不同数据类型间转换,注意不同的数据类使用的方法会有不同。 Double.toString(0.08)、Integer.parseInt(“123”)、 Double.ValueOf(“0.08”).intValue ()等,见JDK Doc,面向对象编程,14,语言基础类库:Math类,Math类用来完成常用的数学运算 数学常量:E,PI 数学运算 Math.abs(- 8.09); Math.exp( 5.7); Math.random(); Math.sqrt(9.08); Math.pow( 2,3); /乘方 Math.round(99.6); 均为static,使用时无需创建实例 Math.method(variable);,面向对象编程,15,数学运算的Math类,属性 public final static double E; / 数学常量e public final static double PI; / 圆周率常量 方法(均为静态方法) public static int abs(int i); public static double sin(double a); public static double log(double a); public static double max(double a, double b); public static double pow(double a, double b); public static double random(); / 产生01之间伪 随机数,面向对象编程,16,数学运算的Math类,public static double exp(double a); public static int round(float a); public static double sqrt(double a); 例: System.out.println(Math.E); 2.718281828 System.out.println(Math.PI); 3.14159265 System.out.println(Math.pow( 2,3); 8.0 System.out.println(Math.round(99.6); 100 System.out.println(Math.abs(- 8.09); 8.09,面向对象编程,17,public class StringDemo public static void main(String args) String s; s = “String test “; s = new String(“String test “); int stringLength = s.length(); System.out.println(“stringLength = “ + stringLength); boolean startTest = s.startsWith(“Str“); boolean endTest = s.endsWith(“est“); System.out.println(“startTest = “ + startTest); System.out.println(“endTest = “ + endTest); int blankIndex = s.indexOf( ); System.out.println(“blankIndex = “ + blankIndex);,字符串相关类String,面向对象编程,18,int subStringIndex = s.indexOf(“est“); System.out.println(“subStringIndex = “ + subStringIndex); int lastIndex = s.lastIndexOf(s); System.out.println(“lastIndex = “ + lastIndex); String s2 = “String test2“; int compare = pareTo(s); System.out.println(“compare = “ + compare); boolean equalTest = s2.equals(s); System.out.println(“equalTest = “ + equalTest); char singleChar = s.charAt(3); System.out.println(“singleChar = “ + singleChar);,面向对象编程,19,String subString = s.substring(3); System.out.println(“subString = “ + subString); String trimString = s.trim(); System.out.println(“trimString = “ + trimString + “); String upperCase = s.toUpperCase(); System.out.println(“upperCase = “ + upperCase); String lowerCase = s.toLowerCase(); System.out.println(“lowerCase = “
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号