public class Student {
private String name;
private String age;
//空参构造
public Student() {
}
//有参构造
public Student(String name, String age) {
this.name = name;
this.age = age;
}
//输出
public void show(){
System.out.println("My name is "+name+",age is "+age);
}
//setter和getter方法
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public String getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(String age) {
this.age = age;
}
}
public class Test4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串");
String s1 = sc.nextLine();
//切割数据
String[] s = s1.split(",");
//遍历输出
for (int i = 0; i < s.length; i++) {
System.out.println(s[i]);
}
//构造方法赋值
Student student = new Student(s[0], s[1]);
student.show();
}
}