package spurs_ping;
import java.util.Scanner;
/*定义一个有关学生的Student类,内包含成员变量:String name,String sex,int age,所有的变量必须为私有的(private).
1.编写有参的构造函数:能对name,sex,age赋值.
2.覆盖toString函数:
按照格式:类名[name=,sex=,age=]输出.使用idea自动生成,然后修改成该输出格式
3.对每个属性生成setter/getter方法
4.main方法中
输入1行name age sex,调用上面有参构造函数新建对象
输入样例:
tom 15 male
输出样例:
Student [name=‘tom’, sex=‘male’,age=15]
*/
class Student{
private String name;
private String sex;
private int age;
public String toString() {
String str=“Student [name=’”+name+"’, sex=’"+sex+"’, age="+age+"]";
return str;
}
public Student(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String name=sc.next();
int age=Integer.valueOf(sc.next());
String sx=sc.next();
Student student=new Student(name,sx,age);
System.out.println(student);
}
}
输出:
tom 15 male
Student [name=‘tom’, sex=‘male’, age=15]