私はまだJavaの初心者です。私の質問は本当に基本的なものかもしれません。
私はクラスのスーパークラスのボックスを持っています,
package chapter8;
public class Box {
double width;
private double height;
private double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
double volume() {
return width * height * depth;
}
}
BoxWeight は Box スーパークラスのサブクラスです。
package chapter8;
public class BoxWeight extends Box {
double weight;
BoxWeight(double w, double h, double d, double m){
super(w, h, d);
weight = m;
}
}
今、私はDemoBoxWeightにメインを持っています
package chapter8;
public class DemoBoxWeight {
public static void main(String[] args) {
BoxWeight myBox1 = new BoxWeight(2, 3, 4, 5);
System.out.println("Volume1 :" + myBox1.volume());
System.out.println("Weight1 :" + myBox1.weight);
System.out.println("Widht1: " + myBox1.width);
System.out.println("Depth1: " + myBox1.depth); // as depth is private, it is not accessible
}
}
高さと深さはプライベートとして定義されているため、これらの変数の値を実際に渡す DemoBoxWeight はアクセスできません。Private を default/public に変更できることはわかっていますが、値を渡すクラスが実際にアクセスできるようにする別の方法もありますか?
PS:私は新しいので、私の用語は間違っている可能性があり、私の質問は本当にばかげています