设计一个完整的JAVA程序实现类、对象、继承、抽象类、接口应用
时间: 2023-10-20 20:26:36 浏览: 53
好的,我来为你设计一个完整的JAVA程序实现类、对象、继承、抽象类、接口应用。
首先,我们来设计一个类Person,作为我们程序的基础类,包含姓名、年龄、性别等属性和基本的getter和setter方法。
```
public class Person {
private String name;
private int age;
private String gender;
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
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;
}
}
```
接着,我们来设计一个继承自Person的类Student,表示学生,包含学号、年级、专业等属性和基本的getter和setter方法。
```
public class Student extends Person {
private String studentId;
private String grade;
private String major;
public Student(String name, int age, String gender, String studentId, String grade, String major) {
super(name, age, gender);
this.studentId = studentId;
this.grade = grade;
this.major = major;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
```
接着,我们来设计一个抽象类Animal,表示动物,包含种类、食物等属性和基本的getter和setter方法,同时定义一个抽象方法move()表示动物的移动方式。
```
public abstract class Animal {
private String species;
private String food;
public Animal(String species, String food) {
this.species = species;
this.food = food;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public String getFood() {
return food;
}
public void setFood(String food) {
this.food = food;
}
public abstract void move();
}
```
接着,我们来设计一个实现了Animal接口的类Bird,表示鸟类,重写move()方法实现鸟类的飞行方式。
```
public class Bird implements Animal {
private String species;
private String food;
public Bird(String species, String food) {
this.species = species;
this.food = food;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public String getFood() {
return food;
}
public void setFood(String food) {
this.food = food;
}
@Override
public void move() {
System.out.println("Bird is flying.");
}
}
```
最后,我们来设计一个测试类Main,包含main()方法,用于测试我们设计的类、对象、继承、抽象类、接口等应用。
```
public class Main {
public static void main(String[] args) {
// 创建一个Person对象
Person person = new Person("张三", 20, "男");
System.out.println("姓名:" + person.getName());
System.out.println("年龄:" + person.getAge());
System.out.println("性别:" + person.getGender());
// 创建一个Student对象
Student student = new Student("李四", 18, "男", "20210001", "大一", "计算机科学与技术");
System.out.println("姓名:" + student.getName());
System.out.println("年龄:" + student.getAge());
System.out.println("性别:" + student.getGender());
System.out.println("学号:" + student.getStudentId());
System.out.println("年级:" + student.getGrade());
System.out.println("专业:" + student.getMajor());
// 创建一个Bird对象
Bird bird = new Bird("鹦鹉", "果实");
System.out.println("种类:" + bird.getSpecies());
System.out.println("食物:" + bird.getFood());
bird.move();
}
}
```
这样我们就完成了一个完整的JAVA程序实现类、对象、继承、抽象类、接口应用。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](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)
![](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)
![](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)