java
package com.jmj.principles.dmeo2.after;
/**
* 四边形接口
*/
public interface Quadrilateral {
double getLength();
double getWidth();
}
java
package com.jmj.principles.dmeo2.after;
/**
* 长方形类
*/
public class Rectangle implements Quadrilateral{
private double length;
private double width;
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;
}
}
java
package com.jmj.principles.dmeo2.after;
public class RectangleDemo {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.setLength(20);
rectangle.setWidth(10);
resize(rectangle);
printLengthAndWidth(rectangle);
}
//扩宽方法
public static void resize(Rectangle rectangle){
//宽如果比长小,进行扩宽的操作
while (rectangle.getWidth()<=rectangle.getLength()){
rectangle.setWidth(rectangle.getWidth()+1);
}
}
public static void printLengthAndWidth(Rectangle rectangle){
System.out.println(rectangle.getLength());
System.out.println(rectangle.getWidth());
}
}
java
package com.jmj.principles.dmeo2.after;
import com.jmj.principles.dmeo2.before.Rectangle;
/**
* 正方形
*/
public class Square implements Quadrilateral {
private double side;
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
@Override
public double getLength() {
return side;
}
@Override
public double getWidth() {
return side;
}
}