1、int m;int m;数组的缺省值与其它的变量不同,不论数组在向处创建,它总是使用可以使用缺省值。示例: public class MyAr public static void main(String argv) int i = new int5; int i5;编译错误 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;kn.length;k+) System.out.println(nk.length); System.out.println(i5); /!运行错误,超
2、界 System.out.println(i4);/正确,打印0 System.out.println(j);/!编译错误,没有初始化 For(int k=0;i.length; Ik=k;Objective 2, Declaring classes and variablesDeclare classes, inner classes, methods, instance variables static, variables and automatic (method local) variables, making appropriate use of all permitted mod
3、ifiers (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 , privat
4、e , static ,final, abstract)。在JAVA中万事万物皆对象,即使JAVA程序本身也是对象。类的定义和对象的生成public class MyClass/类定义int i;float f;/类数据成员void amethod()/方法/局部变量MyClass aClass =new MyClass();/创建类的一个实例(对象)修饰符说明private被了变量所在的类,其它任何类都不可以访问这个成员。无访问修饰符所谓的包访问权限,同一个包内的其它类可以访问这个成员。Protected与无访问修饰符具有权限外,还允许子类访问这个成员。Public具有全局可视性,任何其它类
5、都可以访问这个成员。StaticStatic变量称为类变量,类的所有对象共享这个变量。Static方法称为类方法,它只能访问static变量。静态方法不能被子类overriding为非静态方法,但静态方法可以被子类静态方法所Hided.Native只用于方法,指明方法体是由其它编程语言编写的。它以;结尾不是以结尾。Public native void fastcalc();AbstractAbstract修饰符可用于类与方法前,在用于方法前时表明方法不具备方法体。只支类中包括了抽象方法则它必须声明为抽象类。如果一个类实现一个接口时,它没有为接口中所有的方法提供实现时,也必须声明为抽象类。Fin
6、alFinal修饰符可用于类,方法和变量,fianl类不能被继承,所有final类中的方法都是教学final方法。Final变量的值必须在创建时设定并具不能被修改。Synchronized防止多个线程同时访问相同的代码段。Transient表明类序列化时,变量不必序列化。Volatile表明变量由于线程可能异步修改。abstract class Baseabstract public void myfunc();/抽象方法public void another()/实例方法System.out.println(Another method);static void smethod()/类方法b
7、ase smethodpublic class Abs extends Basepublic static void main(String argv)Abs a = new Abs();Base b=new Abs();a.amethod();/动态绑定,运行时调用a.smethod();/静态方法,编译时调用b.smethod();public void myfunc()My funcpublic void amethod()/实现抽象方法myfunc();static void smethod()/hiding父类静态方法Abs smethodObjective 3, The defau
8、lt constructorFor a given class, determine if a default constructor will be created and if so state the prototype of that constructor目标3缺省构造器结定一个类,确定是否有缺省构造器构造器是与类名相同的方法,并具没有返回值。缺省构造器是一个不具有任何参数的构造器。在你没有提供构造器的条件下,编译器为自动化为你提供一个缺省的构造器,但一旦你定义了任何一个构造器,编译器就不会为你提供缺省构造器。在这种条件下,如果你使用不具有参数的构造器将有错误。构造器可以有访问修饰符
9、,但不能有native,abstract,static,synchronized和final修饰符。class BaseBase(int i)this.i=i;single int constructorvoid Base()System.out.println(“in void Base()”);/Base()public class Cons extends Base Cons(int i)/Cons(int i)super(i);Base c = new Base();/编译错误,没有构造函数Cons n=new Cons(3);/编译错误,Base没有无参数构造函数,void Base
10、()函数不是构造函数。Objective 4, Overloading and overridingState the legal return types for any method given the declarations of all related methods in this or parent classes.目标4重载与覆写为所有在自己或父类中的相关方法声明有效的返回值,相同类中的方法当在同一个类中有多个方法具有相同的名称时,这个方法就被重载了。只有参数的次序和类型是区分重载方法的依据,而返回值和参数的名称对区分重载方法没有贡献,所以,不能以返回值的不同来重载方法。子类中
11、的方法可以在子类中重载父类的方法,只要新的重载方法具有不同的参数次序或类型。当你在子类中的方法具有于父类中的方法相同的signature,则称子类覆写了父类的方法。注意:子类覆写父类方法,它的访问修饰符可以不同,但子类要具有比父类更加严格的访问权限。静态方法不能被覆写只能被HIDED。基本类型为参数的重载基本类型可以自动进行窄化转型(narrowing conversion),在没有相应数据类型的重载方法,它的数据类型向上晋升。public void another(int i)Another int method+i);/public int another(int i) /!编译错误,重复
12、定义public void another(double d)Another double method +d);static void smethod()a.another(4);a.another(4.9f);/注意:它调用了覆写方法b.another(4.9f);它不调用覆写方法public void myfunc()public void amethod()void another(float f)Abs another float method+f);void another(int i)/编译错误,访问权限弱化Abs another methodAbs another int me
13、thodSection 2 Flow control and exception HandlingObjective 1, The if and switch statementsWrite code usingifandswitchstatements and identify legal argument types for these statements.目标1if和switch语句使用if和switch编写代码并能区分有效的参数类型if的条件语句中只能是boolean型的数据。Int k=1;If(k )System.out.println(k);If(k=1) System.out
14、.println(k);正确Switch的条件语句中只能是int数据类型,或int的窄化数据类型也就是byte,char, shortLong i;Switch(i)/错误Case 1: . break;Case 2: break;Default: .case语句的break的使用,在switch块中只有遇到break语句时,块才跳出。Default语句不必总安排在switch语句的最后,但default语句不在最后时,要使用break.三元操作符?:public class MySwitchMySwitch ms= new MySwitch();ms.amethod();ms.ifmetho
15、d();byte k=30;/ byte,char,short,intswitch(k)default: /Put the default at the bottom, not hereThis is the default outputcase 10:ten break;case 20:twentybreak;public void ifmethod()int k=1;boolean b;/if(k=1) System.out.println(Oneif(k=1) System.out.println(oneif(b=true) System.out.println(true输出结果:try
16、 itObjective 2, looping, break and continueWrite code using all forms of loops including labeled and unlabeled use of break and continue and state the values taken by loop counter variables during and after loop execution.目标2循环,break和continue编写各种(不)具有label的break和continue的循环语句。明确循环开始与结束时,循环记数器的值。For循
17、环语法: for(initialization; conditional expression;increment)逗号运算符它只能用于for的控制表达式中。for(int i=1,j=i+10;i5;i+,j=i*2)do-while与while的区别在开do-while中的语句至少执行一次。Goto与break,continue虽然goto是java的保留字,但java而不使用它。Java中的跳跃功能由break与continue提供。在java中,惟一一个放置lable而能够产生效益的地点就是恰恰放在迭代语句前。一般而论,break与continue只会中断当前的循环,而与label搭配,可以中断多层循环。Label1:OuteriterationInneriteration/1continue; /2.Continue label1;/3Break label1; /41中断内层迭代,回到外层迭代2将执行点移至内层迭代的起始处3同时中断内层迭代,再从外层迭代开始4同时中断内外层迭代,不再进行任何迭代务必记下。在java中使用label的惟一理由是跳过一个以上的嵌套层次。public class LabeledWhil
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1