用java写一个person类对象是学生姓名年龄班级
时间: 2024-05-16 07:15:17 浏览: 102
可以这样写:
```java
public class Person {
private String name;
private int age;
private String className;
public Person(String name, int age, String className) {
this.name = name;
this.age = age;
this.className = className;
}
// Getter and setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
```
使用方法:
```java
Person student = new Person("张三", 18, "一班");
System.out.println("学生姓名:" + student.getName());
System.out.println("学生年龄:" + student.getAge());
System.out.println("学生班级:" + student.getClassName());
```
阅读全文