this关键字
1.使用this调用方法
this调用普通方法时使用“this.方法()”调用。this还可以实现构造方法的互相调用
使用this调用构造方法的时候也有一些限制
- 使用this调用构造方法,必须放在构造方法的首行
- 在使用this()进行构造方法互调用时,至少保证留下一个出口,即:没有使用this调用其他构造
class Person {
private String name;
private int age;
public Person(){
System.out.println("1.这是一个无参构造!");
}
public Person(String name){
this();
System.out.println("2.这是一个单参构造!");
}
public Person(String name , int age){
this(name);
System.out.println("3.这是一个双参构造!")
}
}
public class TextDemo6{
public static void main(String ages[]){
Person per = new Person("张三",20);
}
}
2.使用this访问:使用this访问属性时使用“this.属性”进行访问
static关键字
1.使用static定义属性
使用static关键字声明的属性就是公共属性,此属性会保存在称为“全局数据区”的内存区域,且一般使用static定义的属性往往会通过类名称直接调用 即:“ 类名称.属性="**" ”
因此static属性又被称为“类属性” 而且static属性可以在一个类没有进行实例化时直接访问
注:一般情况下不会优先考虑static属性
2.static定义方法
static方法和普通方法存在一些限制
- 使用static方法只能够调用static属性和static方法,其他不可调用
- 使用非static方法可以调用任意static属性和方法
主类中的普通方法的调用只能通过实例化对象调用
即:new 类名称().方法名()
public class TextDemo6{
public static void main(String ages[]){
new TextDemo6().fun();
}
public void fun(){
System.out.println("Hello World!");
}
}
final关键字
在Java之中final被称为终结器,指的是完结的含义,在程序中可以定义类、方法、常量
1.使用final定义的类不能有子类,表示太监类
2.使用final定义的方法不能被子类所覆写
3.使用final定义的变量称为常量,常量必须使用final定义,而且声明常量的时候要设置好内容,不能修改
如果使用 public static final 定义常量,表示的是全局常量
注:常量的变量名称都要大写



