构造方法的作用:
1.负责对象初始化工作,为对象的属性赋初值;
2.创建对象时,其类的构造方法确保在用户操作对象之前,系统保证初始化的进行。
构造方法的语法规则:
1.构造方法名与类名相同;
2.没有返回类型;
3.方法实现主要为字段赋初值。
构造方法的调用:
1.new操作符:创建新的对象时就会调用相应类的构造方法;
2.Java系统保证每个类都有构造方法。
Constuctor1:默认无参的构造方法
public class Constractor1{
public static void main(String args[]){
Person p=new Person();
p.setName("Steven");
System.out.println(p.getName());
p.setAge();
System.out.println(p.getAge());
p.setCity("Beijing");
System.out.println(p.getCity());
}
}
//When a class has no construct method,System will set a default construct method without parameter.
class Person{
private String name;
private int age;
private String city;
//default construct method has no parameters
//construct method with parameters.Force us to give enough parameters.
public Person(){
//String name,int age,String city
this.name=name;
this.age=age;
this.city=city;
};
//set,get name
public void setName(String cname){
name=cname;
}
public String getName(){
return name;
}
//set,get age
public void setAge(int cage){
age=cage;
}
public int getAge(){
return age;
}
//set,get city
public void setCity(String ccity){
city=ccity;
}
public String getCity(){
return city;
}
}
Constructor2:设置参数的构造方法
public class Constractor2{
public static void main(String args[]){
//Transfer parameters as well as new an object
Person p=new Person("Steven",,"Beijing");
//1.Crate space in heap and give the default value(null)
//2.Do assignment(if there is)
//3.Call the method and transfer the parameters
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getCity());
}
}
//When a class has no construct method,System will set a default construct method without parameter.
class Person{
private String name;
private int age;
private String city;
//default construct method has no parameters
//construct method with parameters.Force us to give enough parameters.
public Person(String name,int age,String city){
this.name=name;
this.age=age;
this.city=city;
};
//set,get name
public void setName(String cname){
name=cname;
}
public String getName(){
return name;
}
//set,get age
public void setAge(int cage){
age=cage;
}
public int getAge(){
return age;
}
//set,get city
public void setCity(String ccity){
city=ccity;
}
public String getCity(){
return city;
}
}
This 关键字:调用方法的当前类内的对象
1. 在类的方法中使用this()关键字,表示调用此方法的对象引用;
2. this可以看作是一个变量,它的值就是当前对象的引用;
3. 使用this可以处理方法中成员变量和形参同名的问题;
public class IotekTeacher{
private String name;
private int age;
public IotekTeacher(String name,int age){
this.name=name;
this.age=age;
}
}
4 当在方法内需要用到调用该方法的对象时,就可以用this;
5 在类的构造方法中可以调用this(【参数列表】)调用该类的指定构造方法。(一个类里面可以有多个构造方法)
方法中可以调用方法。上述两个方法属于同一个类时,被调用的方法前默认有一个this。可以写也可以不写。
pubic String toString(){
System.out.println(this.getAge());
return "名字:"+this.name+",今年:"+this.age;
}
构造方法可以调用构造方法,对this()的调用必须放在首条语句。
定义了带参数的构造方法后,无参的构造方法依然存在。
public Person(){
System.out.println("无参的构造方法");
}
public Person(String name,int age,String city){
this.name=name;
this.age=age;
this.city=city;
}
New 一个对象时,提示错误:non-static variable this cannnot be referenced from a static context.“非静态变量不可以从静态上下文中引用”
原因:要new的对象可能在静态上下文
解决方法:自习检查要new的对象的属性