目录
1
this
this关键字:
- 指的是当前调用的对象(是一个引用,一个变量,变量中保存了内存地址指向了自身)
this使用在:
- 当局部变量和成员变量重名的时候可以使用this指向调用成员变量
- 通过this调用另一个构造方法
- 在构造方法中,如果使用this方法调用其他构造方法,那么this语句必须放在第一句
tip:
this关键字只能用在构造函数和成员方法内部(声明变量也可以使用)
另:
- 每个实例对象,都有一个this
- this出现在”实例方法“当中,this指向当前正在执行这个的对象
- this在大多数情况下都是可以省略的
- this不使用在带static的方法中
- this()这个方法只出现构造方法的第一行
实例1: 成员方法中
//1
public class Student {
String name;
public void doSome(){
System.out.println("123");//成员方法
System.out.println(this.name);//在s1中,this.name==s1.name
}
public void doSome1(){
this.doSome();//在s1中,this.doSome==s1.doSome
}
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "张三";
s1.doSome1();
}
}
//结果:123 张三
示例2:在构造方法中
//2
public class Data {
int year;
int month;
int day;
public Data() {
this(1970,1,1);//this()这个方法只出现构造方法的第一行
//表示调用这个类的另一个构造方法,作用:赋初值
}
public Data(int year,int month,int day) {
this.year =year;
this.month=month;
this.day=day;
}
}
//3
public class DataText {
public static void main(String[] args) {
Data data1=new Data(1970,1,1);
Data data2=new Data();
System.out.println(data1.year+"年"+data1.month+"月"+data1.day+"日");
System.out.println(data2.year+"年"+data2.month+"月"+data2.day+"日");
}
}
//结果:
//1970年1月1日
//1970年1月1日
2
static
辅助理解:
- 1班的人班级所属都是1班;中国人国籍都是中国。
static关键字:
- 修饰的方法为静态方法
- 修饰的变量为静态变量
- 所修饰元素访问方式:“类名. ”
- 所修饰元素特征为 类级别,与具体对象无关
另:
- 静态代码块:static{java语句}
- 静态代码块只在类加载时执行一次
- 方法定义为静态方法:在方法不一定需要对象来使用时定义
- 静态变量存在方法区内存当中
- 所修饰的变量称为静态变量,其在类加载的时候初始化,不需要创建变量,内存已经开辟了
作用:
如:如数学工具类方法
3
final
final关键字
- 表示最终的,不可变的
- final修饰的类无法被继承
- final修饰的方法无法被覆盖
- final修饰的变量,赋值后,不可重新赋值;一般和static联合使用,称为常量
- final修饰的实例对象,不会被垃圾回收器回收
注意:
- final修饰的变量需要手动赋值(执行构造方法时给实例变量赋值)
常量的语法格式:
- public static final 类型 常量名 = 值;
4
super
(1)super关键字
- 全部小写
(2)对比着this学习
(3)super
- 能出现在实例方法和构造方法当中
- 语法为:“super.” ”super()“
- 不能使用在静态(static)方法中
- super.大部分情况是可以省略的
- 不能省略" super. ":当父类和子类有相同的属性,或者有同样的方法时候,想在子类访问父类的属性或者方法。
- super() 只能出现在构造方法第一行,通过当前的构造方法去调用”父类“中的构造方法,目的为:创建子类对象的时候,可以先初始化父类型的特征。
(4)super()
- 表示通过子类的方法调用父类的构造方法
(5)重要结论:
- 当构造方法第一行没有this()和super(),默认一个super();
- 建议定义一个类的时候,把类的无参数构造方法定义出来,特别是父类的
(6)super的使用:
super.属性名 | 访问父类的属性 |
super.方法名(实参) | 访问父类的方法 |
super(实参) | 调用父类的构造方法 |
(7)示例 1
//辅助理解哦
//4
public class SuperText {
public static void main(String[] args) {
new B();
}
}
class A{
public A(){
this(4);
System.out.println("父类A的无参数构造方法");
}
public A(int i){
System.out.println("父类A的有参数构造方法");
}
}
class B extends A{
public B(){
this("Hello");
System.out.println("子类B的无参数构造方法");
}
public B(String youCan){
super();
System.out.println("子类B的无参数构造方法");
}
}
//结果:
//父类A的有参数构造方法
//父类A的无参数构造方法
//子类B的无参数构造方法
//子类B的无参数构造方法
示例2
//
//5
public class SuperText1 {
public static void main(String[] args) {
Vip v=new Vip("张三");
v.shopping();
}
}
class Customer{
String name;
public Customer(){}
public Customer(String name){
this.name=name;
}
}
class Vip extends Customer{
String name;
public Vip(){}
public Vip(String name){
super(name);
}
public void shopping(){
System.out.println(this.name+"正在购物");
System.out.println(super.name+"正在购物");
System.out.println(name+"正在购物");
}
}
//结果:
//null正在购物
//张三正在购物
//null正在购物