RE:JAVA学习-了解对象和类+数组

  1. 方法的签名:方法名+参数列表

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

public class OverloadDemo {

    public static void main(String[] args) {
        a();
        a(1);
    }   
    public static void a(){
        System.out.println("没有参数的方法a");
    }
    public static void a(int i){
        System.out.println("参数为int型数值为"+i+"的方法a");
    }

}

二.构造方法: 语法: [访问修饰符] 类名(){//构造方法体}
1>给成员变量赋初值
2>与类同名,没有返回值类型
3>在创建对象时被自动调用
4>若自己不写构造方法,则编译器默认提供一个无参构造方法
若自己写了构造方法,则编译器不再默认提供
5>构造方法可以重载

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

public class Test {
    private int a;
    public Test(){
        this.a=1;//给成员变量赋值
        this.show();//调用show方法
    }
    public Test(int a){
        this();//调用构造方法
        this.a=a;//通过有参构造方法给成员变量赋值
    }
    public void show(){
        System.out.println("show");
    }
}

四.引用类型数组
引用类型也可以写成数组形式,该数组的各个位置存储的元素为该引用类型的对象.
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][]; //数组的数组
arr[0] = new int[2];
arr[1] = new int[3];
arr[2] = new int[2];
arr[1][0] = 100;

4>int[][] arr = new int[3][4]; //3行4列
for(int i=0;i

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值