递归:反复用方法调用方法,用PPT画出来更容易理解,面试时也会用到。
index是什么意思怎么用?
内存的布局在以后有什么作用?
public class Fab {
public static void main(String arg[]) {
System.out.println(f(40));
}
public static long f(int index) {
if (index == 1||index == 2) {
return 1;
}
long f1 = 1L;
long f2 = 1L;
long f = 0;
for(int i=0; i<index-2; i++) {
f = f1 + f2;
f1 = f2;
f2 = f;
}
return f;
}
}
面向对象编程,类和对象的关系,
对象:用计算机语言对问题域中事物的描述,对象通过“属性”和“方法”来分别对应事物所具有的静态和动态属性
类:是描述同类型的对象的抽象概念,类中定义了这一类对象的静态和动态属性。
对象可以看成该类的一个具体实例,类是一类对象的模板。
小狗程序:
public class Dog {
static int furColor;
float height;
float weight;
void catchMouse(Mouse m) {
//m.scream();
}
public static void main(String[] args) {
//int i;
System.out.println(furColor);
Dog d = new Dog();
Mouse m = new Mouse();
d.catchMouse(m);
HelloWorld hw = new HelloWorld();
}
}
class Mouse {
int height;
}
Person程序:
public class Person {
int id;
int age = 20;
Person(int _id, int _age) {
id = _id;
age = _age;
}
public static void main(String[] args) {
Person tom = new Person(1,25);
Person jerry= new Person(1,20);
Point p = new Point();
}
}
class Point {
Point() {}
int x;
int y;
}
定义成员变量时可以初始化,也可不初始化,默认值为0,false,0L,0.0F,'/u0000',0.0D
成员变量作用在整个类体。
局部变量必须初始化。
java中除了基本类型之外的变量都称为引用类型,默认为null。
用new创建对象
.的用法:对象 . 成员变量 , 对象 . 方法