1.方法的签名:方法名+参数列表
2.方法的重载(Overload):
1)发生在同一个类中,方法名称相同,参数列表不同
2)编译器在编译时会根据签名自动绑定调用的方法
3.构造方法:构造函数、构造器,构建器
1)给成员变量赋初值
2)与类同名,没有返回值类型
3)在创建(new)对象时被自动调用
4)若自己不写构造方法,则编译器默认无参构造方法
若自己写了构造方法,则不再默认提供
5)构造方法可以重载
4.this:指代当前对象,哪个对象调方法this指的就是哪个对象
只能用在方法中,方法中访问成员变量之前默认有个this.
this的用法:
1)this.成员变量名--------------访问成员变量
2)this.方法名()----------------调用方法
3)this()-----------------------调用构造方法
5.引用类型数组:
1)Cell[] cells = new Cell[4]; //创建Cell数组对象
cells[0] = new Cell(2,5); //创建Cell对象
cells[1] = new Cell(2,6);
cells[2] = new Cell(2,7);
cells[3] = new Cell(3,6);
2)Cell[] cells = new Cell[]{
new Cell(2,5),
new Cell(2,6),
new Cell(2,7),
new Cell(3,6)
};
3)int[][] arr = new int[3][];
arr[0] = new int[2];
arr[1] = new int[3];
arr[2] = new int[2];
arr[1][0] = 100; //给arr中第2个元素中的第1个元素赋值为100
4)int[][] arr = new int[3][4]; //3行4列
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
arr[i][j] = 100;
}
2.方法的重载(Overload):
1)发生在同一个类中,方法名称相同,参数列表不同
2)编译器在编译时会根据签名自动绑定调用的方法
3.构造方法:构造函数、构造器,构建器
1)给成员变量赋初值
2)与类同名,没有返回值类型
3)在创建(new)对象时被自动调用
4)若自己不写构造方法,则编译器默认无参构造方法
若自己写了构造方法,则不再默认提供
5)构造方法可以重载
4.this:指代当前对象,哪个对象调方法this指的就是哪个对象
只能用在方法中,方法中访问成员变量之前默认有个this.
this的用法:
1)this.成员变量名--------------访问成员变量
2)this.方法名()----------------调用方法
3)this()-----------------------调用构造方法
5.引用类型数组:
1)Cell[] cells = new Cell[4]; //创建Cell数组对象
cells[0] = new Cell(2,5); //创建Cell对象
cells[1] = new Cell(2,6);
cells[2] = new Cell(2,7);
cells[3] = new Cell(3,6);
2)Cell[] cells = new Cell[]{
new Cell(2,5),
new Cell(2,6),
new Cell(2,7),
new Cell(3,6)
};
3)int[][] arr = new int[3][];
arr[0] = new int[2];
arr[1] = new int[3];
arr[2] = new int[2];
arr[1][0] = 100; //给arr中第2个元素中的第1个元素赋值为100
4)int[][] arr = new int[3][4]; //3行4列
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
arr[i][j] = 100;
}
}
package oo.day02; //格子类 public class Cell { int row; //行号 int col; //列号 Cell(){ this(0,0); //调用构造方法 } Cell(int n){ this(n,n); //调用构造方法 } Cell(int row,int col){ this.row = row; this.col = col; } void drop(){ //下落1格 row++; } void moveLeft(int n){ //左移n格 col-=n; } String getCellInfo(){ //获取行号和列号 return row+","+col; } void drop(int n){ //下落n格 row+=n; } void moveLeft(){ //左移1格 col--; } }
package oo.day02; //格子类的测试类 public class CellTest { public static void main(String[] args) { Cell c1 = new Cell(); //调无参构造 Cell c2 = new Cell(3); //调1个参的构造 Cell c3 = new Cell(2,5); //调2个参的构造 printWall(c3); /* Cell c = new Cell(); c.row = 2; c.col = 5; c.drop(); c.drop(2); c.moveLeft(); c.moveLeft(3); printWall(c); */ } //打墙+打格 public static void printWall(Cell cc){ for(int i=0;i<20;i++){ //行 for(int j=0;j<10;j++){ //列 if(i==cc.row && j==cc.col){ //行列匹配 System.out.print("* "); }else{ System.out.print("- "); } } System.out.println(); //换行 } } }
package oo.day02; //J型 public class J { Cell[] cells; J(){ this(0,0); } J(int row,int col){ cells = new Cell[4]; cells[0] = new Cell(row,col); cells[1] = new Cell(row+1,col); cells[2] = new Cell(row+2,col-1); cells[3] = new Cell(row+2,col); } void drop(){ for(int i=0;i<cells.length;i++){ cells[i].row++; } } void moveLeft(){ for(int i=0;i<cells.length;i++){ cells[i].col--; } } void moveRight(){ for(int i=0;i<cells.length;i++){ cells[i].col++; } } void print(){ //输出4个格子的行号和列号 for(int i=0;i<cells.length;i++){ String str = cells[i].getCellInfo(); System.out.println(str); } } }
package oo.day02; //T型与J型的测试类 public class TJTest { public static void main(String[] args) { T t = new T(2,5); t.print(); System.out.println("下落后:"); t.drop(); t.print(); System.out.println("左移后:"); t.moveLeft(); t.print(); } }
package oo.day02; //引用数组的演示 public class RefArrayDemo { public static void main(String[] args) { Cell[] cells = new Cell[4]; cells[0] = new Cell(2,5); cells[1] = new Cell(2,6); cells[2] = new Cell(2,7); cells[3] = new Cell(3,6); Cell[] cs = new Cell[]{ new Cell(2,5), new Cell(2,6), new Cell(2,7), new Cell(3,6) }; int[][] arr = new int[3][]; arr[0] = new int[2]; arr[1] = new int[3]; arr[2] = new int[2]; arr[1][0] = 100; int[][] as = new int[3][4]; for(int i=0;i<as.length;i++){ for(int j=0;j<as[i].length;j++){ as[i][j] = 100; } } } }
package oo.day02; /* * 补充: * 1)同一个文件中,可以包含多个类 * 2)public修饰的类只能有一个 * 3)public修饰的类必须与文件同名 */ //重载的演示 public class OverloadDemo { public static void main(String[] args) { Aoo o = new Aoo(); o.say(); o.say("zhangsan"); o.say(25); o.say("zhangsan", 25); o.say(25, "zhangsan"); } } class Aoo{ void say(){} void say(String name){} void say(int age){} void say(String name,int age){} void say(int age,String name){} //int say(){return 1;} //编译错误,重载与返回值类型无关 //void say(String address){} //编译错误,重载与参数名称无关 }
package oo.day02; //T型 public class T { Cell[] cells; //格子数组 T(){ this(0,0); } T(int row,int col){ this.cells = new Cell[4]; //创建Cell数组对象 this.cells[0] = new Cell(row,col); //创建Cell对象 this.cells[1] = new Cell(row,col+1); this.cells[2] = new Cell(row,col+2); this.cells[3] = new Cell(row+1,col+1); } void drop(){ for(int i=0;i<this.cells.length;i++){ this.cells[i].row++; } } void moveLeft(){ for(int i=0;i<this.cells.length;i++){ this.cells[i].col--; } } void moveRight(){ for(int i=0;i<this.cells.length;i++){ this.cells[i].col++; } } void print(){ //输出4个格子的行号和列号 for(int i=0;i<this.cells.length;i++){ String str = this.cells[i].getCellInfo(); System.out.println(str); } } }