编写Java程序,定义一个类student,包括域“学号、姓名、年龄”;方法“获得学号、姓名、年龄、修改年龄”

2024-11-27 12:09:42
推荐回答(3个)
回答1:

class Student {
int age;
String name;
int number;
Student(int a,String s,int n)
{
age=a;
name=s;
number=n;
}

int getAge()
{
return age;
}

int getNumber()
{
return number;
}
String getName()
{
return name;
}
void 修改年龄(int age1)
{
age=age1;
}
int 获得年龄()
{
return age;
}
}

public class StudentText {
public static void main(String args[])
{
Student student = new Student(20,"xx",35);
System.out.println("姓名: "+student.getName());
System.out.println("年龄: "+student.getAge());
System.out.println("学号: "+student.getNumber());
student.修改年龄(25);
System.out.println("修改年龄: "+student.获得年龄());
}
}

/*
*答案: 姓名: xx
年龄: 20
学号: 35
修改年龄: 25

*/

回答2:

egduwu

回答3:

public class Student {
public int id;//学号
public String name;//姓名
public int age;//年龄

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}