〖JAVE经验〗考试要点分享

Certification Key for SCJP1.4

Section 1 Declarations and Access Control

Objective 1, Creating Arrays

Write code that declares, constructs and initializes arrays of any base type using any of the permitted forms, both for declaration and for initialization

目标1, 创建数组

采用不同的格式来编写任一基本数据类型数组的声明,构造及初始化的代码。

数组是一连串对象或基本数据,它们必须同型,并以一个标识符封装在一起。数组好象一个对象,通过new关键字来创建。

声明数组

数组的声明并不能为数组分配内存。声明时数组不能有大小信息,也就是说编译器并不允许你告诉它究竟数组有多大,它只是一个reference(引用),并没有对应的空间。声明数组的方式有: int[] a1; int a1[]两种,int num[5]是错误的数组声明方式。

声明并创建数组

声明一个数组并同时为它分配内存。

Int num[] =new int[5];

声明并初始化数组

声明一个数组并同时进行初始化。

Int num[]=new int[]{0,1,2,3,4};

Int num[]=new int[5]{0,1,2,3,4}; //!错误

数组知道自己的大小

与c++不同,数组知道自己的大小,当数组越界时,会抛出ArrayIndexOutOfBoundsException异常。数组具有length属性(不是length()方法),它能告诉你数组的大小。

多维数据

int m[][] ; int [] m[]; int[][] m;

数组的缺省值

与其它的变量不同,不论数组在向处创建,它总是使用可以使用缺省值。

示例:

public class MyAr{

public static void main(String argv[]){

int[] i = new int[5];

int i[5]; //!编译错误

int[] m[]={{1,2,3,4},{2,3,4,5},{4,5,6,7}};

int[][] n={{1,2,3,4},{2,3,4,5},{4,5,6}};

int j;

m=n;

for(int k=0;k%26lt;n.length;k++){

System.out.println(n[k].length);

}

System.out.println(i[5]); //!运行错误,超界

System.out.println(i[4]); //正确,打印0

System.out.println(j); //!编译错误,没有初始化

For(int k=0;k%26lt;i.length;k++){

I[k]=k;

}

}

}

Objective 2, Declaring classes and variables

Declare classes, inner classes, methods, instance variables static, variables and automatic (method local) variables, making appropriate use of all permitted modifiers (such as public final static abstract and so forth). State the significance of each of these modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.

目标2 声明类与变量

声明类,内部类,方法,实例变量,静态变量,自动变量(局部变量),正确使用各种修饰符(public , private , static ,final, abstract)。

在JAVA中万事万物皆对象,即使JAVA程序本身也是对象。

类的定义和对象的生成

public class MyClass{ //类定义

int i;

float f;      //类数据成员

void amethod(){  //方法

int i;    // 局部变量

}

}

MyClass aClass =new MyClass();  //创建类的一个实例(对象)

修饰符说明

private

被了变量所在的类,其它任何类都不可以访问这个成员。

无访问修饰符

所谓的包访问权限,同一个包内的其它类可以访问这个成员。

Protected

与无访问修饰符具有权限外,还允许子类访问这个成员。

Public

具有全局可视性,任何其它类都可以访问这个成员。

Static

Static变量称为类变量,类的所有对象共享这个变量。

Static方法称为类方法,它只能访问static变量。静态方法不能被子类overriding为非静态方法,但静态方法可以被子类静态方法所Hided.

Native

只用于方法,指明方法体是由其它编程语言编写的。它以;结尾不是以{}结尾。

Public native void fastcalc();

Abstract

Abstract修饰符可用于类与方法前,在用于方法前时表明方法不具备方法体。只支类中包括了抽象方法则它必须声明为抽象类。如果一个类实现一个接口时,它没有为接口中所有的方法提供实现时,也必须声明为抽象类。

Final

Final修饰符可用于类,方法和变量,fianl类不能被继承,所有final类中的方法都是教学final方法。Final变量的值必须在创建时设定并具不能被修改。

Synchronized

防止多个线程同时访问相同的代码段。

Transient

表明类序列化时,变量不必序列化。

Volatile

表明变量由于线程可能异步修改。

示例:

abstract class Base{

abstract public void myfunc();  //抽象方法

public void another(){     //实例方法

System.out.println(“Another method”);

}

static void smethod(){   //类方法

System.out.println(“base smethod”);

}

}

public class Abs extends Base{

public static void main(String argv[]){

Abs a = new Abs();

Base b=new Abs();

a.amethod();  // 动态绑定,运行时调用

a.smethod();  //静态方法,编译时调用

b.smethod();

}

public void myfunc(){

System.out.println(“My func”);

}

public void amethod(){  //实现抽象方法

myfunc();

}

static void smethod(){ //hiding 父类静态方法

System.out.println(“Abs smethod”);

}

}

Objective 3, The default construcTor

For a given class, determine if a default construcTor will be created and if so state the prototype of that construcTor

目标3 缺省构造器

结定一个类,确定是否有缺省构造器

构造器是与类名相同的方法,并具没有返回值。缺省构造器是一个不具有任何参数的构造器。在你没有提供构造器的条件下,编译器为自动化为你提供一个缺省的构造器,但一旦你定义了任何一个构造器,编译器就不会为你提供缺省构造器。在这种条件下,如果你使用不具有参数的构造器将有错误。

构造器可以有访问修饰符,但不能有native,abstract,static,synchronized和final修饰符。

示例:

class Base{

int i;

Base(int i){

this.i=i;

System.out.println(“single int construcTor”);

}

void Base(){

System.out.println(“in void Base()”);

}

//Base(){}

}

public class Cons extends Base {

Cons(int i){}

//Cons(int i){super(i);}

public static void main(String argv[]){

Base c = new Base(); //编译错误,没有构造函数

Cons n=new Cons(3); //编译错误,Base没有无参数构造函数,void Base()函数不是构造函数。

}

}

Objective 4, Overloading and overriding

State the legal return types for any method given the declarations of all related methods in this or parent classes.

目标4 重载与覆写

为所有在自己或父类中的相关方法声明有效的返回值,

相同类中的方法

当在同一个类中有多个方法具有相同的名称时,这个方法就被重载了。只有参数的次序和类型是区分重载方法的依据,而返回值和参数的名称对区分重载方法没有贡献,所以,不能以返回值的不同来重载方法。

子类中的方法

可以在子类中重载父类的方法,只要新的重载方法具有不同的参数次序或类型。当你在子类中的方法具有于父类中的方法相同的signature,则称子类覆写了父类的方法。注意:子类覆写父类方法,它的访问修饰符可以不同,但子类要具有比父类更加严格的访问权限。

静态方法不能被覆写只能被HIDED。

基本类型为参数的重载

基本类型可以自动进行窄化转型(narrowing conversion),在没有相应数据类型的重载方法,它的数据类型向上晋升。

示例:

class Base{

public void another(int i){

System.out.println(“Another int method”+i);

}

//public int another(int i){} //!编译错误,重复定义

public void another(double d){

System.out.println(“Another double method “+d);

}

static void smethod(){

System.out.println(“base smethod”);

}

}

public class Abs extends Base{

public static void main(String argv[]){

Abs a = new Abs();

Base b=new Abs();

a.amethod();

a.smethod();

b.smethod();

a.another(4);

a.another(4.9f); // 注意:它调用了覆写方法

b.another(4.9f); // 它不调用覆写方法

}

public void m

更多免费相关学习经验请访问:Tore_m_1206686_21115_1_1.html”>http://www.shangxueba.com/sTore_m_1206686_21115_1_1.html

妩媚动人,让我感受到了大自然的神奇。

〖JAVE经验〗考试要点分享

相关文章:

你感兴趣的文章:

标签云: