上机作业5.14
package llm;
public abstract class Vehicle {
private String brand;
private String color;
private double speed;
public Vehicle(String brand, String color) {
super();
this.brand = brand;
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand =brand;
}
public abstract void run();
}
package llm;
public class Car extends Vehicle {
private int loader;
public Car(String brand, String color, int loader) {
super(brand, color);
this.loader = loader;
}
public void run() {
System.out.println(getColor() + "色" + getBrand() + "可载" + this.loader + "时速为" + getSpeed() + "米/小时");
}
}
package llm;
public class VehicleText {
public static void main(String[] args) {
Vehicle car = new Car("Honda", "red", 2);
car.run();
car.setColor("black");
car.setBrand("benz");
car.run();
}
}

package llm;
public abstract class Shape {
protected double area;
protected double per;
protected String color;
public Shape() {
}
public Shape(String color) {
this.color = color;
}
public abstract void getArea();
public abstract void getPer();
public abstract void showAll();
}
package llm;
public class Rectangle extends Shape {
double width;
double height;
public Rectangle() {
}
public Rectangle(double width, double height, String color) {
super();
this.width = width;
this.height = height;
this.color = color;
}
public void getArea() {
area = width * height;
}
public void getPer() {
per = (width + height) * 2;
}
public void showAll() {
System.out.println("矩形面积为:" + area + ",周长为:" + per+",颜色:"+color);
}
}
package llm;
public class Circle extends Shape {
double radius;
public Circle() {
}
public Circle(double radius, String color) {
this.color = color;
this.radius = radius;
}
public void getArea() {
area = radius * radius * 3.14;
}
public void getPer() {
per = 2 * radius * 3.14;
}
public void showAll() {
System.out.println("圆的面积为:" + area + ",周长为:" + per + ",颜色:" + color);
}
}
package llm;
public class PolyDemo {
public static void main(String[] args) {
Circle circle = new Circle(4, "red");
Rectangle rectangle = new Rectangle(8, 9, "blue");
circle.getArea();
circle.getPer();
circle.showAll();
rectangle.getArea();
rectangle.getPer();
rectangle.showAll();
}
}

浙公网安备 33010602011771号