根据这篇文章,我知道紧耦合和松耦合之间的区别:https ://www.upgrad.com/blog/loose-coupling-vs-tight-coupling-in-java/
我不明白的是它使用的例子。
对于松耦合,Java 代码:
class Volume {
public static void main(String args[]) {
Cylinder b = new Cylinder(25, 25, 25);
System.out.println(b.getVolume());
}
}
final class Cylinder {
private int volume;
Cylinder(int length, int width, int height) {
this.volume = length * width * height;
}
public int getVolume() {
return volume;
}
}
对于紧密耦合,Java 代码:
class Volume {
public static void main(String args[]) {
Cylinder b = new Cylinder(15, 15, 15);
System.out.println(b.volume);
}}
class Cylinder {
public int volume;
Cylinder(int length, int width, int height) {
this.volume = length * width * height; }}
谁能解释第二个代码如何使两个类(体积和圆柱体)绑定在一起(紧密耦合)?或者是什么让第一个代码松耦合?谢谢。