JAVA重难点问题剖析--实例讲解好理解.doc

上传人:11****ws 文档编号:3179729 上传时间:2019-05-24 格式:DOC 页数:41 大小:48.37KB
下载 相关 举报
JAVA重难点问题剖析--实例讲解好理解.doc_第1页
第1页 / 共41页
JAVA重难点问题剖析--实例讲解好理解.doc_第2页
第2页 / 共41页
JAVA重难点问题剖析--实例讲解好理解.doc_第3页
第3页 / 共41页
JAVA重难点问题剖析--实例讲解好理解.doc_第4页
第4页 / 共41页
JAVA重难点问题剖析--实例讲解好理解.doc_第5页
第5页 / 共41页
点击查看更多>>
资源描述

1、JAVA 重难点问题剖析(一)这次主要剖析以下四个问题:构造函数定义问题、类实例化时的歧义、equals 和=、protected 成员的访问问题一、(1)构造函数前不加 void,否则变为方法,只能加可访问性修饰。public class Name void Name()System.out.println(“Method“);public static void main(String args)Name n=new Name();无输出,去 void 则输入 Method;(2)如果需要在不同包的类中使用构造函数创建对象必须前面加上 public;二、类实例化时的歧义问题(1)单类形导入

2、(即 import p1.TestC 而非 import p1.*)时用此类实类化;(2)完全标示名解决歧义问题 p1.TestC b=new p1.TestC()package p1; /定义第一个类public class TestC public TestC()System.out.println(“p1“);package p2; /定义第二个类public class TestC TestC()System.out.println(“p2“);package p2; /定义第三个类import p1.*; /(1)public class TestB public static vo

3、id main(String args) TestC c=new TestC(); /(2) 输出 p2,若要输出 p1,可将(1)处改为 import p1.TestC 或者将(2)处改为 p1.TestC b=new p1.TestC();三、数值、对象比较问题java.lang.object 类中的 equal()方法:public boolean equals(Object obj) return (this = obj);java.lang.string 类中覆盖 object 中的 equals()方法:public boolean equals(Object anObject)

4、if (this = anObject) return true;if (anObject instanceof String) String anotherString = (String)anObject;int n = count;if (n = anotherString.count) char v1 = value;char v2 = anotherString.value;int i = offset;int j = anotherString.offset;while (n- != 0) if (v1i+ != v2j+)return false;return true;retu

5、rn false;例子:class TestString s;Test(String s)this.s=s;public class Fyz public static void main(String args) String s1=new String(“hello“);String s2=new String(“hello“);Test T1=new Test(“JAVA“);Test T2=new Test(“JAVA“);System.out.println(s1.equals(s2); /true:String、Boolean 等类的 equals 覆盖了 object 的,比较的

6、不再是对象而是数值System.out.println(T1.equals(T2); /false:没有覆盖 object 的,所以比较的仍然是对象System.out.println(S1=s2); /false:=比较的是对象,无论是什么对象;System.out.println(T1=T2); /false:同上T2=T1; /使引用变量 T1、T2 都成为同一对象的别名System.out.println(T1.equals(T2); /true:对象相同System.out.println(T1=T2); /true:同上四、若在不同包中子类使用对象引用来访问父类的 protecte

7、d 成员,必须将对象实例化为子类对象,而不能实例为父类对象来访问,即子类只能以实现继承层次中的身份访问其超类的 protected 成员。相同包子类中使用对象引用的类型都可以。package test1;public class A public void publicMethod()System.out.println(“public 方法“); protected void protectedMethod()System.out.println(“protected 方法“);void friendlyMethod()System.out.println(“friendly 方法“);pr

8、ivate void privateMethod()System.out.println(“private 方法“);package test1;public class ExtendsA extends Apublic static void main(String args) A A1=new A(); A1.publicMethod();A1.protectedMethod();A1.friendlyMethod();/A1.privateMethod();ExtendsA ExtendsA1=new ExtendsA();ExtendsA1.publicMethod();Extends

9、A1.protectedMethod(); ExtendsA1.friendlyMethod();/ExtendsA1.privateMethod();public void printinfo()publicMethod();protectedMethod();friendlyMethod();/privateMethod();package test2;import test1.A;public class ExtendsA extends Apublic static void main(String args) A A1=new A();A1.publicMethod();/ A1.p

10、rotectedMethod(); 调用不行/A1.friendlyMethod();/A1.privateMethod();ExtendsA ExtendsA1=new ExtendsA();ExtendsA1.publicMethod(); ExtendsA1.protectedMethod(); /可以调用 /ExtendsA1.friendlyMethod();/ExtendsA1.privateMethod();public void printinfo() publicMethod();protectedMethod();/可以直接调用/friendlyMethod();/priv

11、ateMethod();多态与类型转化原理分析:一、多态性:超类引用在运行时既能代表超类本身的对象,也能代表其子类的对象的能力。类的一个成员若想表现多态必须可以被覆盖:对于成员变量而言,不会发生覆盖现象(会隐藏),在子类出现相同变量的定义时只会隐藏父类变量,因此不会表现多态。同时变量调用在编译时就会解析,不符合动态绑定的特征;在成员方法中,静态方法和 final 方法(private 方法)也不会发生覆盖现象(会隐藏),因此也不会表现多态性。因此只有除静态方法和 final 方法以外的方法才会表现多态性。二、向上类型转化时:丢失添加的方法和字段,剩余的为:基类字段基类静态方法或 final 方

12、法 /前二者为不能被覆盖的成员,因此保留,无多态性基类其他方法(若被子类覆盖则为子类覆盖的新方法)package test3;class oopsuper static String str = “父类字段“;public void publicMethod()System.out.println(“父类 public 方法“);protected void protectedMethod()System.out.println(“父类 protected 方法“);/(1)private void privateMethod()System.out.println(“父类 private 方

13、法“);static void staticMethod()System.out.println(“父类静态方法“); public class oopsub extends oopsuper String str = “子类字段“;public void publicMethod()System.out.println(“子类 public 方法“);protected void protectedMethod()System.out.println(“子类 protected 方法“);/(2)private void privateMethod()System.out.println(“

14、子类 private 方法“);static void staticMethod()System.out.println(“子类静态方法“); public static void main(String args) oopsuper upcast = new oopsub();System.out.println(upcast.str);/方法调用才具有多态性,而域没有多态性/ 能被覆盖的方法的行为才有多态特性upcast.publicMethod();upcast.protectedMethod();/若注释掉(1)则有错误;若注释掉(2)则输出:子类 protected 方法/ 不能被覆

15、盖的方法final 方法(含私有方法)、静态方法的行为不具有多态特性/ upcast.privateMethod();访问的是父类的私有方法,不能访问,不具有多态现象upcast.staticMethod();JAVA 重难点问题剖析(二)By fengyuzhe 发表于 2007-12-14 12:44:00 0这次主要剖析以下问题:抽象类与接口的区别、别名、局部变量的语句块作用域、this 构造和 super 构造一、抽象类与接口的区别:* 1.抽象类中可以定义所有成员变量(含实例变量和静态变量含常量)和非空方法,而接口中只能定义常量和空方法;* 2.抽象类在定义抽象方法时必须加 abst

16、ract,而在接口中可以加但不需要加;* 3.接口允许多继承:一个接口可以基层多个接口,实现接口的类也可以继承多个接口,但 JAVA 的类仅支持单继承。package test;interface object1 / int var1;/static int var1s;int VAR1 = 2;/ 接口中只能定义常量,等同于 final static int VAR1=2;int interfaceMethod1();abstract int interfaceMethod2();/接口中的方法其实就是抽象方法,但在接口中一般不加 abstract/ int inferfaceMethod3

17、() 接口中不能含有非抽象方法abstract class object2 int var2;static int var2s;final static int VAR2 = 2;/抽象类中可以定义变量也可以定义常量abstract int abstractMethod1();/int abstractMethod2();空方法必须加 abstract 修饰符/abstract int abstractMethod3() 抽象方法不能指定方法体void abstractMethod4()/抽象类中可以含有非抽象方法二、下面中的引用变量都是别名package test2;public class

18、 test int a;public test(int i) this.a = i;public static void main(String args) test test1 = new test(10);test test2 = test1;System.out.println(test1.a);System.out.println(test2.a);test1 = null;/ System.out.println(test1.a);System.out.println(test2.a);三、局部变量的语句块作用域:语句块中所声明的变量的作用域处在声明该变量的语句块中,语句块外部不能访

19、问public class hong public static void main(String args) int var1 = 1;int var2 = 2;System.out.println(var1);/ System.out.println(var2);/ var2 不能别解析,因为 var2 的作用域处在语句块中四、this 构造和 super 构造二者必须位于构造函数的第一行,this()构造用于串链同一个类中的构造函数,而 super()构造用于激活超类的构造函数,如果构造函数的第一句不是 this()构造或者 super()构造,则会插入一条指向超类默认构造函数的 sup

20、er()调用class test int i,j;test(int i, int j) this.i = i;this.j = j;public class SuperDemo extends test int k,l;SuperDemo() this(11,12);/* 错误,插入插入一条指向超类默认构造函数的 super()调用SuperDemo(int i, int j) this.k = i;this.l = j;*/public static void main(String args) SuperDemo sd = new SuperDemo();System.out.print(

21、sd.i+“+ sd.j+sd.k+sd.l);一 Input 和 Output1. stream 代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源。在 Java 的 IO 中,所有的stream(包括 Input 和 Out stream)都包括两种类型:1.1 以字节为导向的 stream以字节为导向的 stream,表示以字节为单位从 stream 中读取或往 stream 中写入信息。以字节为导向的 stream 包括下面几种类型:1) input stream:1) ByteArrayInputStream:把内存中的一个缓冲区作为 InputStream 使用2)

22、 StringBufferInputStream:把一个 String 对象作为 InputStream3) FileInputStream:把一个文件作为 InputStream,实现对文件的读取操作4) PipedInputStream:实现了 pipe 的概念,主要在线程中使用5) SequenceInputStream:把多个 InputStream 合并为一个 InputStream2) Out stream1) ByteArrayOutputStream:把信息存入内存中的一个缓冲区中2) FileOutputStream:把信息存入文件中3) PipedOutputStream:

23、实现了 pipe 的概念,主要在线程中使用4) SequenceOutputStream:把多个 OutStream 合并为一个 OutStream1.2 以 Unicode 字符为导向的 stream以 Unicode 字符为导向的 stream,表示以 Unicode 字符为单位从 stream 中读取或往 stream 中写入信息。以Unicode 字符为导向的 stream 包括下面几种类型:1) Input Stream1) CharArrayReader:与 ByteArrayInputStream 对应2) StringReader:与 StringBufferInputStre

24、am 对应3) FileReader:与 FileInputStream 对应4) PipedReader:与 PipedInputStream 对应2) Out Stream1) CharArrayWrite:与 ByteArrayOutputStream 对应2) StringWrite:无与之对应的以字节为导向的 stream3) FileWrite:与 FileOutputStream 对应4) PipedWrite:与 PipedOutputStream 对应以字符为导向的 stream 基本上对有与之相对应的以字节为导向的 stream。两个对应类实现的功能相同,字是在操作时的导向

25、不同。如 CharArrayReader:和 ByteArrayInputStream 的作用都是把内存中的一个缓冲区作为InputStream 使用,所不同的是前者每次从内存中读取一个字节的信息,而后者每次从内存中读取一个字符。1.3 两种不现导向的 stream 之间的转换InputStreamReader 和 OutputStreamReader:把一个以字节为导向的 stream 转换成一个以字符为导向的stream。2. stream 添加属性2.1 “为 stream 添加属性”的作用运用上面介绍的 Java 中操作 IO 的 API,我们就可完成我们想完成的任何操作了。但通过 F

26、ilterInputStream 和FilterOutStream 的子类,我们可以为 stream 添加属性。下面以一个例子来说明这种功能的作用。如果我们要往一个文件中写入数据,我们可以这样操作:FileOutStream fs = new FileOutStream(“test.txt”);然后就可以通过产生的 fs 对象调用 write()函数来往 test.txt 文件中写入数据了。但是,如果我们想实现 “先把要写入文件的数据先缓存到内存中,再把缓存中的数据写入文件中”的功能时,上面的 API 就没有一个能满足我们的需求了。但是通过 FilterInputStream 和 Filter

27、OutStream 的子类,为 FileOutStream 添加我们所需要的功能。2.2 FilterInputStream 的各种类型2.2.1 用于封装以字节为导向的 InputStream1) DataInputStream:从 stream 中读取基本类型(int、char 等)数据。2) BufferedInputStream:使用缓冲区3) LineNumberInputStream:会记录 input stream 内的行数,然后可以调用 getLineNumber()和setLineNumber(int)4) PushbackInputStream:很少用到,一般用于编译器开发

28、2.2.2 用于封装以字符为导向的 InputStream1) 没有与 DataInputStream 对应的类。除非在要使用 readLine()时改用 BufferedReader,否则使用DataInputStream2) BufferedReader:与 BufferedInputStream 对应3) LineNumberReader:与 LineNumberInputStream 对应4) PushBackReader:与 PushbackInputStream 对应2.3 FilterOutStream 的各种类型2.2.3 用于封装以字节为导向的 OutputStream1)

29、DataIOutStream:往 stream 中输出基本类型(int、char 等)数据。2) BufferedOutStream:使用缓冲区3) PrintStream:产生格式化输出2.2.4 用于封装以字符为导向的 OutputStream1) BufferedWrite:与对应2) PrintWrite:与对应3. RandomAccessFile1) 可通过 RandomAccessFile 对象完成对文件的读写操作2) 在产生一个对象时,可指明要打开的文件的性质:r ,只读;w ,只写;rw 可读写3) 可以直接跳到文件中指定的位置4. I/O 应用的一个例子import jav

30、a.io.*;public class TestIOpublic static void main(String args)throws IOException/1.以行为单位从一个文件读取数据BufferedReader in = new BufferedReader(new FileReader(“F:nepalonTestIO.java“);String s, s2 = new String();while(s = in.readLine() != null)s2 += s + “n“;in.close();/1b. 接收键盘的输入BufferedReader stdin = new B

31、ufferedReader(new InputStreamReader(System.in);System.out.println(“Enter a line:“);System.out.println(stdin.readLine();/2. 从一个 String 对象中读取数据StringReader in2 = new StringReader(s2);int c;while(c = in2.read() != -1)System.out.println(char)c);in2.close();/3. 从内存取出格式化输入tryDataInputStream in3 = new DataInputStream(

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育教学资料库 > 精品笔记

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。