构造器其实是一个方法,一个创建对象的方法。
//构造器: 构造器的语法 : 修饰符 类名 (数据类型 参数名...){}
构造方法的目的: 创建对象
//构造器public class Test_50 {public static void main(String[] args) { Person132 p =new Person132(); Person132 p1 =new Person132("中国"); Person132 p2 =new Person132("中国",100); System.out.println(p.name); System.out.println(p1.name); System.out.println(p2.name); }}class Person132{String name ;int age;public Person132() {}public Person132(String name) {this.name =name;}public Person132(String name, int age) {this(name);this.age=age;}public void introduce() {System.out.println(age+"\t"+name);}}//每一个类都有一个默认的构造器,也就是构造方法,它是一个无参构造器,每个类至少都会有一个。//假如自己创建了一个构造器,那么默认构造器将被覆盖,必须自己重新创建一个无参构造器。//this可以调用本类的构造方法。//构造方法的细节:一个类可以有多个构造器,但是它们的存在是以重载的形式体现的。//构造方法可以用pirvate 修饰符,但是无法同构次构造方法来创建对象。//构造方法和对象方法不同: /** * 1.执行时间问题:构造方法是在创建的时候,就已经调用完成了,当对象创建完成则执行完毕,而对象方法则不同,它是在创建完成之后,手动调用,什么时候调用什么时候就会被执行。 * 2.调用次数问题:构造方法时在创建的时候会自动的进行调用一次,当对象执行完毕的时候,我们不能够手动人为的再去调用构造方法,而对象方法则可以随时想调用就调用没有次数限制。 * 3.互相调用问题:构造方法可以调用对象方法,而对象方法却无法调用构造方法. * **/
构造方法不能调用其他构造方法,但是可以使用this关键字来进行调用。
1 this调用构造方法的语句必须放在构造方法的第一行,且只能出现一次。
2 构造方法中可以使用this来调用对象方法,但是对象方法this却叼用不了构造方法
3 不能再同一个类中用两个this互相调用彼此
练习:

class Student1 {
String name;
String birthday;
int age;
double height;
double weight;
char sex;
double java;
double math;
double sql;
public Student1(String name, int age, char sex, String birthday) {
this.name=name;
this.age=age;
this.sex=sex;
this.birthday=birthday;
}
public Student1(double java, double math, double sql) {
this.java=java;
this.math=math;
this.sql=sql;
}
public Student1() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public double getJava() {
return java;
}
public void setJava(double java) {
this.java = java;
}
public double getMath() {
return math;
}
public void setMath(double math) {
this.math = math;
}
public String getName() {
return name;
}
public String getBirthday() {
return birthday;
}
public double getHeight() {
return height;
}
public double getWeight() {
return weight;
}
public double getSql() {
return sql;
}
public void setName(String name) {
if (name.length() < 2) {
name = "无效名";
} else {
this.name = name;
}
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public void setAgee(int age) {
if (age < 0 || age > 120) {
this.age = 0;
} else {
this.age = age;
}
}
public void setHeight(double height) {
this.height = height;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void sexSex(char sex) {
if (sex != '男' || sex != '女') {
this.sex = ' ';
} else {
this.sex = sex;
}
}
public void sexJava(double java) {
if (java < 0 || java > 120) {
this.java = 0;
} else {
this.java = java;
}
}
public void sexMath(double math) {
if (math < 0 || java > 120) {
this.math = 0;
} else {
this.math = math;
}
}
public void setSql(double sql) {
if (sql < 0 || sql > 120) {
this.sql = 0;
} else {
this.sql = sql;
}
}
public void run() {
height += -0.01;
weight += -0.5;
System.out.println("身高:" + height + "\t体重:" + weight);
}
public void eat() {
height += 0.01;
weight += 0.5;
}
public void study() {
java += 1;
math += 1;
sql += 1;
System.out.println(java + "\t" + "math+\t" + sql);
}
public void sleep() {
System.out.println("我的名字叫:" + name + "\t我的生日是:" + birthday + "\t我的年龄是:" + age + "\t我的身高是:" + height + "\t我的体重是:"
+ weight + "\t我的性别是:" + sex + "\t我的java成绩是" + java + "\t我的math成绩是:" + "math" + "\t我的sql成绩是:" + sql);
System.out.println("总分为:" + (java + sql + math));
System.out.println("平均分为:" + (java + sql + math) / 3);
}
}
public class Test_51 {
public static void main(String[] args) {
Student1 s1 =new Student1();
s1.sleep();
Student1 s2 =new Student1(111,122,44);
s2.sleep();
Student1 s3 =new Student1("张学友",20,'男',"2018-2-2");
s3.sleep();
}
}