在java中创建一个叫做Student的类,对象属性:姓名、年龄、性别、身高,对象方法:说话、计算加法、年龄长一岁,类属性:学校名称;类方法:学习封装所有对象属性,编写属性的getter和setter方法
时间: 2024-05-05 13:17:46 浏览: 126
```
public class Student {
private String name;
private int age;
private String gender;
private double height;
private static String schoolName = "XXX学校";
public Student(String name, int age, String gender, double height) {
this.name = name;
this.age = age;
this.gender = gender;
this.height = height;
}
public void speak() {
System.out.println("大家好,我叫" + name + ",今年" + age + "岁,身高" + height + "米,是一个" + gender);
}
public int add(int a, int b) {
return a + b;
}
public void increaseAge() {
age++;
}
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public static String getSchoolName() {
return schoolName;
}
public static void setSchoolName(String schoolName) {
Student.schoolName = schoolName;
}
}
```
阅读全文