A .this keyword
1. Call the other constructor in the constructor
Direct use this (parameter) constructor is invoked
class Person{
public String name;
public Person(String n){
name=n;
}
public Person(){
this("陌生人");
}
}
2. Access via this property or method
访问普通方法时this.方法名称(参数)
访问属性时this.属性名
//访问普通方法
class Person{
public String name;
public Person(String n){
name=n;
this.toString();
}
public String toString(){
}
}
//访问属性
class Person{
public String name;
public Person(String name){
this.name=n;
}
}
public class jk{
public static void main(String[]args){
Person p=new Person("张三");//需要在创建对象时,需要调用构造方法对类中的属性进行赋值
}
}
3.this represents the current object reference
class Date{
public Date after(){
return this;
}
}
public class Main{
public static void main(String[]args){
Date p=new Date();
Date q=new Date();//创建了两个person类的对象,其引用指向了内存空间
Date p1=p.after();//调用对象p的方法返回的是品对象的一个引用
Date q1=q.after();
}
}
For example, I wrote a class Date
class Date{
public Date after(){
return this;
}
public Date befor(){
return this;
}
}
public class Main{
public static void main(String[]args){
Date p=new Date(2019-7-21);//创建了两个person类的对象,其引用指向了内存空间
Date p1=p.after(80);//调用对象p的方法返回的是品对象的一个引用
Date q1=p1.befor(80);
}
}//最终打印仍然为2019-7-21
II. Static classes and attributes
1. Classification of methods and properties
- Methods: The common method / static methods
2. Properties: General Property / static properties
static meaning unbundling is object, i.e. no longer stored in object (heap), but stored in the class (Method region)
Precautions for static method call:
1.不能通过this访问
2.不能调用普通方法
3.不能访问普通属性
static 修饰的属性为静态属性存放在方法区,可以被任何方法访问
static class methods can not access non-static class properties / methods
The method allows access to non-static class static class methods / properties
2. The method of accessing static properties / methods
内部:1.属性名称/方法名称(实参)
2.类名称.属性名称/类名称.方法名称(参数)
3.this.属性名称/方法名称(参数)
外部:1.类名称.属性名称/方法名称(参数)
2.引用.属性名称/方法名称(参数)