Java类与对象详细讲解(下)

目录

一、this的引入

1.1 this的定义

1.2  this的特性

二、static介绍

2.1  静态成员

2.2 静态成员变量

2.3  静态成员方法

三、代码块


在这里我们回顾了上一篇文章:Java类与对象详细讲解(上),认识了什么是类,什么是对象,以及类与对象的概念和创建。接下来我将带领大家继续学习类与对象剩余部分知识点。

一、this的引入

1.1 this的定义

我们先来看一个例子,代码如下:

public class Date {
   public int year;
   public int month;
   public int day;
public void setDay(int y, int m, int d){ //形参y,m,d
   year = y;  //成员变量 year、month、day
   month = m;//这里有个思考:如果形参名字和成员变量名字相同,当对象调用时,程序会发生什么?                                              
   day = d;
}
public void printDate() {
   System.out.println(year + "/" + month + "/" + day);
  }

public static void main(String[] args) {
// 构造三个日期类型的对象 d1 d2 d3
   Date d1 = new Date();
   Date d2 = new Date();
   Date d3 = new Date();
// 对d1,d2,d3的日期设置
   d1.setDay(2020,9,15);
   d2.setDay(2020,9,16);
   d3.setDay(2020,9,17);
   d1.printDate();
   d2.printDate();
   d3.printDate();
  }
}

上述代码中形参和成员变量不相同时,程序编译运行ok,正常输出。

那我们再来看这个思考题,如果形参名字和成员变量名字相同,当对象调用时,程序会发生什么?

那我们就在编译器中修改对应代码去验证一下

 public void setDay(int year, int month, int day){ //形参y,m,d
       year = year;  //成员变量 year、month、day
       month = month;//这里有个思考:如果形参名字和成员变量名字相同,当对象调用,程序会发生什么
        day = day;
    }

       结果我们发现形参名不小心与成员变量名相同,那函数体中到底是谁给谁赋值?成员变量给成员变量?参数给参数?参数给成员变量?成员变量参数?估计 自己都搞不清楚了。所以此时编译运行程序会得到结果为默认值,对象调用方法赋的值并没有成功。

此时当我们在成员变量前添加this引用后再运行代码,此时代码编译运行成功,输出对应结果

 public void setDay(int year, int month, int day){ //形参year,month,day
        this.year = year;  //成员变量 year、month、day
        this.month = month;
        this.day = day;
    }
    public void printDate() {
        System.out.println(this.year + "/" + this.month + "/" + this.day);
    }

在这里我们便引入了this的概念

this 引用指向 调用该成员方法的对象 ,在成员方法中所有成员变量的操作,都是通过this 引用去访问 。只不过所有的操作对用户是透明的,即用户不需要来传递,编译器自动完成

1.2  this的特性

1. this 的类型:对应类的类型引用,即哪个对象调用就是哪个对象的引用类型
2. this 只能在 "非静态 成员方法 " 中使用
3. " 成员方法 " 中, this 只能引用当前对象,不能再引用其他对象
4.this也可以在构造方法中引用,但必须放在构造方法里面第一条语句
5. this 成员方法 第一个隐藏的参数,编译器会自动传递,在成员方法执行时,编译器会负责将调用成员方法 对象的引用传递给该成员方法, this 负责来接收
this.year ;     访问当前对象的成员变量
this.fun();     访问当前对象的成员方法
this();           调用当前对象的其他构造方法
这里有一条需要说明的点: this不能调用静态成员方法和属性,即static修饰的成员方法
额外补充知识点:成员变量与局部变量的区别
成员变量:定义在类的内部,方法的外部
局部变量:定义在方法内部,类的外部

二、static介绍

      在Java中  static是关键字。一句话描述static关键字就是:方便在没有创建对象的情况下进行调用(方法/变量)。被static关键字修饰的方法或者变量不需要依赖于对象来进行访问,只要类被加载了,就可以通过类名去进行访问。

2.1  静态成员

      在 Java 中,被 static 修饰的成员,称之为静态成员,也可以称为类成员,其不属于某个具体的对象,是所有对象所共享的。同理static修饰的成员变量,称为静态成员变量。static修饰的成员方法,称为静态成员方法。
      

2.2 静态成员变量

1. 不属于某个具体的对象,是类的属性,所有对象共享的,不存储在某个对象的空间中
2. 既可以通过对象访问,也可以通过类名访问,但一般更推荐使用类名访问
public class Student{
   public String name;
   public String gender;
   public int age;
   public static String classRoom = "Bit306"; //classRoom此时为静态成员变量

public static void main(String[] args) {
// 静态成员变量可以直接使用 "类名.静态成员变量名" 访问
System.out.println(Student.classRoom);

Student s1 = new Student("Li leilei", "男", 18);
Student s2 = new Student("Han MeiMei", "女", 19);
Student s3 = new Student("Jim", "男", 18);

// 也可以通过 "对象.静态成员变量名" 访问:但是classRoom是三个对象共享的
System.out.println(s1.classRoom);
System.out.println(s2.classRoom);
System.out.println(s3.classRoom);
  }
}
3. 静态成员变量存储在方法区当中
4. 生命周期伴随类的一生 ( 即:随类的加载而创建,随类的卸载而销毁 )

2.3  静态成员方法

static 修饰的成员方法称为静态成员方法,是类的方法,不是某个对象所特有的 。静态成员一般是通过静态方法来访问的。
public class Student{

   private static String classRoom = "Bit306"; //static修饰的成员变量

   public static String getClassRoom(){ //static修饰的成员方法
         return classRoom;
    }
}
public class TestStudent {
   public static void main(String[] args) {
       System.out.println(Student.getClassRoom());//通过 "类.静态成员方法名()" 来调用
   }
}

静态方法特性:

1. 不属于某个具体的对象,是类方法,所有对象共享的,不存储在某个对象的空间中
2. 可以通过对象调用,也可以通过类名 . 静态方法名 (...) 方式调用,更推荐使用后者
3. 不能在静态方法中访问任何非静态成员变量
public static String getClassRoom(){
  System.out.println(name);
   return classRoom;
}
// 编译失败:Error:(35, 28) java: 无法从静态上下文中引用非静态变量 name


public static String getClassRoom(){
    age += 1;
   return classRoom;
}
// 编译失败:Error:(35, 9) java: 无法从静态上下文中引用非静态变量 age
4. 静态方法中不能调用任何非静态方法,因为非静态方法有this参数,在静态方法中调用时候无法传递this引用
public static String getClassRoom(){
   func();
   return classRoom;
}
// 编译报错:Error:(35, 9) java: 无法从静态上下文中引用非静态方法 func()

三、代码块

代码块包括 普通代码块定义在方法中的代码块), 静态代码块(static修饰的代码块)、 实例代码块定义在类中的代码块 ( 不加修饰符 ))和 同步代码块(此同步代码块后续再谈)
3.1  普通代码块:定义在方法中的代码块
public class Main{
   public static void main(String[] args) {
    { //直接使用{}定义,普通方法块
        int x = 10 ;
     System.out.println("x1 = " +x);
  }
}

3.2  实例代码块定义在类中的代码块(不加修饰符)

public class Student{
   //实例成员变量
    private String name;
    private String gender;
    private int age;
       
   {   //实例代码块
      this.name = "bit";
      this.age = 12;
      this.sex = "man";
   System.out.println("I am instance init()!");
}

public Student() {
    System.out.println("I am Student init()!");
}

public void show(){
  System.out.println("name: "+name+" age: "+age+" sex: "+sex);
  }
}
public class Main {
    public static void main(String[] args) {
       Student stu = new Student();
               stu.show();
  }
}

3.3   静态代码块(static修饰的代码块)
public class Student{
   private String name;
   private String gender;
   private int age;
   private double score;
   private static String classRoom;

  { //实例代码块
     this.name = "bit";
     this.age = 12;
     this.gender = "man";
   System.out.println("I am instance init()!");
 }

   static {  // 静态代码块
            classRoom = "bit306";
       System.out.println("I am static init()!");
}
public Student(){
     System.out.println("I am Student init()!");
}
public static void main(String[] args) {
     Student s1 = new Student();
     Student s2 = new Student();
  }
}
注意事项
(1)静态代码块不管生成多少个对象,其只会执行一次
(2)如果一个类中包含多个静态代码块,在编译代码时,编译器会按照定义的先后次序依次执行
(3)实例代码块只有在创建对象时才会执行
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值