Java初探

来源:互联网 发布:python 贝叶斯分类器 编辑:程序博客网 时间:2024/05/17 03:48

打起这个念头是在做CC150时发现好多时候code太complex,不如java来的简单,早晚得学,所以就东一榔头西一棒子的看开了,也没怎么记。

昨天看到了wiki上一个很好的java与C++对比总结,很适合C++ers转向Java,http://en.wikipedia.org/wiki/Comparison_of_Java_and_C%2B%2B

今天又看了一些interfaces,inheritance等。

1. java函数传递,都是by-value,但是由于每个东西都是object的reference,所以就相当于pass-by-reference,一个明证在下:

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.

For example, consider a method in an arbitrary class that moves Circle objects:

public void moveCircle(Circle circle, int deltaX, int deltaY) {    // code to move origin of circle to x+deltaX, y+deltaY    circle.setX(circle.getX() + deltaX);    circle.setY(circle.getY() + deltaY);            // code to assign a new reference to circle    circle = new Circle(0, 0);}

Let the method be invoked with these arguments:

moveCircle(myCircle, 23, 56)

Inside the method, circle initially refers to myCircle. The method changes the x and y coordinates of the object that circle references (i.e., myCircle) by 23 and 56, respectively. These changes will persist when the method returns. Then circle is assigned a reference to a new Circle object with x = y = 0. This reassignment has no permanence, however, because the reference was passed in by value and cannot change. Within the method, the object pointed to by circle has changed, but, when the method returns, myCircle still references the same Circle object as before the method was called.

2. 注意primitive,int和double是pass-by-reference的。

3. interface就相当于api,公司出于利益不想把具体的code公布,给出method signature(也就是method name和parameter list),关键词为interface,在具体implement的时候,类名后面加上implement XX (interface name)。

4. class的构建函数,可以不定义,但是每个函数都是object的子类,所以会跑到那里去default,但是如果在子类不定义,会跑到super里找无参数的constructor,如果没有,则complier会complain。

5. 多线程可以implement runnable函数,也可以继承thread的run(), http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread



0 0
原创粉丝点击