Java入门9——类和对象+this+构造方法

终于终于,我们开启了Java的大门!类和对象才是Java梦开始的地方,让我们一起来学习吧!


一、类和对象

通常来讲,一个Java文件里面写一个类,那么类是什么呢?

类,字面意思,就是一类东西,比如说狗就是一个大类,具体分到那么多品种~

那么接下来我们以狗举例子~

我们先写一个关于狗的类:

public class Dog {
    public String name;//一只狗有名字
    public int age;//一只狗有年龄
    public void action(){
        System.out.println("Dog Action");//一只狗还会有它的行为
    }
}

我们用图来分析一下这个类:

那么我们如何调用狗这个类里面的方法呢?

我们就要new一个对象了~

public class Dog {
    public String name;
    public int age;
    public void action(){
        System.out.println("Dog Action");
    }

    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.age = 18;
        dog.name = "WANGWANG";
        System.out.println(dog.name);
        System.out.println(dog.age);
        dog.action();
    }

}

其中 dog 就是你新建的一个对象,然后通过 对象.方法 就可以调用类里面的方法了,定义对象的名字,年龄也是同样适用的~

给大家看一下输出结果~

 

这就是我们所讲的类和对象 

二、this关键字

讲了类和对象,那就一定要提到this关键字,我们依旧以狗举例子~

假设我们又定义了狗的出生年月日,我们要往里传参,不巧形参和实参同名,这个时候我们打印出来的结果又是怎样的呢?

public class Dog {
    public String name;
    public int age;
    public void action(){
        System.out.println("Dog Action");
    }



    public int year;
    public int month;
    public int day;
    public void birthday(int year, int month, int day) {
        year = year;
        month = month;
        day = day;
    }
    public void printDate(){
        System.out.println(year + "-" + month + "-" + day);
    }





    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.age = 18;
        dog.name = "WANGWANG";
        System.out.println(dog.name);
        System.out.println(dog.age);
        dog.action();
        
        

        dog.birthday(1990, 1, 1);
        dog.printDate();
    }

}

运行一下,我们发现结果如下:

 

我们发现狗的出生年月日竟然是0-0-0,我们不是传参1990,1,1吗?

原来这个时候,是因为形参和实参出了问题

 

这里的 year = year ;month = month;day = day;就相当于,形参自己给自己赋值了,所以输出结果是0-0-0,那么我们如何避免这个问题呢?很简单,我们只需要加上this~

我们再把改过之后的代码放出来 

public class Dog {
    public String name;
    public int age;
    public void action(){
        System.out.println("Dog Action");
    }

    public int year;
    public int month;
    public int day;
    public void birthday(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public void printDate(){
        System.out.println(year + "-" + month + "-" + day);
    }

    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.age = 18;
        dog.name = "WANGWANG";
        System.out.println(dog.name);
        System.out.println(dog.age);
        dog.action();


        dog.birthday(1990, 1, 1);
        dog.printDate();
    }

}

我们再来看一下运行结果:

 

这个时候就是正常的!

那么this有哪些用法呢?

我们先给大家总结出来,然后一条一条用代码验证~

this

1. 可以访问当前对象的成员变量

2.可以访问当前对象的成员方法

3.通过this调用当前对象的其他构造方法

PS: 静态方法当中不使用this(以后细说)

我们先学前两条用法!~还是以狗为例子,代码如下:

public class Dog {
    public String name;
    public int age;
    public void action(){
        System.out.println("Dog Action");
    }

    public int year;
    public int month;
    public int day;
    public void birthday(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public void printDate(){
        System.out.println(year + "-" + month + "-" + day);
    }
    public void func(){
        this.age = 18;
        this.name = "wangwang";
        this.printDate();
    }

    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.birthday(1990, 1, 1);
        dog.func();
        System.out.println(dog.name);
        System.out.println(dog.age);
        dog.action();
        
    }

}

 

这就是this的前两条功能

那第三个用法是怎么样的呢?我们要先学一下构造方法~

三、构造方法

注意:构造方法和成员方法是不一样的!!!

 1.我们为什么要学构造方法呢?

通常我们会用构造方法初始化成员变量

2.构造方法长啥样?

还是以狗为例子,我们又回到了起点,这次我们在狗的类里,只定义了名字和年龄

这个时候,我们把不带参数的构造方法写出来~

public class Dog1 {
    public String name;
    public int age;

    public Dog1() {
        
    }

    public static void main(String[] args) {
        Dog dog = new Dog();

        System.out.println(dog.name);
        System.out.println(dog.age);
    }

}

 当我们写一个类的时候,没写构造方法的时候,构造方法是默认有一个不带参数的构造方法的

3.构造方法初始化成员变量

构造方法可以重载,所以我们来试一试用构造方法初始化成员变量

public class Dog1 {
    public String name;
    public int age;

    public Dog1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public static void main(String[] args) {
        Dog1 dog = new Dog1("wang",12);
        
        System.out.println(dog.name);
        System.out.println(dog.age);
    }
}

我们发现此时是带两个参数的构造方法,而且我们都初始化了成员变量

那么,构造方法是什么时候调用的呢?看下面这张图就理解啦!

原来是在 new 一个对象的时候就调用了构造方法,这就是我们一般用来初始化成员变量的方法~


今天内容就到这里啦~东西有点多,大家好好复习,反复搓代码,加油!!! 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Re.不晚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值