2022.5.17 关于JAVA 类和对象(基础知识)

目录

 this 关键字

构造方法


我们知道 int byte char double 等等都是基本的类型,此时我们想自定义一个类呢?

举例1:

//创建一个用来洗衣服的机器的类
class WashMachine{
    //以下定义的为 属性或字段 所谓的成员变量  特点:在类的里面 在方法的外面
   public String brand; //该机器的品牌名称
   public double weight; //该机器的重量
    public double price; //该机器的价格

    //以下定义的为 行为或者方法 所谓的成员方法
    public void WashClothes(){
        int a=10;//此时在方法中定义的变量为局部变量
        System.out.println(brand+"正在洗衣服中");//在方法内可以直接使用其类中的成员变量
    }
    public void dryClothes(){
        System.out.println(brand+"正在脱水中");
    }
}

注意:类名必须采用大驼峰的方式定义!(WashMachine 首字母大写)


我们自定义了一个类后,当我们想去使用这个新定义的类型时,便需要实例化我们的类型,也就是所谓的用类类型创建对象的过程。

举例2:

//定义一个狗的类型
class Dog{
    public String name;
    public int age;
    public String color;

    public void bark(){
        System.out.println(name+"正在汪汪汪!");
    }
    public void eat(){
        System.out.println(name+"正在吃狗粮!");
    }
}

public class Test {
    public static void main(String[] args) {
        // 一般使用new关键字来实例化一个对象
        Dog dog =new Dog();
        //上行代码便是实例化了一个 Dog类型的对象,并将其赋值给 Dog类型的dog变量
        //我们还需明白 dog实际存储的是 new Dog() 这一对象的地址
        System.out.println(dog.name);//我们可以采用该方法来访问对象中的成员变量
        System.out.println(dog.age);
        dog.eat();//访问对象中的成员方法
        dog.bark();
        dog.color="黄色";//我们也可以以这种方式来给对象中的成员变量赋值
        System.out.println(dog.color);

        Dog dog2 =new Dog();
        //通过我们自定义 Dog类可以实例化多个对象,且每个对象都有着自己的 name age color 互不干扰
        Dog dog3 =new Dog();
    }
}

输出结果如下:

我们发现当没有定义 Dog 类中的成员变量的时候,引用它时便会输出其默认定义的初始值

各类型初始值如下表所示:

类型名称初始值
引用数据类型    如 String等null
 int byte short 类型0
long 类型0L
double类型0.0
float类型0.0f
boolean 类型 false
char 类型 '\u0000'

注意:局部变量一定要初始化,不然会报错!

           一般一个文件定义一个类

           main方法所在的类一般要用public修饰

           public修饰的类必须要和文件名相同


就地初始化也是给类中成员变量初始化的一种方式

举例:

//创建一个日期类
class Date{
    public int year=2022;
    public int month=5;
    public int day=8;

    public void printDate(){ //设置一个打印日期的方法
        System.out.println(this.year+" "+this.month+" "+this.day);
    }
}

public class Test2 {
    public static void main(String[] args) {
       Date date1=new Date(); //实例化一个对象
        date1.printDate();
    }
}

输出结果为:

注意:一般不会这么来初始化我们类中的成员变量,因为并不是每个实例化的对象 年 月 日 都是我们就地初始化的值

 this 关键字

引例:

//创建一个日期类
class Date{
    public int year;
    public int month;
    public int day;

    public void setDate(int y,int m,int d){ //创建一个设置日期的方法 
        year=y;
        month=m;
        day=d;
    }

    public void printDate(){ //设置一个打印日期的方法
        System.out.println(year+" "+month+" "+day);
    }
}

public class Test2 {
    public static void main(String[] args) {
       Date date1=new Date(); //实例化一个对象
       date1.setDate(2003,5,8); //给该对象赋值
       date1.printDate();//打印出该对象的值
        Date date2=new Date();
        date2.setDate(2022,5,8);
       date2.printDate();
    }
}

输出结果为: 


引例2: (当我们的setDate 方法中 形参名和成员变量的名字相同时)

//创建一个日期类
class Date{
    public int year;
    public int month;
    public int day;

    public void setDate(int year,int month,int day){ //创建一个设置日期的方法
        year=year;
        month=month;
        day=day;
    }

    public void printDate(){ //设置一个打印日期的方法
        System.out.println(year+" "+month+" "+day);
    }
}

public class Test2 {
    public static void main(String[] args) {
       Date date1=new Date(); //实例化一个对象
       date1.setDate(2003,5,8); //给该对象赋值
       date1.printDate();//打印出该对象的值
        Date date2=new Date();
        date2.setDate(2022,5,8);
       date2.printDate();
    }
}

输出结果为:

我们发现其结果输出都为0,那这是因为什么呢?

形参中的year month day 都是局部变量 ,且有局部变量优先的原则,从而setDate方法中的year=year month=month day=day 都是局部变量 ,不是其类中的成员变量,从而并没有赋值给对象的成员变量,从而打印出来的都是成员变量的初始值 “ 0 ” 


解决该问题的代码:

//创建一个日期类
class Date{
    public int year;
    public int month;
    public int day;

    public void setDate(int year,int month,int day){ //创建一个设置日期的方法
        this.year=year; //可以使用关键字this来解决该情形
        this.month=month;
        this.day=day;
    }

    public void printDate(){ //设置一个打印日期的方法
        System.out.println(this.year+" "+this.month+" "+this.day);//这里我们也最好加上 this 关键字 使其指向更明确
    }
}

public class Test2 {
    public static void main(String[] args) {
       Date date1=new Date(); //实例化一个对象
       date1.setDate(2003,5,8); //给该对象赋值
       date1.printDate();//打印出该对象的值
        Date date2=new Date();
        date2.setDate(2022,5,8);
       date2.printDate();
    }
}

输出结果为: 

tihs的定义:

this引用指向当前对象(成员方法运行时调用该成员方法的对象),在成员方法中所有成员变量的操作,都是通过该引用去访问。

依据上面的例子和其定义可知,当 date1调用setDate方法时,setDate方法中的this代表的就是date1,当 date2调用setDate方法时,setDate方法中的this代表的就是date2。

注意:

this只能在成员方法中使用,且在成员方法中,this只能引用当前对象,不能再引用其他对象

构造方法

概念:

构造方法是一个特殊的成员方法,名字必须和类名相同,在创建对象时,由编译器自动调用,并且再整个对象的生命周期内只调用一次。

举例1:

//创建一个鸟的类型
class Bird{
    public String name;
    public int age;

    public void fly(){
        System.out.println(name+"正在飞翔!");
    }

    public Bird(){  //这里便是构造方法,其方法名与类名相同 其没有返回值
        System.out.println("调用了构造方法!");
    }
//    当我们没有写构造方法时,编译器会自动提供一个不带参数的构造方法
//    形如:
//    public Bird(){
//
//    }
//    对象的初始化分为两大步:
//    先为对象分配内存空间
//    再调用合适的构造方法(构造方法不止一个)
}

public class Test3 {
    public static void main(String[] args) {
        Bird bird=new Bird();  //这里的 Bird() 便是调用我们不带参数的构造方法!
    }
}

输出结果为:


那我们便可以使用构造方法来直接给我们的对象进行初始化 代码如下:

//创建一个鸟的类型
class Bird{
    public String name;
    public int age;

    public void fly(){
        System.out.println(name+"正在飞翔!");
    }

    public Bird(){  //这是不带参数的构造方法
        this.name="二哈";
        this.age=3;
        System.out.println("调用了不带参数的构造方法!");
    }
    public Bird(String name,int age){
        this.name=name;
        this.age=age;
        System.out.println("调用了带参数的构造方法!");
    }

}

public class Test3 {
    public static void main(String[] args) {
        Bird bird=new Bird();  //这里的 Bird() 便是调用我们不带参数的构造方法!
        System.out.println(bird.name);
        System.out.println(bird.age);
        System.out.println("===============");
        Bird bird2=new Bird("大傻",5);//这里的 Bird(大傻,5) 便是调用我们带参数的构造方法!
        System.out.println(bird2.name);
        System.out.println(bird2.age);
    }
}

输出结果为: 

注意:当我们自己提供了构造方法,我们编译器便不会再自动提供不带参数的构造方法了!


引例:(利用this关键字来在一个构造方法中调用另一个构造方法)

//创建一个鸟的类型
class Bird{
    public String name;
    public int age;

    public void fly(){
        System.out.println(name+"正在飞翔!");
    }

    public Bird(){  //这是不带参数的构造方法
        this("大傻",5);
        //我们可以通过关键字this来调用我们的另一个带有三个参数的构造方法
        //且该行代码需写在第一行
        System.out.println("调用了不带参数的构造方法!");
    }
    public Bird(String name,int age){
        this.name=name;
        this.age=age;
        System.out.println("调用了带参数的构造方法!");
    }
}

public class Test3 {
    public static void main(String[] args) {
        Bird bird=new Bird();  //这里的 Bird() 便是调用我们不带参数的构造方法!
        System.out.println(bird.name);
        System.out.println(bird.age);
    }
}

输出结果: 


引例:(利用this关键字来调用一个成员方法)

//创建一个鸟的类型
class Bird{
    public String name;
    public int age;

    public void fly(){
        System.out.println(name+"正在飞翔!");
    }

    public void printShow(){ //设置一个打印鸟信息的方法
        System.out.println("年龄"+this.age+"岁名叫"+this.name+"的鸟");
    }

    public Bird(){  //这是不带参数的构造方法
        this("大傻",5);
        System.out.println("调用了不带参数的构造方法!");
    }
    public Bird(String name,int age){
        this.name=name;
        this.age=age;
        this.printShow();//这里使用了this关键字来调用 成员方法
        System.out.println("调用了带参数的构造方法!");
    }
}

public class Test3 {
    public static void main(String[] args) {
        Bird bird=new Bird();  //这里的 Bird() 便是调用我们不带参数的构造方法!
    }
}

输出结果为: 


以上就是类和对象的基础知识的全部内容!!

如有错误或者能改进的地方 请各大佬指出 我会及时改正!!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

茂大师

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

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

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

打赏作者

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

抵扣说明:

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

余额充值