package com.ht.TestThis;
public class TestThisKey {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s1 = new Student("ht",23,"男",107);
System.out.println(s1.name+"\t"+s1.age+"\t"+s1.sex+"\t"+s1.score);
}
}
class Student{
String name;
int age;
String sex;
double score;
public Student() {};
public Student(String n,int a,String s,double d) {
System.out.println("四参构造执行");
name = n;
age = a;
sex = s;
score = d;
}
}

package com.ht.TestThis;
public class TestThisKey {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s1 = new Student("ht",23,"男",107);
Student s2 = new Student("hts",223,"男s",1207);
System.out.println(s1.name+"\t"+s1.age+"\t"+s1.sex+"\t"+s1.score);
System.out.println(s2.name+"\t"+s2.age+"\t"+s2.sex+"\t"+s2.score);
}
}
class Student{
String name;
int age;
String sex;
double score;
public Student() {};
public Student(String name,int age,String sex,double score) {
System.out.println("四参构造执行");
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
}
}
this第二种用法:调用本类中的其他构造方法。如:this(),this(实参)
package com.ht.TestThis;
public class TestThisKey {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s1 = new Student("ht",23,"男",107);
Student s2 = new Student("hts",223,"男s",1207);
System.out.println(s1.name+"\t"+s1.age+"\t"+s1.sex+"\t"+s1.score);
System.out.println(s2.name+"\t"+s2.age+"\t"+s2.sex+"\t"+s2.score);
}
}
class Student{
String name;
int age;
String sex;
double score;
public Student() {};
public Student(String name,int age,String sex) {
this.name= name;
this.age= age;
this.sex= sex;
}
public Student(String name,int age,String sex,double score) {
this(name,age,sex)//this[实参]必须在构造方法的首行 只能在构造方法,不能在普通方法中
this.score = score;
}
}
右侧赞助一下 代码改变世界一块二块也是爱
浙公网安备 33010602011771号