java定义一个抽象类名为person的抽象类,类里属性有:性别,身高,体重,且私有,能够通过get方法获取属性值,能通过set方法设置属性值。有一个虚方法eat(),且要求在子类里实现eat()方法
时间: 2024-03-25 17:41:17 浏览: 62
好的,以下是定义一个抽象类名为Person的抽象类,包含性别、身高和体重三个私有属性,并提供了公有的get和set方法。此外,还有一个虚方法eat,要求在子类中实现。
```java
public abstract class Person {
private String gender;
private double height;
private double weight;
public Person(String gender, double height, double weight) {
this.gender = gender;
this.height = height;
this.weight = weight;
}
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 double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public abstract void eat();
}
```
注意,这里使用了Java语言的抽象类和抽象方法的语法。由于eat方法是虚方法,因此在父类中只定义了方法名和返回类型,而没有实现方法体。子类需要继承这个抽象类,并重写eat方法。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)