abstract class Shape { abstract void display();} class Circle extends Shape { void display(){ System.out.println("Circle"); }} class Rectangle extends Shape { void display() { System.out.println("Rectangle");}} class Triangle extends Shape { void display() { System.out.println("Triangle");} } public class AbstractClassDemo{ public static void main(String args[]){ (new Circle()).display(); (new Rectangle()).display(); (new Triangle()).display(); }}} }
时间: 2024-03-07 18:54:10 浏览: 119
这段代码是一个Java程序,其中定义了一个抽象类Shape和三个具体类Circle、Rectangle和Triangle,这三个类都继承自Shape类并实现了它的抽象方法display()。在main()方法中,分别创建了一个Circle、一个Rectangle和一个Triangle对象,并调用它们的display()方法,输出了它们的形状名称。这段代码展示了Java语言中抽象类和多态的使用方法。
相关问题
定义一个抽象类为形状类Shape,其中包含抽象方法display(),在其子类圆形Circle和长方形Rectangle中实现该抽象方法,分别输出“这是一个圆形”和“这是一个长方形”。编写MainClass,在其main()函数中进行测试。
在Java中,你可以按照以下步骤创建这个抽象类和它的两个子类:
首先,我们定义一个名为`Shape`的抽象类,它有一个抽象方法`display()`:
```java
abstract class Shape {
abstract void display();
}
```
然后,我们创建两个实现了`Shape`抽象类的具体子类:`Circle`和`Rectangle`,它们分别覆盖了`display()`方法:
```java
class Circle extends Shape {
@Override
public void display() {
System.out.println("这是一个圆形");
}
}
class Rectangle extends Shape {
@Override
public void display() {
System.out.println("这是一个长方形");
}
}
```
接下来,在`MainClass`中,我们可以创建这两个子类的对象并调用它们的`display()`方法进行测试:
```java
public class MainClass {
public static void main(String[] args) {
Shape circle = new Circle(); // 创建一个Circle对象
circle.display(); // 输出 "这是一个圆形"
Shape rectangle = new Rectangle(); // 创建一个Rectangle对象
rectangle.display(); // 输出 "这是一个长方形"
}
}
```
在这个例子中,尽管`circle`和`rectangle`都是`Shape`类型的引用,但由于多态性,它们分别调用了各自子类的`display()`方法,展示了多态带来的灵活性。
用java创建shape父类由特定形状,如矩形、三角形和圆形继承父类名字。测试每个类 用自己的驱动程序才能使用。创建一个txt文本储存10组这三种类型数据(包含父类的名字和它本身的变量且文本文件中每行一个数据)并创建一个测试类(创建start()方法创建列表通过引用传递的方法来create()方法和display()方法create()方法为测试方法display为打印结果来测试该文本的数据,还要创建驱动类来驱动
以下是实现题目要求的Java代码:
Shape.java
```java
public abstract class Shape {
protected String name;
public Shape(String name) {
this.name = name;
}
public abstract double getArea();
public String getName() {
return name;
}
}
```
Rectangle.java
```java
public class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(String name, double width, double height) {
super(name);
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
}
```
Triangle.java
```java
public class Triangle extends Shape {
private double base;
private double height;
public Triangle(String name, double base, double height) {
super(name);
this.base = base;
this.height = height;
}
@Override
public double getArea() {
return 0.5 * base * height;
}
public double getBase() {
return base;
}
public double getHeight() {
return height;
}
}
```
Circle.java
```java
public class Circle extends Shape {
private double radius;
public Circle(String name, double radius) {
super(name);
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
public double getRadius() {
return radius;
}
}
```
ShapeList.java
```java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ShapeList {
private List<Shape> shapes;
public ShapeList() {
shapes = new ArrayList<>();
}
public void create(String filename) {
try {
File file = new File(filename);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
String name = parts[0].trim();
switch (name) {
case "Rectangle":
double width = Double.parseDouble(parts[1].trim());
double height = Double.parseDouble(parts[2].trim());
shapes.add(new Rectangle(name, width, height));
break;
case "Triangle":
double base = Double.parseDouble(parts[1].trim());
double triHeight = Double.parseDouble(parts[2].trim());
shapes.add(new Triangle(name, base, triHeight));
break;
case "Circle":
double radius = Double.parseDouble(parts[1].trim());
shapes.add(new Circle(name, radius));
break;
default:
System.out.println("Unknown shape type: " + name);
break;
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + filename);
}
}
public void display() {
for (Shape shape : shapes) {
System.out.println("Shape: " + shape.getName());
System.out.println("Area: " + shape.getArea());
if (shape instanceof Rectangle) {
Rectangle rect = (Rectangle) shape;
System.out.println("Width: " + rect.getWidth());
System.out.println("Height: " + rect.getHeight());
} else if (shape instanceof Triangle) {
Triangle tri = (Triangle) shape;
System.out.println("Base: " + tri.getBase());
System.out.println("Height: " + tri.getHeight());
} else if (shape instanceof Circle) {
Circle circle = (Circle) shape;
System.out.println("Radius: " + circle.getRadius());
}
System.out.println();
}
}
}
```
ShapeTest.java
```java
public class ShapeTest {
public void start() {
ShapeList list = new ShapeList();
list.create("shapes.txt");
list.display();
}
public static void main(String[] args) {
ShapeTest test = new ShapeTest();
test.start();
}
}
```
Driver.java
```java
public class Driver {
public static void main(String[] args) {
ShapeTest test = new ShapeTest();
test.start();
}
}
```
shapes.txt
```
Rectangle,10.5,8.2
Triangle,5,12
Circle,7.3
Rectangle,12,4
Circle,6.5
Triangle,9,6
Rectangle,5,5
Circle,3.4
Triangle,8,4
Rectangle,7,3
```
运行Driver.java或ShapeTest.java,即可读取shapes.txt文件中的形状数据并打印出每个形状的信息。
阅读全文