《Thinking in Java》读书笔记(更新中2005.11.08)

Charter 10 Detecing Types

1.RTTI(run-time type identification)
forms including:
a.the classic cast: "(Shape)", which uses RTTI to make sure the cast in correct, throw ClassCastException
b.the Class object representing the type of your object: "Gum.class"
c.the keyword instanceof,which tells you if an object is an instance of a particular type: "if(x instanceof Dog)"

2.instanceof vs. Class
a.instanceof: are you this class, or a class derived from this class?
b.Class: no concern with inheritance

3.RTTI vs. reflection
compiler opens and examines the .class file at compile time vs. ...by the run-time environment

4.RMI(Remote Method Invocation)
it allows a Java program to have objects distributed across many machines, likes distributed computing

5.you use RTTI only when you must

Charter 9 Error Handling with Exceptions
1.Exception arguments
a.throw new XXXException;
b.throw new XXXException("t = null");

2.the exception root class is the Throwable

3. catch(Exception e)
   {
     ... e.GetMessage();
     ... e.getLocalizedMessage();
     ... e   //toString()
     ... e.printStackTrace();
   }
越往下,显示的异常信息越详细

4.Exception restrictions P401
a.OK to add new exceptions for constructors, but you must deal with the base constructor exceptions
b.Regular methods must conform to base class
c.Interface CANNOT add exceptions to existing methods from the baseclass, if the methods doesn't exist in the base class
  , the exception is OK
d.Overridden methods can throw inherited exceptions

Charter 8 Interfaces & Inner Classes
1.An Inteface can also contain fields, but these are implicitly static and final.

2.When you implement an interface, the methods from the interface must be defined as public.

3.You should always prefer interface to abstract classes, if it's possible to create your base class without any method definitions or members variables.

4.Months.JANUARY
  public interface Months{
    int
      JANUARY = 1, ...
  }

5.Anonymous inner classes
  ...
  return new Contents(){
    private int i = 1;
    pulic int value();
  };  //Semicolon mark the end of the anonymous inner class

6.Inner classes have access rights to all the elements in the enclosing class.

7.So one way to look at the inner class in as the rest of the solution of the multiple-inheritance problem.

Charter 7 Polymorphism
1.多态的理解
eg. Wind,Stringed,Brass 是Instrument的三个SubSet
a. //定义
   public void tune(Wind i);
   public void tune(Stringed s);
   public void tune(Brass b);
   //使用
   Wind flute = new Wind();
   Stringed violin = new Stringed();
   Brass frenchHorn = new Brass();
   tune(flute);
   tune(violin);
   tune(frenchHorn);
b.//定义
  public void tune(Instrument i);
  Instrument i1 = new Wind();
  Instrument i2 = new Stringed();
  Instrument i3 = new Brass();
  tune(i1);
  tune(i2);
  tune(i3);
c.比较a、b两种写法,显然b种更高明,它将tune方法参数定义为父类,这样所有其子类的对象都可以调用一个tune,
  多态其实就是让你站在高处看问题,站得高了,你想往下去什么地方都可以,有很大的灵活性。

2.构造器执行过程分为三步:
  a.先执行其父类的构造器,若没有显示调用,则调用父类无参的构造器(发现没有就会报错)
  b.本类成员的初始化
  c.本类的构造器

3.当用父类的引用指向子类的对象时,这时只能调用父类所含的方法,要想调用子类的方法,必须将父类的引用Downcast
  为子类的引用。

先写刚刚看的第六章,之后在传上前几章的内容。
Charter 6 Reusing Classes
1.Name hiding
all the overloaded methods of the base class are available in the derived class.

2.Choosing composition vs. inheritance
inheritance: is-a relationship (common)
composition: has-a relationship

3.toString
System.out.println(orc)
隐式调用toString将orc格式化成字符串输出

4.Final data
a.compile-time constant that won't ever change
b.initialized at run time that you don't want change

Charter 5 Hiding the Implementation
1.use your domain name to get unique package names,eg. com.bruceeckel.simple

Charter 4 Initializtion & Cleanup
1.如果程序找不到对应参数类型的Constructor,就会把Constructor中的类型提升 eg. char-->int
2.this用法
a.一个Constructor去调用另一个Constructor  eg.  this(参数)
b.方法要返回当前对象  eg.  return this
c. this.s = s
3.The char value is zero, which prints as a space.
4.new won't creat a nonarray primitive

Charter 3 Controlling Program Flow
1.create a random number
eg.  Radom rand = new Random();
     int j;
     j = rand.nextInt(20);   //    0<=j<20
2.a better controlling flow
eg.  for(int j=31; j>=0; j--)
       if(((1<<j) & i) != 0)
         System.out.println("1");
       else
         System.out.println("0");
这段程序实现了将整数型变量i传化成二进制输出,用一个变量j即控制了循环次数,用作为与i作与运算的对象。

Charter 2 Everying is an Object
1.String s = "abcd";  //simple to initialize a reference
  String s = new String("abcd");  //normal to initialize a reference

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值