Java OOP day02

笔记:
1.方法的签名:方法名+参数列表。java规定:同一个类中,不可以出现多个签名完全相同的方法

2.方法的重载(Overload):
1)发生在同一个类中,方法名称相同,参数列表不同
2)编译器在编译时会根据自动签名绑定调用的方法

3.构造方法:构造函数、构造器、构建器
1)常常用于给成员变量赋初值(初始化对象)
2)与类同名,没有返回值类型
3)在创建对象时被自动调用
4)若自己不写构造,则编译器默认一个无参构造
若自己写了构造,则不再默认提供
5)构造方法可以重载

4.this:指代当前对象,哪个对象调指的就是哪个对象
只能用在方法中,方法中访问成员变量之前默认有个this.
this的用法:
1)this.成员变量名—————访问成员变量
2)this.方法名()—————–调用方法
3)this()————————调用构造方法

5.引用类型数组:
1)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);
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][4]; //3行4列
arr[0] = new int[3];
arr[1] = new int[2];
arr[2] = new int[3];
arr[1][0] = 100; //给arr中第2个元素中的第1个元素赋值为100

补充:
1)同一个文件中,可以包含多个类
2)public修饰的类只能有一个
3)public修饰的类必须与文件名相同
4)主函数为程序的入口,如果定义错误不能使用

HW:
1.完成俄罗斯方块T类格子,并测试

public class T {
//属性四个格子,格子数组
Cell[] cells;
T(int r,int c){
cells=new Cell[4];//创建格子数组
this.cells[0]=new Cell(r,c);//用参数控制
this.cells[1]=new Cell(r,c+1);
this.cells[2]=new Cell(r,c+2);
this.cells[3]=new Cell(r+1,c+1);
}

T(){
    this(1,1);//起始坐标默认值 
}

void Drop(){
    for(int amount=0;amount<this.cells.length;amount++){
        this.cells[amount].row++;//四个格子一起落,可用for循环。
    }

}
void MoveLeft(){
    for(int amount=0;amount<this.cells.length;amount++){
        this.cells[amount].col--;
    }
}
void MoveRight(){
    for(int amount=0;amount<this.cells.length;amount++){
        this.cells[amount].col++;
    }
}
void PrintRCInfo(){//同样四个格子的行列信息一起打印
    for(int amount=0;amount<this.cells.length;amount++){
        String receipt=cells[amount].GetCellInfo();
        System.out.println(receipt);
    }
}


public static void main(String[] args) {
    T t=new T();
    System.out.println("初始坐标:");
    System.out.println("下落后:");
    t.Drop();
    t.PrintRCInfo();
    System.out.println("左移后");
    t.MoveLeft();
    t.PrintRCInfo();
    System.out.println("右移后");
    t.MoveRight();
    t.PrintRCInfo();

}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值