package package com bruceeckel simple public class List
包( package)(续) 包名可以有层次,以小数点分割。包名 一般全小写; package com. bruceeckel. simple; public class List {… … } C: DOCJava. TcombruceeckelsimpleList. java n Java 语言编程技术 9
包( package)(续) n CLASSPATH环境变量,指定搜寻包的 路径。 set classpath = C: DOCJava. T set classpath =. ; C: DOCJava. T; c: javacls. jar Java 语言编程技术 10
名字冲突 n n 若通过*导入了两个库,而且它们包括相同的 名字,就会产生名字冲突。 如: import com. bruceeckel. util. *; import java. util. *; … Vector v = new Vector(); 解决办法: java. util. Vector v = new java. util. Vector(); Java 语言编程技术 13
组合 n 在一个类里定义其它类的对象句柄 public class Sprinkler. System { private String valve 1, valve 2; Water. Source source; … } Java 语言编程技术 18
Polymorphism n Music. java Instrument void play() public class Music { Wind public static void tune(Instrument i) { void play() i. play(Note. MIDDLE_C); } public static void main(String[] args) { Wind flute = new Wind(); tune(flute); // Upcasting } Java 语言编程技术 22 } ///: ~
Polymorphism(续) n Music 2. java Instrument void play() Wind Stringed Brass void play() Java 语言编程技术 23
Polymorphism(续) n Music 3. java Java 语言编程技术 24
Polymorphism(续) n 组合与继承:Transmogrify. java Stage a Actor a; Abstract Actor act() change() go() a. act(); Happy. Actor Sad. Actor act() Java 语言编程技术 25
Run-time Type Identification n RTTI. java Java 语言编程技术 28
实例类型的判断 public void method(Actor a){ if( a instanceof Happy. Actor) { Happy. Actor h = (Happy. Actor) a; …… } else if (a instanceof Sad. Actor) { Sad. Actor s = (Sad. Actor) a; …… } …… Java 语言编程技术 } 29
//已有两个类Circle和Rectangle, 完成相关参数的 计算 class Circle { public int r; Circle(int r) {this. r=r} public int area() {return 3. 14*r*r; } } n Java 语言编程技术 31
class Rectange { public int width, height; Rectangle (int w, int h) { width=w, height=h; } public int area() { return width*height; } Java 语言编程技术 } 32
abstract class Shape { abstract float area(); } class Circle extends Shape { public int r; Circle(int r) {this. r=r; } public float area() { return 3. 14*r*r; } } Java 语言编程技术 34
class Square extends Shape { public int width, height; Rectangle (int w, int h){ width=w, height=h; } public float area() {return width*height; } } Java 语言编程技术 35
interface 接口(续) n 接口的定义 public interface 接口名 [extends 父接口名列表] { // 常量域声明 public static final 域类型 域名 = 常量值; // 抽象方法声明 public abstract 返回值类型 方法名( 参数列表 ); } n 接口的实现(implement) class Class. Name implements Interface. Name Java 语言编程技术 37
interface 接口(续) n 例:Music 5. java Java 语言编程技术 38
interface 接口(续) 例 : Adventure. java n instanceof 运算符可用来判断一个对象的 类是否实现了某个接口。 n Action. Character fight () Can. Fight fight() Can. Swim swim () Can. Fly fly () Hero fight() swim () fly () Java 语言编程技术 41
interface 接口(续) 名字冲突 Interface. Collision. java n 接口的继承 Horror. Show. java u 可以多重继承 n Java 语言编程技术 42
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; } // Java 语言编程技术 44
内部类(续) n 例:Parcel 1. java public class Parcel 1 { class Contents { ……. . . } class Destination { ……… } ………. } Parcel 1类中定义了 两个inner classes: Contents 和 Destination Java 语言编程技术 46
内部类(续) public class Parcel 2 { class Contents { //inner class 1 private int i ; Contents(int cn) { i=cn; } public int value() { return i; } } class Destination { //inner class 2 private String label; Destination(String where. To) { label = where. To; } String read. Label() { return label; } Java 语言编程技术 } 47
内部类(续) public Destination to(String s) { //method 1 return new Destination(s); } public Contents cont(int cn) { //method 2 return new Contents(cn); } public void ship(String dest, int cn) { //method 3 Contents c = cont(cn); Destination d = to(dest); System. out. println("Contents: " + c. value()); System. out. println("Distination: " + d. read. Label()); } …… } Java 语言编程技术 48
内部类(续) class pl 2 { public static void main(String[] args) { Parcel 2 p 1 = new Parcel 2(); p 1. ship("Beijing", 100); Parcel 2. Contents cc = p 1. new Contents(1010); System. out. println(cc. value()); return handles to inner classes Parcel 2 p 2 = new Parcel 2(); Parcel 2. Contents c = p 2. cont(200); Parcel 2. Destination d = p 2. to("Shanghai"); System. out. println(c. value()); System. out. println(d. read. Label()); } } Java 语言编程技术 49
内部类(续) interface Destination { String read. Label(); } public class Parcel 4 { public Destination dest(String s) { // dest方法 class PDestination implements Destination { private String label; private PDestination(String where. To) { label = where. To; } public String read. Label() { return label; } } return new PDestination(s); } } Java 语言编程技术 51
public class Parcel 5 { void internal. Tracking(boolean b) { if(b) { class Tracking. Slip { private String id; Tracking. Slip(String s) { id = s; } String get. Slip() { return id; } } Tracking. Slip ts = new Tracking. Slip("slip"); String s = ts. get. Slip(); System. out. println(s); } // Can't use it here! Out of scope: //! Tracking. Slip ts = new Tracking. Slip("x"); } Java 语言编程技术 } 53
内部类(续) n 匿名内部类 例: interface Contents { int value(); } public class Parcel 6 { public Contents cont() { return new Contents() { private int i = 11; public int value() { return i; } }; // 此处要分号 } Java 语言编程技术 } 54
内部类(续) return new Contents() { //� 用� 含构造函数 private int i = 11; //Contents是接口 public int value() { return i; } }; //分号表示表达式的结束 该语句将返回值同表示返回值类型的类定义结合 在一起。它相当于: class My. Contents implements Contents { private int i = 11; public int value() { return i; } } return new My. Contents(); Java 语言编程技术 55
内部类(续) public class Wrapping { public class Parcel 7 { public Wrapping wrap(int x) private int i; { public Wrapping(int x) // Base constructor call: { return new Wrapping(x) { i = x; public int value() { } return super. value() * 47; public int value() { } return i; }; // Semicolon required } } } ///: ~ …… Java 语言编程技术 56
内部类(续) n 匿名类没有构造方法,但可以在声明成 员变量时初始化和进行实例初始化( Instance initialization) public Destination dest(final String dest) { return new Destination() { private String label = dest; public String read. Label() { return label; } }; Java 语言编程技术 57
内部类(续) dest(final String dest, final float price) { return new Destination() { 不能重载 private int cost; // Instance initialization for each object: { cost = Math. round(price); if(cost>100) System. out. println("Over budget!"); } private String label = dest; public String read. Label() { return label; } }; } Java 语言编程技术 58
- Slides: 62