目录
☂️this的用法
1.访问当前对象的成员变量
2.访问当前对象的成员方法
3.调用当前对象的其他构造方法来简化代码
☂️封装
什么是封装?
访问修饰限定符
☂️static修饰成员变量
☂️static修饰成员方法
☂️this的用法
1.访问当前对象的成员变量
我们来看下面的两组代码:
一、
class Date{ public int year; public int month; public int day; Date(){ }//构造方法 public void setDate(int y,int m,int d){ year = y; month = m; day = d; } public void show(){ System.out.println(year+" 年 "+month+" 月 "+day+" 日 "); } } public class Test { public static void main(String[] args) { Date date = new Date(); date.setDate(2023,07,16); date.show(); } }
运行截图:
二、
class Date{ public int year; public int month; public int day; Date(){ }//构造方法 public void setDate(int year,int month,int day){ year = year; month = month; day = day; } public void show(){ System.out.println(year+" 年 "+month+" 月 "+day+" 日 "); } } public class Test { public static void main(String[] args) { Date date = new Date(); date.setDate(2023,07,16); date.show(); } }
运行截图:
👆仔细对比上面的代码,我们会发现setDate()方法的参数换了一下名称,第二个代码的赋值就失败了;这是因为编译的时候已经分不到等号左右两边的year、month、day哪一个是对象的,哪一个是形参的。
所以我们的代码要尽量习惯加上this,避免这种情况的发生
👇参考下面代码:
class Date{ public int year; public int month; public int day; Date(){ }//构造方法 public void setDate(int year,int month,int day){ this.year = year; this.month = month; this.day = day; } public void show(){ System.out.println(this.year+" 年 "+this.month+" 月 "+this.day+" 日 "); } } public class Test { public static void main(String[] args) { Date date = new Date(); date.setDate(2023,07,16); date.show(); } }
运行截图:
加上this,赋值就成功了;
这个时候加上this表示当前对象,this.year表示访问当前对象的年属性;this.month表示访问当前对象的月属性;this.day表示访问当前对象的日属性;
2.访问当前对象的成员方法
class Date{
public int year;
public int month;
public int day;
Date(){
}//构造方法
public void func1(int year,int month){
this.year=year;
this.month=month;
}
public void func2(int day){
this.func1(2023,7);
this.day=day;
}
public void show(){
System.out.println(this.year+" 年 "+this.month+" 月 "+this.day+" 日 ");
}
}
public class Test {
public static void main(String[] args) {
Date date = new Date();
date.func2(28);
date.show();
}
}
在func2()中,this表示访问当前对象的成员方法
3.调用当前对象的其他构造方法来简化代码
class Date{ public int year; public int month; public int day; public Date(){ System.out.println("这是一个无参的构造方法"); }//构造方法 public Date(int year,int month){ this(); //调用类中的无参构造方法 this.year=year; this.month=month; System.out.println("这是有两个参数的构造方法"); } //构造方法的重载 } public class Test { public static void main(String[] args) { Date date=new Date(2023,7);//创建一个对象的时候会调用构造方法 } }
运行截图:
当创建一个对象的时候,会调用对应的构造方法。上面创建对象的时候传了两个参数,所以调用的是带两个参数的构造方法。带两个参数的构造方法的第一个代码是this(),意思是调用的无参的构造方法,所以先输出的是“这是一个无参的构造方法”。
注意:互相调用的时候不能成环,不然的话会死循环
☂️封装
什么是封装?
就是将类内的一些细节隐藏起来,并且提供一些公开的接口让用户进行交互。
访问修饰限定符
Java中主要通过类和访问权限来实现封装:类可以将数据以及封装数据的方法结合在一起,更符合人类对事物的认
知,而访问权限用来控制方法或者字段能否直接在类外使用。Java中提供了四种访问限定符:
当age被private修饰的时候,age就被封装了起来。我们可以看到通过person1引用是无法访问age的。这个时候如果我们想修改age或者访问age这个属性就可以给它一个对外的接口。
public class PerSon { public String name; private int age; public void setAge(int age) { this.age = age; } public int getAge(){ return age; } } class Test1 { public static void main(String[] args) { PerSon person1 = new PerSon(); person1.name = "张三"; person1.setAge(18); System.out.println(person1.name); System.out.println(person1.getAge()); } }
运行截图:
☂️static修饰成员变量
特性:
1.static修饰的成员变量,称作静态成员变量;
2.静态的成员变量是不属于某一个具体对象的,而是属于类,也叫做类变量;
3.静态成员变量可以通过类名访问,也可以通过对象访问,但是更推荐类名访问,因为它不属于对象;
4.类变量(静态成员变量)存储在方法区;
为什么说静态成员变量不属于某个具体的对象呢?我们来看下面的代码:
通过类名访问classroom的时候,很明显编译器会有提示; 而通过对象调用的时候则没有提示;
☂️static修饰成员方法
被static修饰的成员方法称为静态成员方法,是类方法,不是某个对象所特有的;
【静态方法特性】
1. 不属于某个具体的对象,是类方法
2. 可以通过对象调用,也可以通过类名.静态方法名(...)方式调用,更推荐使用后者
3. 不能在静态方法中访问任何非静态成员变量(静态方法中不能使用this)只要是非静态的成员都需要通过对象来访问,获取静态的成员变量或者设置静态的成员变量,此时的方法最好是静态的,否则你要是非静态的,你还得实例化对象,就好比脱裤子放屁。
public static String getClassRoom(){
System.out.println(this);
return classRoom;
} // 编译失败:Error:(35, 28) java: 无法从静态上下文中引用非静态变量 this
public static String getClassRoom(){
age += 1;
return classRoom;
} // 编译失败:Error:(35, 9) java: 无法从静态上下文中引用非静态变量 age
public static String getClassRoom(){
doClass();
return classRoom;
} // 编译报错:Error:(35, 9) java: 无法从静态上下文中引用非静态方法 doClass()