声明:下面的所有代码都省略了setter getter构造 在本类中访问封装的属性都必须使用setter getter为了减少阅读量此处省
类中的方法一共有两种:
普通方法:this.方法名称(参数...)
构造方法:this(参数...)
范例:调用本类方法
class Person{
private String name;
private int age;
public Person() {
}
public Person(String name,int a) {
this.name=name;
this.age=age;
this.print();//调用本类方法
}
public void print() {
System.out.println("*********************");
}
public String getInfo() {
return "姓名: "+this.name+" 年龄:"+this.age;
}
}
public class StringDemo {
public static void main(String args[]) {
System.out.println(new Person("张三",20).getInfo());
}
}
结果//*********************
姓名: 张三 年龄:0
虽然调用本类的普通方法前可以不使用this关键词。但是建议追加是this,这样的目的是可以区分方法的定义来源。
范例:调用构造
构造方法与普通方法最大的区别:使用关键字new实例化类新对象的时候使用一次,而普通方法是在对象实例化完成后(构造已经执行过了)可以调用多次
那么在java里面支持类构造方法的互相调用
观察如下代码
class Person{
private String name;
private int age;
public Person() {
System.out.println("**************");
}
public Person(String name) {
System.out.println("**************");
this.name=name;
}
public Person(String name,int age) {
System.out.println("**************");
this.name=name;
this.age=age;
}
public String getInfo() {
return "姓名: "+this.name+" 年龄:"+this.age;
}
}
public class StringDemo {
public static void main(String args[]) {
System.out.println(new Person().getInfo());
System.out.println(new Person("张三").getInfo());
System.out.println(new Person("张三",20).getInfo());
}
}
//结果
**************姓名: null 年龄:0
**************
姓名: 张三 年龄:0
**************
姓名: 张三 年龄:20
现在给出的程序中出现了重复的代码,所以这个样的操作很明显不应该出现,必须消除重复数据
范例:使用this解决问题
class Person{
private String name;
private int age;
public Person() {
System.out.println("**************");
}
public Person(String name) {
this();//调用本类中的无参构造
this.name=name;
}
public Person(String name,int age) {
this(name);//调用本类中的有参构造
this.age=age;
}
public String getInfo() {
return "姓名: "+this.name+" 年龄:"+this.age;
}
}
public class StringDemo {
public static void main(String args[]) {
System.out.println(new Person().getInfo());
System.out.println(new Person("张三").getInfo());
System.out.println(new Person("张三",20).getInfo());
}
}
//结果
**************
姓名: null 年龄:0
**************
姓名: 张三 年龄:0
**************
姓名: 张三 年龄:20
虽然使用this可以实现构造方法的互相调用,但是此时有如下的两点要求
一、this()调用构造方法的语句必须放在构造方法的首行。
二、使用this调用构造方法的时候请留有出口;
举例:
也不可以在普通方法中,因为是构造方法执行完后才进行this的调用所以当它存在与普通方法中时就会出现错误
错误举例, 不遵循递归原理
个人认为就是没有遵循递归构造函数
那怎么让这个不报错呢, 很简单就是在一个三参的构造方法中使用
代码如下
class Person{
private String name;
private int age;
private int hight;
public Person() {
System.out.println("**************");
}
public Person(String name) {
this();
this.name=name;
}
public Person(String name,int age) {
this(name);
this.age=age;
}
public Person(String name,int age,int hight) {
this("hello",20);
this.hight=hight;
}
public String getInfo() {
return "姓名: "+this.name+" 年龄:"+this.age+"身高:"+hight;
}
}
public class StringDemo {
public static void main(String args[]) {
System.out.println(new Person().getInfo());
System.out.println(new Person("张三").getInfo());
System.out.println(new Person("张三",20).getInfo());
System.out.println(new Person("张三",20,100).getInfo());
}
}//结果
**************
姓名: null 年龄:0身高:0
**************
姓名: 张三 年龄:0身高:0
**************
姓名: 张三 年龄:20身高:0
**************
姓名: hello 年龄:20身高:100