java学习小技巧Word文件下载.docx
《java学习小技巧Word文件下载.docx》由会员分享,可在线阅读,更多相关《java学习小技巧Word文件下载.docx(17页珍藏版)》请在冰点文库上搜索。
![java学习小技巧Word文件下载.docx](https://file1.bingdoc.com/fileroot1/2023-5/7/b8a01952-7dca-4287-aedc-0892a088a8f2/b8a01952-7dca-4287-aedc-0892a088a8f21.gif)
2)&
(true||(++i>
2))
真
8、>
>
带符号右移,前面移空的位置添加符号位相同的数
0|0011000
右移两位(正数)
0|0000110
1|0011000
右移两位(负数)
1|1111100
>
带符号右移,前面移空的位置添加0
9、获得-5到2的随机数
inti;
Randomr=newRandom();
i=r.nextInt();
//
i=Math.abs(i%10)+1;
//获得0到10的随机数
i=Math.abs(i%8)-5;
//获得-5到-2的随机数
10、数组创建时,大小(内存)可以是前面的变量.可以动态创建数组的大小(内存),创建后就不能再改大小.
例:
intt=9;
int[][]jiu;
jiu=newint[t][];
11、变量的作用域。
定义的数个变量其实是放在一个栈的结构中,后定义的变量先消失,先定义的变量后消失,作用域比后定义的变量大。
12、.基本数据类型参数的传递是值传递,
引用....................址传递.
classLength{
intlength;
}
classPeople{
voidwalk(Lengthlength){
length.length=+=2;
publicsaticvoidmain(String[]args){
Lengthl=newLength();
l.length=20;
newPeople().walk(l);
System.out.println(l.length);
13、方法的重载,不能通过返回值类型不同来区别,只能通过参数的不同来区别.
14、方法或变量加static和
不加static的方法,是类的对象的方法.对象消失,方法消失
加static的方法,是类的方法.对象消失,方法存在.方法的地址是静态的与类绑定
变量和上面也一样.
15、静态方法,只能访问类的静态成员和该方法的成员
16、JAVA不支持多重继承,类似一个树,一个类只有一个父类,但可以有多个接口
C++支持多重继承,类似一个网,一个类可以有多个父类
17、子类默认调用了父类无参构造函数.如果父类没有无参构造函数,必须手动重写子类的构造方法,并用super(参数)调用父类中有参构造的方法.
privateintage;
publicPeople(intage){
this.age=age;
classDoctorextendsPeople{
//不写以下构造方法则出错.
publicDoctor(){
super(23);
//或者
publicDoctor(intage){
super(age);
解决方法:
在写类的有参构造方法时,最好定义个无参的构造方法.
18、构造方法调用父类的构造方法super()前不能有其他的语句.
19、final可以修饰形式参数,防止被改
voidwalk(finalintlength){
20、import导入包的理解,重要的事情总要放到前面
21、private与abstract冲突,原因:
子类实现(继承)不了接口(抽象类)中被private修饰的成员,但接口(抽象类)中的方法必须要重写,加private就没办法重写了
例:
interfaceInterfaceTest{
intf();
privateabstractintff();
//此句错误,要去掉private
22、内部类可以被外部使用,可以访问外部类(宿主类)的privite成员;
内部类成员加public也可以被外部访问,但也危险,定义成private外部不能访问此类了(常用).
publicclassOutClass{
privateinti=2;
publicclassInnerClass{
publicintj=i;
23、抽象类不用继承也能用
abstractclassOut2{
publicabstractintf();
publicstaticOut2getInstance(){
returnnewInner2();
privatestaticclassInner2extendsOut2{//static
publicintf(){
return2;
publicstaticvoidmain(String[]args){
Out2ou=Out2.getInstance();
//抽象类不用继承也能用,获得此类的实例
inti=ou.f();
24、接口里也可以定义类(内隐类)
classA{
intf(){
25、内部类的使用.类NoNameInnerClass不用implements实现接口,而用传递进来对象来用接口
interfaceInter{
voidpaint();
publicclassNoNameInnerClass{
publicvoidpaint(Interp){//传递进来对象来用接口
p.paint();
publicvoidfunc(){
//为了获得对象,定义一个内部类,把此对象做参数
classPaintimplementsInter{
publicvoidpaint(){
System.out.println("
paint."
);
Paintp=newPaint();
paint(p);
26、内部类的使用.不用类名,直接创建对象,无名内隐类,类名是他实现的接口名字
//直接创建对象,无名内隐类,类名是他实现的接口名字,
paint(newInter(){
});
27、单态设计模式。
能够创建类的唯一实例。
把构造方法定义成private,再创建静态的成员方法getInstance()获得此类唯一实例.
例1.
publicclassChineseTest{
publicstaticvoidmain(String[]args){
ChineseObj1=Chinese.getInstance();
ChineseObj2=Chinese.getInstance();
System.out.println(Obj1==Obj2);
}
classChinese{
privatestaticChineseobjRef=newChinese();
privateChinese(){
publicstaticChinesegetInstance(){
returnobjRef;
privatestaticChineseobjRef;
28、泛型应用
Vector<
E>
类后的E代表泛型,用来限制往里面放的数据类型
String>
v=newVector<
();
v.add("
aaa"
bbb"
System.out.println(v.get(0));
29、如果一个方法可能有异常,则用thrownewException("
"
)来抛出异常
如果方法内用throw抛出异常,则此方法头后必须用throws(多个s)声明可能会抛出异常。
如果一个方法头后用throws声明可能会抛出异常,则此方法在用的时候必须用try-catch语句
publicclassLx_Yichang{
staticintdiv(intx,inty)throwsException{
if(y==0){
thrownewException("
除数不能为0!
!
//方法内用throw抛出异常
}else{
returnx/y;
try{//用try-catch语句
intz=0;
z=Lx_Yichang.div(10,0);
System.out.println(z);
catch(Exceptionex){
System.out.println(ex.toString());
ex.printStackTrace();
finally{
End!
30、Hashtable类应用,可以通过get(键)或get(hashCode)来获得值内容。
importjava.util.Hashtable;
classPhoneList{
Stringname;
StringphoneNumber;
publicPhoneList(Stringname,StringphoneNumber){
this.name=name;
this.phoneNumber=phoneNumber;
publicclassHashtableTest{
//利用泛型
Hashtable<
String,PhoneList>
hashTable=newHashtable<
String,PhoneList>
hashTable.put("
wang0"
newPhoneList("
wang"
"
0000000"
));
wang1"
1111111"
wang2"
2222222"
wang3"
3333333"
System.out.println(hashTable.get("
).phoneNumber);
//不利用泛型
Hashtablehash=newHashtable();
hash.put("
//System.out.println(((PhoneList)hash.get("
)).phoneNumber);
//不用泛型,需要强制类型转换
//强制类型转换时,最好先进行类型判断
Objecto=hash.get("
if(oinstanceofPhoneList){
System.out.println(((PhoneList)o).phoneNumber);
//利用hashcode
Integer,PhoneList>
table=newHashtable<
Integer,PhoneList>
PhoneListp1=newPhoneList("
888888888888"
table.put(newInteger(p1.hashCode()),p1);
System.out.println(table.get(newInteger(p1.hashCode())).phoneNumber);
31、提供一个关于游戏的简短描述,网页,游戏名称,提供商,金钱初始值等等。
这些数据可以置于.jad文件中。
打包后是放在JAR的META-INF文件夹里。
用MIDlet类的getAppProperty(Stringkey)来获得其中的值.
32、Canvas的hideNotify()里写暂停的代码。
showNotify()写继续游戏的代码。
来电话时自动调用hideNotify()。
pauseApp也可以实现,但有的手机不支持如Nokia手机
34、运行提示ALERT:
java/lang/ClassFormatError:
Badversioninformation.原因
原因:
当前编译器的版本过高。
解决方法:
编译器的版本不要过高否则有的手机不认,选择编译器方法:
点项目右键属性->
JavaCompiler->
Enableprojectspecificsettings打勾,然后选择版本较低的编译器
35、把所有的引用都设置为null才可以变为垃圾
Heroh=newHero();
Heroh2=h;
h=null;
//此时,h并没变为垃圾,因为还有h2指向它,需要把h2也设置为null,h才变为垃圾
h2=null;
36、去掉无用包(ctrl+shift+0).或右键->
Source->
OrganizeImports
37、WTK2.5的安装后,ECLIPSE的设置
Window->
Preferences->
DeviceManagement->
Import->
找到WTK的安装路径
38、添加资源文件夹名
在项目上右键->
Properties->
双击JavaBuildPath->
点AddFoler->
在下面的选项中选择updateexclusionfiltersinothersourcefolderstosolvenesting,再添入资源文件夹名,如src、res等。
39、添加抽象类、接口中的方法。
例如对继承Canvas类,需要添加protectedvoidkeyPressed(intkeyCode){}方法时.
在代码上右键->
选择Override/ImplementsMethods->
在窗口中,对要重写的方法打勾->
Ok。
40、在for语句中,第2个循环条件可以和逻辑表达试取与
for(inti=0;
i<
100&
i%2==0;
i++)
41、DataInputStream包装FileInputStream后,底层操作文件,上层按指定格式输出(最易使用)
42、FileInputStream的应用
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
publicclassPekingFileInputStreamTest1{
try{
//在项目根目录里创建文件fileInputStream.txt
Filefile=newFile("
fileInputStream.txt"
FileOutputStreamfout=newFileOutputStream(file);
fout.write(65);
fout.close();
//输出到文件完毕后关闭
//开始读
FileInputStreamfin=newFileInputStream("
intb=fin.read();
System.out.println(b);
fin.close();
}catch(Exceptione){
//TODO:
handleexception
e.printStackTrace();
43、利用DataOutputStream包装FileInputStream按指定格式输出
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
publicclassPekingFileInputStreamTest2{
//包装下按指定格式输出
DataOutputStreamdout=newDataOutputStream(fout);
//子类fout做参数传给父类,子类当父类用
dout.writeInt(8793);
dout.writeUTF("
感动中国"
dout.close();
DataInputStreamdin=newDataInputStream(fin);
intb=din.readInt();
Strings=din.readUTF();
System.out.println(s);
din.close();
44、利用PrintWriter包装三次的例子。
PrintWriter包装OutputStreamWriter,OutputStreamWriter包装FileOutputStream,FileOutputStream包装File
importjava.io.OutputStreamWriter;
importjava.io.PrintWriter;
publicclassPekingFileInputStreamTest3{
OutputStreamWriterosw=newOutputStreamWriter(fout);
//测试字符流//字符流通向字节流的桥梁
PrintWriterpw=newPrintWriter(osw);
//包装三次
PrintWriterpw=newPrintWriter(newOutputStreamWrit