类和对象
目录
- 理解面对对象
- 类的概念及定义
- 类的实例化
- this引用
- 对象的初始化
1、面对对象
面对对象是一种解决问题的思想,主要依靠对象间的交互完成一件事情。
让我们用洗衣服的例子来理解,当我们手洗衣服的时候,我们需要理解如何搓衣服,放洗衣粉等步骤,这就是面对过程。但是当我们使用洗衣机洗衣服时,我们只需要与洗衣粉,衣服,洗衣机等对象交互即可,不需要明白洗衣机洗衣服的过程,这就是面向对象。
2、类的概念及定义
**类是用来对一个对象(实体)来进行描述的。**比如,我们可以将人定义为一个类,然后在其中描述人的年龄,性别等,描述完就可以被计算机识别了。
类的定义格式如下:
public class ClassName {
field;//字段属性
method;//行为或者成员方法
}
我们使用关键字class来定义类,ClassName为类的名字(大驼峰式定义),{ }中为类的主体。例如,我们可以来定义一个Student类。
public class Student {
public String name;
public String gender;
public short age;
public double score;
public void DoClass(){}
public void DoHomework(){}
public void Exam(){}
}
值得注意的是:
1、一般一个文件当中只定义一个类。
2、main方法所在类用public修饰。
3、public修饰的类名必须和文件名相同。
4、不要随意修改public修饰类的名称,要通过开发工具修改。
3、类的实例化
**定义了一个类就相当于在计算机中定义了一个新的类型。**我们就可以使用关键字new,用类类型创建对象,这个过程被称为类的实例化。比如结合上述的Student类,可以在main方法中实例化对象student1、student2,还可以通过“ . ”来访问对象中的属性和方法:
public static void main(String[] args) {
Student student1 = new Student();
student1.name = "老六";
student1.DoClass();
Student student2 = new Student();
}
说明:
1、类只是一个模型。
2、类是一种自定义的类型。
3、实例化出的对象占用实际的物理空间,存储类成员变量。
4、this引用
public class Date {
public int year;
public int month;
public int day;
public void setDay(int y, int m, int d) {
year = y;
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();
}
}
这是我们定义了一个日期类,可以清晰地明白在setDay内部是参数赋值给成员变量。但是,当形参名不小心与成员变量名相同:
public void setDay(int year, int month, int day) {
year = year;
month = month;
day = day;
}
那函数体中到底是谁给谁赋值?成员变量给成员变量?参数给参数?参数给成员变量?成员变量参数?
其次,三个对象都在调用setDate和printDate函数,但是这两个函数中没有任何有关对象的说明,setDate和printDate函数如何知道打印的是那个对象的数据呢?
这就要使用到this引用。
**this引用指向当前对象(成员方法运行时调用该成员方法的对象),在成员方法中所有成员变量的操作,都是通过该引用去访问。**例如:
public void setDay(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
注意:this引用的是调用成员方法的对象。
this引用的特性:
1、this只能在成员方法中使用。
2、只能引用当前对象。
5、对象的初始化
在java方法内部定义局部变量时,如果不对其初始化,编译会失败。
可是对象的字段即使没有声明仍然可以使用,这是为什么呢?
构造方法
构造方法(也称为构造器)是一个特殊的成员方法,名字必须与类名相同,在创建对象时,由编译器自动调用,并且在整个对象的生命周期内只调用一次。
public class Date {
public int year;
public int month;
public int day;
public Date(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
System.out.println("Date(int,int,int)方法被调用了");
}
public static void main(String[] args) {
// 此处创建了一个Date类型的对象,并没有显式调用构造方法
Date d = new Date(2021,6,9); // 输出Date(int,int,int)方法被调用了
}
}
注意:
1、没有返回值,设置为void也不行。
2、构造方法可以重载(用户根据自己的需求提供不同参数的构造方法)。
public class Date {
public int year;
public int month;
public int day;
// 无参构造方法
public Date(){
this.year = 1900;
this.month = 1;
this.day = 1;
}
// 带有三个参数的构造方法
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
上述Date类中,没有定义任何构造方法,编译器会默认生成一个不带参数的构造方法。注意:一旦用户定义,编译器则不再生成。
构造方法中,可以通过this调用其他构造方法来简化代码。
注意:
1、this(…)必须是构造方法中第一条语句。
2、不能形成环。
public class Date {
public int year;
public int month;
public int day;
public Date(){
//System.out.println(year); 注释取消掉,编译会失败
this(1900, 1, 1);
}
}
默认初始化
Date d = new Date(2021,6,9);
在程序层面只是简单的一条语句,在JVM层面需要做好多事情,下面简单介绍下:
- 检测对象对应的类是否加载了,如果没有加载则加载
- 为对象分配内存空间
- 处理并发安全问题
比如:多个线程同时申请对象,JVM要保证给对象分配的空间不冲突 - 初始化所分配的空间
即:对象空间被申请好之后,对象中包含的成员已经设置好了初始值,比如:
数据类型 默认值
byte 0
char ‘\u0000’
short 0
int 0
long 0L
boolean false
float 0.0f
double 0.0
reference null
就地初始化
public class Date {
public int year = 1900;
public int month = 1;
public int day = 1;
}