17. J2 SDK
JRE
JVMJVM plus all APIs, compilers ,tools, and documentation (what you need in order to write java technology programs).
JVM plus basic APIs (what you need to distribute to people who will run your Java programs)
17
42. (2)Java集成开发工具 集成开发环境(简称IDE及Integrated Development Environment的缩写)是用于提供程序开发环境的应用程序,一般包括代码编辑器、编译器、调试器和图形用户界面工具。就是集成了代码编写功能、分析功能、编译功能、调试功能等一体化的开发软件服务套。所有具备这一特性的软件或者软件套(组)都可以叫做集成开发环境,可以独立运行,也可以和其它程序并用。JAVA的集成开发环境很多,比如 JCreator,NeBeans,Java Studio,JBuilder,Java Workshop,JDeveloper,Visual Age for java,Eclipse等等,这里简单介绍JCreator、netbeans和eclipse。 42
69. package 包名称;
import 包名.类;
……
public class 类名1{
类的成员变量(或域);
方法名(参数) {
局部变量和语句;
}
……
}下图就是Java源程序的一般形式。69
70. 例 程序Line.javapackage chapt02.shape;
import java.awt.Graphics;
public class Line {
protected int x1,y1;
protected int x2,y2;
public Line(double x1,double y1,double x2,double y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void draw(Graphics g){
g.drawLine(x1,y1,x2,y2);
}
}包名称引入的类名称类名称Line方法draw方法方法参数70
71. 3.Java源程序文件的命名按Java语言规定,源程序的文件名必须与由 public 修饰的类的类名称相同。例如下面的程序:public class TestFx extends Applet {
private Fx fx;
public void init() {
fx = new Fx(this.getSize().height);
}
public void paint(Graphics g) {
for(int x=0;x
74. 例 Fx程序计算数学函数。源程序文件名:Fx.java。 f(x) a x>0 - a x≤0 public class Fx {
public int fx(int x){
if(x>213)
return 1;
else
return 0;
}
public static void main(String args[]) {
Fx y = new Fx();
System.out.println(" f(x)="+y.fx(267));
}
}74
96. 96 实例变量(Instance Variables):
非静态域(Non-Static Fields),声明时不用static修饰的域。对象存储它的个体状态在非静态域中,非静态域也被称做“实例变量”,它们的值对于一个类的每个实例是唯一的。实例变量public class Circle
{
static final MIN_RADIUS=1;
protected int m_radius;
protected int m_ox,m_oy;
public Color m_color;
……
……
}96
97. 类变量(Class Variables):
类变量是声明时用static修饰的域,不管类被实例化多少次(也就是创建多少个对象),类变量仅有一个,即类的多个实例(对象)共享类变量。public class Circle
{
static final MIN_RADIUS=1;
protected int m_radius;
protected int m_ox,m_oy;
public Color m_color;
……
……
}实例变量类变量97
98. public class Cuboid{
public String toString(){
String msg="\nCuboid 对象成员\n length :"+length;
msg+="\n width :"+width+"\n high :"+high;
return msg;
}
public static void main(String []argc){
Cuboid c1=new Cuboid();
System.out.println(c1.toString());
}
} 局部变量(Local Variables ) :
在方法中保存临时状态值的变量即局部变量。Java语言没有专门的关键字指明一个局部变量,它声明在方法开始符号“{”和结束符号“}”之间。局部变量仅在声明它的方法中有效,类中的其他方法是不能使用的。局部变量局部变量98
131. 考察示例计算 M=1+2+3+4+5+6,其中M的初值为0。简单的计算语句如下:
M += 1; 等价与 M = M+1;
M += 2; 等价与 M = M+2;
M += 3; 等价与 M = M+3;
M += 4; 等价与 M = M+4;
M += 5; 等价与 M = M+5;
M += 6; 等价与 M = M+6; 其他复杂赋值运算用法与上述示例相同,具体在下面用简表列出。131
161. 161例3-5-1
int x=5,y=7;
boolean relat;
relat = x > y;
relat = x >= y;
relat = x < y;
relat = x <= y;
relat = x == y;
relat = x != y;(源程序文件ch03_5_1.java)
162. 162例3-5-2
从键盘输入两个double数,判断它们之间的关系。
(源程序文件ch03_5_2.java)import java.util.*;
import java.io.*;
public class ch03_5_2{
public static void main(String []argc){
Scanner sc = new Scanner(System.in);
double x,y;
System.out.print("输入第一个数:");
x = sc.nextDouble();
System.out.print("输入第二个数:");
y = sc.nextDouble();转下页
163. 163 boolean relat;
relat = x > y;
System.out.println("结果:");
System.out.println(" " + x + " > " + y + " " + relat);
relat = x >= y;
System.out.println(" " + x + " >= " + y + " " + relat);
relat = x < y;
System.out.println(" " + x + " < " + y + " " + relat);
relat = x <= y;
System.out.println(" " + x + " <= " + y + " " + relat);
relat = x == y;
System.out.println(" " + x + " == " + y + " " + relat);
relat = x != y;
System.out.println(" " + x + " != " + y + " " + relat);
}
}接上页
164. 164
165. 1651652.条件运算表达式条件运算符用于与关系运算符组合成复杂的关系表达式,语法格式:其中 op1 op2 op 可以是变量、常数或方法调用。“运算符”包括:&&,||,!,?:。其中&&和||运算的结果是boolean量,即“true”或“false。下表列出Java条件运算符。 Op1 运算符 Op2
运算符 Op
171. 171import java.util.*;
import java.io.*;
public class ch03_5_3{
public static void main(String []argc){
Scanner sc = new Scanner(System.in);
boolean result;
long m;
System.out.print("输入整数:");
m = sc.nextLong();
result = (m%13==0) && (m%27==0);
System.out.println("结果:");
System.out.println(" "+m+"被13整除且被27整除:" + result);
}
}
180. 180例3-5-5
执行下列语句,检查变量b值的变化。
int x = 3,y = 5;
int b;
b = x&y;
b = x|y;
b = x^y;
b = ~x;
(源程序文件ch03_5_5.java)
181. 181public class ch03_5_5{
public static void main(String []argc){
int x = 3,y = 5;
int b;
System.out.println("xy="+x+" y="+y);
b = x&y;
System.out.println("x&y="+b);
b = x|y;
System.out.println("x|y="+b);
b = x^y;
System.out.println("x^y="+b);
b = ~x;
System.out.println(" ~x="+b);
}
}
205. 205……
c=sc.getChar();
if(c<32)
System.out.println("This is a control character");
else {
if(c>='0'&&c<='9')
System.out.println("This is a digit");
else
if(c>='a'&&c<='z')
System.out.println("This is a small letter");
else
if(c>='A'&&c<='Z')
System.out.println("This is a capital small letter");
else
System.out.printf("This is an other character");
}
……考察下面的代码。
208. 208208int month = 8;
switch (month) {
case 1: System.out.println(“一月"); break;
case 2: System.out.println(“二月"); break;
case 3: System.out.println(“三月"); break;
case 4: System.out.println(“四月l"); break;
case 5: System.out.println(“五月"); break;
case 6: System.out.println(“六月"); break;
case 7: System.out.println(“七月"); break;
case 8: System.out.println(“八月"); break;
case 9: System.out.println(“九"); break;
case 10: System.out.println(“十月"); break;
case 11: System.out.println(“十一月"); break;
case 12: System.out.println(“十二月"); break;
default: System.out.println(“错误月份额值");break;
}
八月
218. 218例如下列程序:class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
}
}Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
221. 221例如下面的程序:class EnhancedForDemo {
public static void main(String[] args){
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
}
}Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
222. 222(3)示例:例 ch03_7_1
计算 1+2+3+…+100 的结果。public class ch03_7_1{
public static void main(String []argc){
int sum = 0;
for(int i=1;i<=100;i++){
sum += i;
}
System.out.println("1+2+3+¡+100="+sum);
}
}(源程序: ch03_7_1.java)
223. 223例 ch03_7_2
计算 1×2×3×…×15 的结果。(源程序: ch03_7_2.java)public class ch03_7_2{
public static void main(String []argc){
long inv = 1;
for(int i=1;i<=15;i++){
inv *= i;
}
System.out.println(" 1×2×3×…×15 ="+inv);
}
}
235. 235……
test:
for (int i = 0; i <= max; i++) {
int n = substring.length();
int j = i;
int k = 0;
while (n-- != 0) {
if (searchMe.charAt(j++) != substring.charAt(k++)) {
continue test;
}
}
foundIt = true;
break test;
}
……
245. 245(2)创建一维数组声明的数组型变量是引用型变量,因此数组声明后只有通过创建才能够使用。创建数组要使用new运算符实现。创建数组通过两个途径:
数组声明后用new运算符创建,语法格式:
数组变量 = new 类型[长度];
数组声明时用new运算符创建,语法格式:
类型[] 数组变量 = new 类型[长度];
如果数组没用创建就直接使用,编译器会提示编译错误:Variable Xxxx may not have been initialized. 数组变量 = new 类型[长度];类型[] 数组变量 = new 类型[长度];
246. 246例如
int[] aArray;
aArray = new int[100];
float[] fArray;
fArray = new float[35];
double[] dData = new double[23];
String[] sTitle = new String[15];
260. 260(2)创建二维数组声明的数组型变量是引用型变量,因此数组声明后只有通过创建才能够使用。创建数组要使用new运算符实现。创建数组通过两个途径:
数组声明后用new运算符创建,语法格式:
数组变量 = new 类型[行数][列数];
数组声明时用new运算符创建,语法格式:
类型[][] 数组变量 = new 类型[行数][列数];数组变量 = new 类型[行数][列数];类型[][] 数组变量 = new 类型[行数][列数];
261. 261例如,int[][] iMoving;
iMoving = new int[5][6];
float[][] fHeight;
fHeight = new float[2][8];
long[][] lDes = new long[4][4];
double[][] dFly = new double[10][5];
301. 301 实现(Java语言程序)public class SortType{
int []data1;
int []data2;
public SortType(int []data){
……
} public void input(int []data){
……
}
public void sort(){
……
}
public void display(){
……
}
}
322. 322public class car extends motorcar {
String name;
int seating;
public car(){
}
public boolean isRun(){
}
}例声明名称为car的类,它的父类是motorcar。因为它被public修饰,所以它可以被其他类使用。这个类的体部包括四个成员,两个数据成员name和seating,两个方法成员car和isRun。
333. 3334.this在实例方法或构造器中,this是一个当前对象的引用——即正 在被方法或构造器调用的对象。可以使用this引用当前对象的任何成员。(1)用this引用域使用this的多数原因是因为一个域被方法或构造器的参数屏蔽,即参数名与域名相同。例如下面的代码:public class Point {
public int x = 0, y = 0;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
334. 334(2)用this引用构造器从一个构造器的内部,可以用this关键字调用在相同类中的另一个构造器。例如下面的程序:public class Rectangle {
private int x, y,width, height;
public Rectangle() {
this(0, 0, 0, 0);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
……
}
}
339. 339public(共有的)域可以被所有类或对象访问。
被 public 修饰的成员变量,对于其他所有的类或对象都是可见的,可以被其他类或对象访问。考察下面的例子:public class PublicLevel{
public static int MAN=1;
public static int WOMAN = 0;
public int iSex;
public String sYourName;
}源程序: PublicLevel.java
350. 350例 ch04_3_1 (源程序: ch04_3_1.java)public class ch04_3_1{
private int []number;
static long count = 0;
{
number = new int[200];
for(int i=0;i
351. 351 public ch04_3_1(){
}
public ch04_3_1(int n){
}
public ch04_3_1(int n,int m){
}
public static void main(String []argc){
new ch04_3_1();
new ch04_3_1(1000);
new ch04_3_1(2000,4300);
}
}接
上
页
353. 353 public class ch04_3_2{
private int []number = initNumber();
public ch04_3_2(){
}
protected final int [] initNumber(){
int[] num = new int[200];
for(int i=0;i
360. 360例 ch04_3_4 (源程序: ch04_3_4.java)public class ch04_3_4{
private static int []m;
private static int size = 4500;
//静态初始化代码块1
static{
m = new int[100];
System.out.println("静态代码块1执行.");
}
//默认构造方法
public ch04_3_4(){
System.out.println("构造方法执行.");
}执行顺序:1执行顺序:3
362. 3627.最终域由修饰符“final”修饰的域,此种域不能被改变,用法格式:
final 数据类型 变量名称;
例如:访问修饰 final 数据类型 变量名称;class PhoneCard
{
final String TYPE_ID=“JPG_TYPE”;
double cardNumber;
……
}
363. 3638. 常量 (Constants)static与final组合通常用来表示常量。语法格式:例如
static final double PI = 3.141592653589793;
static final int MAX_SIZE = 216;修饰 static final 类型 常量名 = <字面量>;按惯例,常量名用大写,如果名称由多个词组成,用“_”连接起来。
364. 364364第四章、继承与多态继承
(构造方法继承)
365. 3653654.4、构造方法继承1.构造方法继承子类可以继承父类的构造方法,但继承遵循下列原则:如果子类没有无参数构造方法,它将继承父类的无参数构造方法作为自己的构造方法。public class A{
public A(){
System.out.println("Constructors: A().");
}
public A(int a){
System.out.println("Constructors: A(int a).");
}
}父类
366. 366public class B extends A{
}子类class Ch05_4_1 {
public static void main(String args[]){
B b;
b = new B();
}
}实例化类B
367. 367如果有自己的无参数构造方法,则在创建对象时先执行父类的无参数构造方法,然后再执行自己的无参数构造方法。public class A{
public A(){
System.out.println("Constructors: A().");
}
public A(int a){
System.out.println("Constructors: A(int a).");
}
}父类
368. 368public class B extends A{
public B(){
System.out.println("Constructors: B().");
}
public B(int a){
System.out.println("Constructors: B(int a).");
}
public B(int a,int b){
System.out.println(“Constructors: B(int a, int b).”);
}
}A的
子类
369. 369public class C extends B{
public C(){
System.out.println("Constructors: C().");
}
public C(int a){
System.out.println("Constructors: C(int a).");
}
public C(int a,int b){
System.out.println("Constructors: C(int a, int b).");
}
}B的
子类
370. 370class Ch05_4_2 {
public static void main(String args[]){
C c;
c = new C();
}
}实例化类C类A类B类C
371. 371如果子类在其构造方法中要调用父类的有参数构造方法,则使用super关键字在子类构造方法的第一条语句调用,调用格式:
super(<参数表>);public class A {
public String name = "Class A";
public A(){
System.out.println("构造方法:A()");
}
}父类
372. 372public class AA extends A{
public AA(){
System.out.println("构造方法:AA()");
}
public AA(int l){
super(l);
}
}子类调用父类的有参数构造方法类A类AA
374. 374public class A{
public A(){
System.out.println("Constructors: A().");
}
public A(int a){
System.out.println("Constructors: A(int a).");
}
}父类
375. 375public class B extends A{
public B(){
System.out.println("Constructors: B().");
}
public B(int a){
super(a);
}
public B(int a,int b){
System.out.println("Constructors: B(int a, int b).");
}
}类A类B子类
376. 376public class C extends B{
public C(){
System.out.println("Constructors: C().");
}
public C(int a){
super(a);
}
public C(int a,int b){
super(a,b);
}
public C(int a,int b, int c){
System.out.println("Constructors: C(int a, int b).");
}
}类A类B类C子类
377. 377class Ch05_4_4 {
public static void main(String args[]){
System.out.println("Create: A");
A a0 = new A();
A a1 = new A(1);
System.out.println("Create: B");
B b0 = new B();
B b1 = new B(1);
B b2 = new B(1,2);
System.out.println("Create: C");
C c0 = new C();
C c1 = new C(1);
C c2 = new C(1,2);
C c3 = new C(1,2,3);
}
}实例化类A
B
C
378. 378A a0 = new A();A a1 = new A(1);B b0 = new B();B b1 = new B(1);B b2 = new B(1,2);
390. 390public class Ch05_5_1 {
public static void main(String args[]) {
System.out.print("Default-Color of the Pen:");
System.out.println(AbstractPen.DEFAULTCOLOR);
System.out.print("Default-Size of the Pen:");
System.out.println(AbstractPen.DEFAULTSIZE);
}
}
391. 391import java.awt.*;
public abstract class AbstractPen {
public static Color DEFAULTCOLOR = Color.BLUE;
public static int DEFAULTSIZE = 1;
public AbstractPen(){
}
public static void modifyDefaultColor(Color color){
DEFAULTCOLOR = color;
}
public static void modifyDefaultSize(int size){
DEFAULTSIZE = size;
}
public abstract void drawLine(int x1,int y1,int x2,int y2);
public abstract void drawRect(int x1,int y1,int x2,int y2);
public abstract void drawArc(int x,int y,int r);
}
394. 394Java语言中接口有一个非常重要的角色。接口不是类树的一部分,尽管它们的工作与类结合。Java程序设计不允许多继承,但接口提供了一种替代方案。考察下面的程序代码:public interface OperateCar {
// method signatures
int turn(Direction direct, double radius, double speed);
int changeLanes(Direction direct, double start, double end);
int signalTurn(Direction direct, boolean signalOn);
int getRadarFront(double distanceToCar, double speed);
int getRadarRear(double distanceToCar, double speed);
}定义接口
395. 395public class OperateBMW760i implements OperateCar {
public int signalTurn(Direction direct, boolean signalOn) {
}
public int turn(Direction direct, double radius, double speed){
}
public int changeLanes(Direction direct, double start, double end){
}
public int signalTurn(Direction direct, boolean signalOn){
}
public int getRadarFront(double distanceToCar, double speed){
}
public int getRadarRear(double distanceToCar, double speed){
}
}实现接口
399. 399public interface DoSomethingInterface extends Interface1,
Interface2, Interface3 {
// constant declarations
double E = 2.718282; // base of natural logarithms
// method signatures
void doSomething (int i, double x);
int doSomethingElse(String s);
}例常量接口方法
400. 400 public interface Transparency {
public static final int OPAQUE=1;
public static final int BITMASK=2;
public static final int TRANSLUCENT=3;
public int getTransparency();
} 例常量接口方法
404. 404接口定义public interface DataTransform extends Runnable {
public static final int INPORT = 5000;
public static final int OUTPORT = 4000;
public static final int FRAMESIZE = 1024;
public void receive(Data data);
public void send(Data data);
}源程序文件:DataTransform.java
405. 4053.实现接口接口定义了程序的一种“行为”协议,类通过接口现实现这种协议。在声明类时实现接口,是通过子句implements确定要实现的接口,一个类可以同时实现多个接口,每个接口之间用“,”分隔开。实现接口必须使用implements关键字,具体格式如下:修饰 class 类名 extends 父类名
implements 接口[,接口]{
……
}
443. 443class InputData {
private static int len = 10;
private static byte []buffer;
public static int InputInteger() throws IOException,
InputDataException{
buffer = new byte[len];
System.in.read(buffer);
String data = new String(buffer,0,buffer.length);
data = data.trim();
try{
return Integer.parseInt(data);
}catch(NumberFormatException nfe){
throw new InputDataException("格式不正确",nfe);
}
}
}抛出异常
444. 444(3)捕获处理异常import java.io.*;
public class ExcepSample {
public static void main(String[] args) {
try{
System.out.println("请输入一个整数(xxxxx):");
System.out.println("input:" + InputData.InputInteger());
}catch(IOException e){
e.printStackTrace();
}catch(InputDataException ide){
ide.printStackTrace();
}
}
}捕获异常
449. 449public class A {
protected int x;
public A(){
x=100;
m = 2.0;
}
private void isNum(){
……
}
}public class B extends A {
protected int y;
public B(){
x = 10;
}
} 类B是由类A派生产生的,所以类B包含了类A的所有非私有成员。
类A的isNum()方法在类B中是不允许被使用的。
455. 455public class X {
……
public boolean isOverrid(){
……
}
……
}public class Y extends X {
……
@Override
public boolean isOverrid(){
……
}
……
}覆盖方法覆盖方法其形式如下:
456. 456public class myApplet extends Applet{
@Override
public void init() {
}
@Override
public void start() {
}
@Override
public void stop() {
}
@Override
public void paint(Graphics g) {
super.paint(g);
}
}被覆盖的父类 Applet的方法例
457. 457在调用覆盖的方法时,被调用的覆盖方法的版本只有一个,就是子类中的覆盖方法。考察下面的程序。public class Ch05_3_2 {
public static void main(String args[]){
X x = new X("X-Object");
x.displayClassMessage();
Y y = new Y("Y-Object");
y.displayClassMessage();
}
}
458. 458public class X {
protected String instanceName = "Un-named";
public X(){
System.out.println("Create X object.");
}
public X(String name){
instanceName = name;;
}
public void displayClassMessage(){
printClassName();
printInstanceName();
}
public void printClassName() {
System.out.println("Class:" + getClass().getName());
}
public void printInstanceName() {
System.out.println("Instance:" + instanceName);
}
}
459. 459public class Y extends X {
public Y(){
System.out.println("Create Y object.");
}
public Y(String name){
instanceName = name;;
}
@Override
public void printClassName() {
System.out.println("Class:" + getClass().getName());
}
}
460. 4603.方法隐藏如果在子类中定义与父类有相同签名类方法,这个方法就隐藏(Hides)父类中的方法。
被调用的隐藏方法的版本取决于是从子类或父类。public class Animal {
public static void testClassMethod() {
System.out.println("The class method in Animal.");
}
public void testInstanceMethod() {
System.out.println("The instance method in Animal.");
}
} 静态方法(类方法)非静态方法(实例方法)
461. 461public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The class method in Cat.");
}
public void testInstanceMethod() {
System.out.println("The instance method in Cat.");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
}静态方法(类方法)非静态方法
(实例方法)静态方法(类方法)非静态方法
(实例方法)执行结果:
The class method in Animal.
The instance method in Cat.
475. 475public class Man extends Person{
public Man(){
}
public Man(String name, float h){
super(name,h);
sex = “男”;
}
}Man类Man类和对象包含了其父类Person类的所有成员,即成员变量和方法。
Man类和对象可以访问Person类的非private成员。Person类的实例变量Person类的构造方法
476. 476public class Women extends Person{
public Man(){
}
public Man(String name, float h){
super(name,h);
sex = “女”;
}
}Women类Women类和对象包含了其父类Person类的所有成员,即成员变量和方法。
Women类和对象可以访问Person类的非private成员。Person类的实例变量Person类的构造方法
480. 480public class X{
private int type;
protected double value;
public String name;
public X(){
type = 0;
name = “un-name”;
value = 0.0;
}
public void setType(int type){
this.type = type;
}
}X 类
481. 481public class Y extends X{
int type;
public Y(String n, double v, int t){
value = v;
name = n;
setType(t);
}
public double getValue(){
return value;
}
public String getName(){
return name;
}
}Y类Y类的成员超类 X的成员超类 X的成员超类 X的成员
482. 4824.super关键字super关键字表示超类的引用,可用super:
访问超类成员。
在子类构造方法使用超类构造方法。
考察下列代码:public class Superclass {
public Superclass(){
}
public void printMethod() {
System.out.println("Printed in Superclass.");
}
}超类
483. 483public class Subclass extends Superclass {
public Subclass(){
super();
}
//overrides printMethod in Superclass
public void printMethod() {
super.printMethod();
System.out.println("Printed in Subclass");
}
public static void main(String[] args) {
Subclass s = new Subclass();
s.printMethod();
}
}子类调用父类方法调用父类构造方法
484. 484484第四章、继承与多态继承
(域的继承与隐藏)
485. 4854854.2、域的继承与隐藏1.域的继承子类继承父类所有的 public 和 protected 成员,而无论这个类是否与父类在同一个包中。
如果子类与父类在同一个包中,它也继承类父类的package-private 成员。
在子类中可以通过成员的名称直接引用父类的成员或者用“this.成员名称”应用父类的成员。下图表示上述关系:
487. 487public class A {
private boolean m;
protected int x;
public A(){
x=100;
m = 2.0;
}
}public class B extends A {
protected int y;
public B(){
x = 10;
}
} 类B是由类A派生产生的,所以类B包含了类A的所有非私有成员。
类A的m域在类B中是不允许被使用的。
494. 494public class A {
protected int x;
public A(){
x=100;
}
}public class B extends A {
protected int x;
public B(){
x = 10;
super.x = 10;
}
} 类B是由类A派生产生的,因为它们都有一个成员变量x,所以类B的成员x实际上隐藏了类A的成员x。
在子类中,用名称 x 引用的类B的x域;super.x引用的是类A的x域。
509. 509import java.awt.*;
import java.applet.*;
public class Ch05_10_1 extends Applet{
private Fx fx;
public void init(){
}
public void start(){
fx = new Fx(0,getSize().width,getSize().height);
}
public void paint(Graphics g){
Font font = g.getFont();
g.setFont(new Font("黑体",Font.BOLD,20));
g.setColor(Color.BLUE);
g.drawString(“绘制函数f(x)当x∈[0,Xmax]时曲线。",10,30);
类Ch05_10_1
522. 522获得当前时间,计算表针绘制坐标,绘制指针,在update ()方法中完成,主要工作://获得当前日期-时间
currentDate = new Date();
//从formatter中分离出当前时间:时、分、秒。
formatter.applyPattern("s");
s = Integer.parseInt(formatter.format(currentDate));
formatter.applyPattern("m");
m = Integer.parseInt(formatter.format(currentDate));
formatter.applyPattern("h");
h = Integer.parseInt(formatter.format(currentDate));
559. 559例 程序名称Sample3_5.java
import java.awt.*;
import java.applet.*;
public class Sample3_5 extends Applet{
public void paint(Graphics g){
g.setColor(Color.blue);
g.drawString("在applet直接播放声音", 50, 60 );
g.setColor(Color.red);
g.fill3DRect(40,90,410,40,true);
play(getCodeBase(), "sound/yahoo1.au");
play(getCodeBase(), "sound/music.au");
}
}
560. 560相关的HTML文件Sample3_5.html
这是利用Applet的play()播放声音的例程
561. 561例 演示用AudioClip接口方式播放一段音乐。
562. 第六章、图形用户界面(GUI)Swing 和 JFC562
563. 6.1 Swing与JFC1.JFCJFC是Java Foundation Classes 的简称,它封装了一组用于建立图形化的用户界面(GUI)和增加Java应用富图形功能和交互性的特性,定义的这些特性如下:Swing GUI Components
Pluggable Look-and-Feel Support
Accessibility API
Java 2D API
Internationalization563
576. 界面创建过程:……
Panel pane;
Button open,close;
pane = new Panel();
open = new Button(“Open”);
open.addActionListener( new Open事件处理() );
pane.add(open);
close = new Button(“Close”);
close.addActionListener( new Close事件处理() );
pane.add(close);
……576
611. 6.文本输入(TextField和TextArea) TextField是一个单行输入的文本输入组件。而TextArea是允许多行输入的文本输入组件。 构造方法
TextField()
TextField(int columns)
TextField(String text)
TextField(String text, int columns)
创建TextField对象。
参数
int columns 输入行的宽度(字符数)
String text 初始字符串
TextArea()
TextArea(int rows, int columns)
TextArea(String text) 611
612. TextArea(String text, int rows, int columns)
TextArea(String text, int rows, int columns, int scrollbars)
创建TextArea对象。
参数
int rows 输入区域的行数。
int columns 输入区域的列数。
String text 输入区域初始内容。
int scrollbars 输入域滚动条的状态:
SCROLLBARS_BOTH 垂直与水平滚动条。
SCROLLBARS_VERTICAL_ONLY 垂直滚动条。
SCROLLBARS_HORIZONTAL_ONLY 水平滚动条。
SCROLLBARS_NONE。无滚动条。 612
626. Runnable 接口的主要成员见下所列。 void run()
使用实现接口 Runnable 的对象创建一个线程时,启动该线程将导致在独立执行的线程中调用对象的 run 方法。
Runnable 接口应该由那些打算通过某一线程执行其实例的类来实现。类必须定义一个称为 run 的无参数方法。 626
627. 1 通过继承构造线程
我们可以建立一个类Thread的子类,通过继承类Thread并覆盖其run()方法和其他方法来构造线程体。例5.1
class TwoThreadsTest {
public static void main (String args[]) {
new SimpleThread("Jamaica").start();
new SimpleThread("Fiji").start();
}
} 类 TwoThreadsTest的main()方法中构造了两个SimpleThread类的线程,一个称为"Jamaica",另一个为 " Fiji ",并在构造后马上就调用了start()方法来启动这两个线程。627
628. class SimpleThread extends Thread {
public SimpleThread(String str) {
super(str);
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + getName());
try {
sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {}
}
System.out.println("DONE! " + getName());
}
}628
629. 2 通过接口构造线程体
通过建立一个实现了Runnable接口的对象,并以它作为线程的目标对象也可以构造线程体。例5.2
public class Clock extends java.applet.Applet
implements Runnable {
Thread clockThread;
public void start() {
if (clockThread == null) {
clockThread = new Thread(this, "Clock");
clockThread.start();
}
}629
630. public void run() {
while (clockThread != null) {
repaint();
try {
clockThread.sleep(1000);
} catch (InterruptedException e){
}
}
}
public void paint(Graphics g) {
Date now = new Date();
g.drawString(now.getHours() + ":" + now.getMinutes() + ":" +
now.getSeconds(), 5, 10);
}
public void stop() {
clockThread.stop();
clockThread = null;
}
}
630
687. import java.net.*;
import java.io.*;
public class SimpleUrl {
public static String fetch(String address){
try{
URL url=new URL(address);
return (String)url.getContent();
}catch(IOException e){
e.printStackTrace();
}
return "";
}
public static void main(String []agrc){
System.out.println(SimpleUrl.fetch("http://www.baidu.com"));
}
}687
688. 一.单实例模式(SINGLETON)对象创建型模式1.意图
保证一个类仅有一个实例,并提供一个访问它的全局访问点。
2.适用性
在下面的情况下可以使用S i n g l e t o n模式
当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。
当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。688