编写一个接口shape,拥有方法area();编写一个接口Art拥有抽象方法draw()编写一个person类包含若干属性和方法定义一个person类的子类student,并且实现上述两种接口;创建student的实力对象,分别调用继承的方法和实现的方法
时间: 2024-01-22 20:21:21 浏览: 61
以下是Java语言的代码实现:
```java
// 定义Shape接口
interface Shape {
double area(); // 计算面积的抽象方法
}
// 定义Art接口
interface Art {
void draw(); // 绘画的抽象方法
}
// 定义Person类
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void introduce() {
System.out.println("My name is " + name + ", and I am " + age + " years old.");
}
}
// 定义Student类,继承Person类并实现Shape和Art接口
class Student extends Person implements Shape, Art {
private double height;
private double weight;
public Student(String name, int age, double height, double weight) {
super(name, age);
this.height = height;
this.weight = weight;
}
// 实现Shape接口的计算面积方法
public double area() {
return height * weight;
}
// 实现Art接口的绘画方法
public void draw() {
System.out.println("I am drawing a picture.");
}
}
// 测试
public class Main {
public static void main(String[] args) {
Student s = new Student("Tom", 18, 1.8, 70.0);
s.introduce(); // 调用继承自Person类的方法
System.out.println("The area of my body is " + s.area()); // 调用实现的Shape接口方法
s.draw(); // 调用实现的Art接口方法
}
}
```
输出结果:
```
My name is Tom, and I am 18 years old.
The area of my body is 126.0
I am drawing a picture.
```
阅读全文