400 likes | 542 Views
JAVA 程序设计. 主要内容. Java 可以做什么? 介绍对象 Primitive Types, Wrappers, and Boxing 声明和注释 构造器和可见性 Static, Final, and 枚举类型 命名,操作和精度 继承 异常 接口. Java 可以做什么?. Java 可以做什么? 介绍对象 Primitive Types, Wrappers, and Boxing 声明和注释 构造器和可见性 Static, Final, and 枚举类型 命名,操作和精度 继承 异常 接口. 跨平台.
E N D
主要内容 • Java可以做什么? • 介绍对象 • Primitive Types, Wrappers, and Boxing • 声明和注释 • 构造器和可见性 • Static, Final, and 枚举类型 • 命名,操作和精度 • 继承 • 异常 • 接口
Java可以做什么? • Java可以做什么? • 介绍对象 • Primitive Types, Wrappers, and Boxing • 声明和注释 • 构造器和可见性 • Static, Final, and 枚举类型 • 命名,操作和精度 • 继承 • 异常 • 接口
易用性 • Threads • Exceptions • Garbage collection
run-time library • java.lang • java.io • java.net • java.util • java.util.regex • java.sql
Java平台 • J2ME • J2SE • J2EE
介绍对象 • Java可以做什么? • 介绍对象 • Primitive Types, Wrappers, and Boxing • 声明和注释 • 构造器和可见性 • Static, Final, and 枚举类型 • 命名,操作和精度 • 继承 • 异常 • 接口
对象和类 • 类:人 • 对象:张三 • 对象是类的一个实例
属性和方法 • 属性(人.name) • 方法(人.eat()) • Demo(person1)
Primitive Types, Wrappers, and Boxing • Java可以做什么? • 介绍对象 • Primitive Types, Wrappers, and Boxing • 声明和注释 • 构造器和可见性 • Static, Final, and 枚举类型 • 命名,操作和精度 • 继承 • 异常 • 接口
Primitive Types • boolean (for true/false values) • char (for character data, ultimately to be input or printed) • int, long, byte, short (for arithmetic on whole numbers) • double, float (for arithmetic on the real numbers)
Wrappers • boolean java.lang.Boolean • char java.lang.Character • int java.lang.Integer • long java.lang.Long • byte java.lang.Byte • short java.lang.Short • double java.lang.Double • float java.lang.Float
Autoboxing and Unboxing int i = 27; Integer myInt = i; // autobox! Double dObj = 27.0; // autobox double d = dObj; // unbox, gets value 27.0
java.lang.Object • public java.lang.Object(); // constructor public • java.lang.String toString(); • public boolean equals(java.lang.Object); • public native int hashCode(); • public final Class getClass(); • protected native java.lang.Object clone() throws CloneNotSupportedException; // methods relating to thread programming • public final native void notify(); • public final void wait() throws InterruptedException;
java.lang.String • String drinkPref = new String( "I like tea" ); • String drinkPref = "I like tea"; • String s = "ABCD".toLowerCase();
声明和注释 • Java可以做什么? • 介绍对象 • Primitive Types, Wrappers, and Boxing • 声明和注释 • 构造器和可见性 • Static, Final, and 枚举类型 • 命名,操作和精度 • 继承 • 异常 • 接口
声明和注释 • if,else,while,for • 注释 • // This is a comments • /* This is a lot of comments */ • /** This is a lot of comments */
构造器和可见性 • Java可以做什么? • 介绍对象 • Primitive Types, Wrappers, and Boxing • 声明和注释 • 构造器和可见性 • Static, Final, and 枚举类型 • 命名,操作和精度 • 继承 • 异常 • 接口
构造器 • Demo(Timestamp)
import and Package • Package • import
可见性 • private • protected • public
Static, Final, and 枚举类型 • Java可以做什么? • 介绍对象 • Primitive Types, Wrappers, and Boxing • 声明和注释 • 构造器和可见性 • Static, Final, and 枚举类型 • 命名,操作和精度 • 继承 • 数据 • 异常 • 接口
What you can make static • Data • Methods • Blocks • Classes
What Field Modifier final Means • Data • Methods • Classes
枚举类型 enum Bread { wholewheat, ninegrain, rye, french } Bread todaysLoaf; todaysLoaf = Bread.rye;
命名,操作和精度 • Java可以做什么? • 介绍对象 • Primitive Types, Wrappers, and Boxing • 声明和注释 • 构造器和可见性 • Static, Final, and 枚举类型 • 命名,操作和精度 • 继承 • 异常 • 接口
operators • ++ -- • ~ • ! • - + * / % = *= /= %= += -= • << >> >>> • instanceof • <= >< >= • == != • & ^ | • && || ? : • <<= >>= >>>= • &= ^= |=
继承 • Java可以做什么? • 介绍对象 • Primitive Types, Wrappers, and Boxing • 声明和注释 • 构造器和可见性 • Static, Final, and 枚举类型 • 命名,操作和精度 • 继承 • 异常 • 接口
继承 • 动物(哺乳动物(猫,狗)) • Demo(animal)
异常 • Java可以做什么? • 介绍对象 • Primitive Types, Wrappers, and Boxing • 声明和注释 • 构造器和可见性 • Static, Final, and 枚举类型 • 命名,操作和精度 • 继承 • 异常 • 接口
接口 • Java可以做什么? • 介绍对象 • Primitive Types, Wrappers, and Boxing • 声明和注释 • 构造器和可见性 • Static, Final, and 枚举类型 • 命名,操作和精度 • 继承 • 异常 • 接口
接口 • 仅仅定义服务和功能 • 干净的(没有任何具体实现) • 一个类可以实现多个接口 • 接口可以继承接口 • 最低限度耦合
一些原则 • 面向接口编程 • 方法的参数要用接口
Interface java.lang.Comparable public interface Comparable { public int compareTo(Object o); }
作业 • Why use object wrappers instead of primitive types directly?