《Thinking in java》阅读笔记(3)-复用类、final

myhalfsea posted @ 2011年1月04日 23:13 in JAVA , 1314 阅读

java中类的复用分为3种:组合、继承、代理

    1、组合:在新的类中产生现有类的对象,因为现有类的对象是新类的组成部分,所以被称为组合。

    2、继承   继承就不用多说了

    3、代理,java并没有对代理产生实际的支持,但很多工具中支持。

final关键字:

1、final数据:对于基本类型,数值不能改变;对于对象引用,引用不变,一旦一个引用指向一个对象,就不能将其指向另一个。

   空白final:还是举例比较实在:

public class Test{
    private final int i = 0; //直接初始化
    private final int j;   //如果此时为初始化,在每个不同的构造器中必须初始化,
    Test(){
        j = 1;  // 构造器初始化
    }
    Test( int x ){
        j = x;     // 构造器初始化
//        i = 2;   不能改变i的值
    }
    public void p(){
//        j = 2;    非构造器中,不能改变其值
        System.out.println("j:"+j);
    }
}

2、final参数,如:

public void p(final int a){
     System.out.println("a:"+a);
//   a = 10;   不能更改值
}

3、final方法:很容易想到,final方法是不能被重写的

4、final类

   final类是不可以被继承中。里面所以方法都隐式的指定为final。

小提示:Vector里面的数据不能存放基本类型,基本类型需要传换成包装类。

 

pavzi.com 说:
2024年1月27日 15:40

Pavzi.com provides all the news about Gadgets, the Economy, Technology, Business, Finance and many more. The main concept or our aim behind this website has been the will to provide resources with full information on each topic which can be accessed through the Internet. To ensure that every reader gets what is important and worthy about the topic they search and link to hear from us. pavzi.com Our site is a multiple Niche or category website which will ensure to provide information and resources on each and every topic. Some of the evergreen topics you will see on our website are Career, Job Recruitment, Educational, Technology, Reviews and others. We are targeting mostly so it is true that Tech, Finance, and Product Reviews. The only reason we have started this website is to make this site the need for your daily search use.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta
Butterfly Theme | Design: HRS Hersteller of mobile Hundeschule.