public class Student {
private String sno
private String name
private char sex
private int age
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String sno, String name, char sex, int age) {
super()
this.sno = sno
this.name = name
this.sex = sex
this.age = age
}
public String getSno() {
return sno
}
public void setSno(String sno) {
this.sno = sno
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
public char getSex() {
return sex
}
public void setSex(char sex) {
this.sex = sex
}
public int getAge() {
return age
}
public void setAge(int age) {
this.age = age
}
}
package langs
public class StudentTest {
public static void main(String[] args) {
Student john = new Student("s001","john",'男',24)
Student mark = new Student("s002","mark",'男',25)
System.out.println("john学号: " + john.getSno())
System.out.println("mark学号: " + mark.getSno())
System.out.println("john姓名: " + john.getName())
System.out.println("mark姓名: " + mark.getName())
System.out.println("john年龄: " + john.getAge())
System.out.println("mark年龄: " + mark.getAge())
john.setAge(26)
System.out.println("john年龄: " + john.getAge())
}
}
class Student {private String name
private String studentId
private int score
public Student(String name, String studentId, int score) {
this.name = name
this.studentId = studentId
this.score = score
}
public void printInfo() {
System.out.println("姓名: " + name + ", 学号: " + studentId)
}
}
public class Main {
public static void main(String[] args) {
Student student1 = new Student("Tom", "S001", 85)
Student student2 = new Student("Jerry", "S002", 90)
Student student3 = new Student("Mike", "S003", 80)
student1.printInfo()
student2.printInfo()
student3.printInfo()
}
}
上面这段代码编写了一个学生类,学生信息包括姓名、学号、成绩。通过调用 printInfo() 方法可以输出每个学生的姓名、学号。
在main()方法中,我们创建了三个学生对象,并调用了每个学生的printInfo()方法,实现了输出每个学生的姓名和学号的功能。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)