一个不错的java面向对象之 继承 抽象 与 接口的题:

2023-12-15 21:30:09

?

?


public class Shape {
	private String color="red";
	private Boolean filled=true;
	
	public Shape() {		
	}
	
	public Shape(String color, Boolean filled) {
		
		this.color = color;
		this.filled = filled;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public Boolean isFilled() {
		return filled;
	}
	public void setFilled(Boolean filled) {
		this.filled = filled;
	}
	@Override
	public String toString() {
		return "Shape [color=" + color + ", filled=" + filled + "]";
	}
	
	
	
	
}

public class Rectangle extends Shape{
	private double width=1.0,length=1.0;
	
	public Rectangle() {}
	
	

	
	
	public Rectangle(double width, double length) {
		super();
		this.width = width;
		this.length = length;
	}





	public Rectangle(double width, double length,String color, Boolean filled) {
		super(color,filled);
		this.width = width;
		this.length = length;
	}

	public double getWidth() {
		return width;
	}

	public void setWidth(double width) {
		this.width = width;
	}

	public double getLength() {
		return length;
	}

	public void setLength(double length) {
		this.length = length;
	}
	
	public double getArea(){
		return width*length;
	}
	
	public double getPerimeter() {
		return 2*(width+length);
	}

	@Override
	public String toString() {
		return "Rectangle ["+super.toString() +",width=" + width + ", length=" + length + "]";
	}
	
}

public class Square extends Rectangle{
	public Square() {}
	
	public Square(double side){
		super(side,side);
	}
	
	
	public Square(double side,String color, Boolean filled){
		super(side,side,color,filled);
	}
	public double getSide() {
		return super.getWidth();
	}
	public void setSide(double side) {
		super.setWidth(side);
		super.setLength(side);
	}
	
	
	public void settWidth(double side) {
		super.setWidth(side);
		super.setLength(side);
	}
	
	
	public void setLength(double side) {
		super.setWidth(side);
		super.setLength(side);
	}
	
	public double getArea(){
		return getSide()*getSide();
	}
	
	public double getPerimeter() {
		return 4*getSide();
	}

	@Override
	public String toString() {
		return "Square ["+ super.toString() + "]";
	}
	

}

public class Circle extends Shape{
	private double radius = 1.0;

	
	public Circle() {
	}
	
	

	public Circle(double radius) {
		super();
		this.radius = radius;
	}



	public Circle(double radius,String color, Boolean filled) {		
		super(color,filled);
		this.radius = radius;		
	}

	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}
	
	
	public double getArea(){
		return Math.PI*radius*radius;
	}
	
	public double getPerimeter() {
		return 2*Math.PI*radius;
	}

	@Override
	public String toString() {
		return "Circle [" + super.toString() + ",radius=" + radius  + "]";
	}
	
	
}

import java.util.*;
//1:无需package
//2: 类名必须Main, 不可修改

public class Main {
public static void main(String[] args) {
	Square square=new Square(66);
	
	System.out.println(square);
	
 }
}

文章来源:https://blog.csdn.net/laocooon/article/details/135024943
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。