1、编写一个满足下列要求的类: 1)为学生创建一个类,这个类包括每个学生的姓名(String)、ID(int)和状态(int)。 状态表示学生的年级:1表示新生,2表示二年级,3表示三年级,4表示四年级。 2)可获得学生的姓名; 3)可修改学生年级; 实例化某个对象时,可根据给定的姓名、编号、年级生成。 2、定义一个抽象类Shape,它包含一个抽象方法getArea(),从Shape类派生出Rectangle和Circle类,这两个类都用getArea()方法计算对象的面积。编写应用程序使用Rectang
时间: 2024-02-15 17:02:23 浏览: 21
1. 学生类的代码:
```java
public class Student {
private String name;
private int id;
private int grade;
public Student(String name, int id, int grade) {
this.name = name;
this.id = id;
this.grade = grade;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
```
2. Shape抽象类和Rectangle、Circle类的代码:
```java
public abstract class Shape {
public abstract double getArea();
}
public class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public void setWidth(double width) {
this.width = width;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
}
public class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
```
应用程序使用Rectangle类计算面积的代码示例:
```java
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5, 10);
System.out.println("Rectangle area: " + rectangle.getArea());
}
}
```