第十三周作业
1.
package School.Day12;
public class Vehicle {
String brand;
String color;
double speed;
public Vehicle(String brand, String color) {
this.brand = brand;
this.color = color;
}
public Vehicle() {
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
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 void run(){
System.out.println(brand + "的" + color + "汽车正在行驶。速度: " + speed );
}
}
package School.Day12;
public class Car extends Vehicle{
private int loader;
public Car(String brand, String color, int loader) {
this.brand = brand;
this.color = color;
this.loader = loader;
}
public void run(){
System.out.println(brand + "的" + color + "汽车正在行驶。速度: " + speed );
System.out.println("可载人数: " + loader);
}
}
package School.Day12;
public class CarTest {
public static void main(String[] args) {
Car c = new Car("Honda","red",2);
c.run();
}
}

2.
package School.Day12;
public abstract class Shape {
int area;
int per;
String color;
public Shape(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Shape() {
}
public abstract int getArea();
public abstract int getPer();
public abstract void showAll();
}
package School.Day12;
public class Rectangle extends Shape{
int width;
int height;
public Rectangle() {
}
public Rectangle(int width, int height,String color) {
this.width = width;
this.height = height;
this.color = color;
}
public int getArea() {
return this.height * this.width;
}
public int getPer() {
return 2 * (this.height + this.width);
}
public void showAll() {
System.out.println("矩形颜色: " + getColor());
System.out.println("矩形面积: " + getArea());
System.out.println("矩形周长: " + getPer());
}
}
package School.Day12;
public class Circle extends Shape{
int radius;
public Circle(int radius,String color) {
this.radius = radius;
this.color = color;
}
public Circle(){
}
public int getArea() {
return 3 * radius * radius;
}
public int getPer() {
return 2 * 3 * radius;
}
public void showAll() {
System.out.println("⚪的颜色: " + this.getColor());
System.out.println("⚪的面积: " + this.getArea());
System.out.println("⚪的周长: " + this.getPer());
}
}
package School.Day12;
public class ShapeTest {
public static void main(String[] args) {
Rectangle r = new Rectangle(10,20,"黑");
r.showAll();
Circle c = new Circle(10,"白");
c.showAll();
}
}

浙公网安备 33010602011771号