什么是构造器呢?
在面向对象编程中,构造器(Constructor) 是类中一种特殊的方法,主要用于在创建类的实例(对象)时初始化对象的状态。它会在对象被创建的瞬间自动调用,确保对象在使用前具备必要的初始值或完成必要的准备工作。
构造器有无参构造器和有参构造器两种,如何一眼定位哪个方法是构造器呢?注意,构造器的名称与类的名称完全相同,同时没有返回值与返回类型!!!!
举个例子,还是大家常见的学生类,这里生成了无参与有参两种构造器,无参顾名思义,没有参数,有参则将Student这个类的所有属性写入,可以发现无论是哪种构造器,都没有返回类型
public class Student {
private String name;
public double weight;
public double height;
public Integer Math;
private Integer Chinese;
public Student() {
}
public Student(String name, double weight, double height, Integer math, Integer chinese) {
this.name = name;
this.weight = weight;
this.height = height;
this.Math = math;
this.Chinese = chinese;
}
}
那么构造器有什么用呢?当然是来初始化的呀
无参构造可以什么都不写,这时系统会自动给变量设置默认值,我们也可以在里面自己给变量设置想要的初始值:
public Student() {
this.Math = 90;
this.Chinese = 80;
}
有参构造是用来在类的外部赋值的,在main方法中new对象时,会自动匹配调用我们类中的构造器,不给参数就会调用无参构造,给值就调用有参构造
我们在上面的无参构造给了Math默认值90,有参构造用来接外面给过来的值,我们看示例:
public class Main {
public static void main(String[] args) {
Student student1 = new Student();
Student student2 = new Student("zzs",75,182,92,88);
System.out.println(student1.Math);
System.out.println(student2.Math);
}
}
这里student1调用无参中我们设置的默认值90,student2的第四个值92是math的值,所以打印出来的结果是90和92
特别注意:当我们不写构造器时,系统里是默认有无参构造器的,但是当我们写了有参构造器时,系统的无参构造器就会消失,所以我们最好是要写就都写,要不写就都不写
什么是封装?
简单来说,就是该藏的藏起来,该显示的显示出来,举例:
我们引出访问权限修饰符的概念:
- 私有属性:用 private 修饰属性,阻止外部直接访问或修改。
- 公共接口:用 public 修饰方法,作为外部与对象交互的唯一途径,在方法中可以加入验证逻辑,确保数据的合法性。
我们根据实例来理解一下,上面的代码中,数学(Math)和语文(Chinese)用来记录成绩,分别用修饰符public,和private修饰,private修饰的语文被藏了起来,我们在外部调用时不能访问,但是public修饰的数学我们却可以访问:
可以看到语文报错了,因为他藏了起来,那我们该如何调用呢?
这里就用到了set与gert方法,set用来传值也就是赋值,get用来把我们的数据取出来
public Integer getChinese() {
return Chinese;
}
public void setChinese(Integer chinese) {
this.Chinese = chinese;
}
这时我们想要再从外面给语文赋值时,就可以用方法来存取了:
其实,完整的封装就是我们提到的权限修饰符与set,get方法两者结合起来,我们来看一下封装的定义:
封装的实现方式( Java ):
通常通过访问权限修饰符(如 private、public、protected)控制属性和方法的可见性,并为私有属性提供公共的 “访问接口”(getter 方法)和 “修改接口”(setter 方法)。
那么有些同学就要问了,为啥要封装呀?
想个问题:如果不封装,就相当与我们现在的public修饰的数学,随便来个人都可以给他赋值改值,那么如果现在这个数学成绩是我的,我的死对头发现了这个数据,给math赋值成-50分,也是可以的,但是像我的语文成绩他就随意访问不了,修改必须调用set方法;那么又有同学问了,他访问了set方法不是照样可以乱改我的成绩吗?所以我们通常会在set方法里做一个数据的校验,这里用语文成绩为例:语文成绩必须在60-100之间,修改我们之前的set方法
public void setChinese(Integer chinese) {
if(chinese<60 || chinese>100){
System.out.println("成绩不合理");
}else {
this.Chinese = chinese;
}
}
那么这时再去给语文赋值时,就会进行校验:
这里的80是我们构造器给的语文成绩初始值,也就是说我的死对头想把我80分的语文成绩改为50分的计划没有成功,我们保护了我们的成绩。当然,这里还存在一些问题,这里只是对数据进行了有限的范围校验,如果他改成61分也是可以通过的,所以在真正的业务中,成绩修改除了对范围的校验外,还有身份校验,权限校验等等,这些校验一般会在特定的板块或者前端来实现,逐渐的我们的set方法中也不写一些校验了,把这种校验放在其他地方去实现,但是在代码开发中,我们保留了这种封装模式的书写风格,成为一种书写规范。
我们来规范一下我们的Student类:
public class Student {
private String name;
public double weight;
public double height;
public Integer Math;
private Integer Chinese;
public Student() {
}
public Student(String name, double weight, double height, Integer math, Integer chinese) {
this.name = name;
this.weight = weight;
this.height = height;
this.Math = math;
this.Chinese = chinese;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Integer getMath() {
return Math;
}
public void setMath(Integer math) {
this.Math = math;
}
public Integer getChinese() {
return Chinese;
}
public void setChinese(Integer chinese) {
this.Chinese = chinese;
}
}
在IDEA中,我们可以用Alt+insert来快捷添加无参,有参构造,set,get方法,还有tostring,哈希函数等等
今天的你长知识了吗?喜欢小编的可以点点赞,加个关注~,持续更新java相关知识,后续会有一些简单的项目和大项目
您的点赞和关注是我更新的动力,感谢支持~
评论前必须登录!
注册