Inheritance 引用变量母类(继承)
大家好,希望你们能帮助我。对不起,我的英语不是我的母语 我是新来的。这就是我想做的:我想创建一个名为“Monitor”的类,它具有以下属性: -调用Monitor实例时,构造函数会在x、y位置(由用户定义)、宽度和高度(由构造函数随机定义)上创建一个矩形。高度取决于带表达式的宽度 然后我想创建一个名为“OldMonitor”的派生类,除了高度之外,其他属性都是相同的。我希望它是监视器母类宽度的2/3,每次调用一个距离 这是我的代码:Inheritance 引用变量母类(继承),inheritance,constructor,processing,Inheritance,Constructor,Processing,大家好,希望你们能帮助我。对不起,我的英语不是我的母语 我是新来的。这就是我想做的:我想创建一个名为“Monitor”的类,它具有以下属性: -调用Monitor实例时,构造函数会在x、y位置(由用户定义)、宽度和高度(由构造函数随机定义)上创建一个矩形。高度取决于带表达式的宽度 然后我想创建一个名为“OldMonitor”的派生类,除了高度之外,其他属性都是相同的。我希望它是监视器母类宽度的2/3,每次调用一个距离 这是我的代码: class Monitor { float x; // p
class Monitor {
float x; // pos x
float y; // pos y
float w, h; // width and height
Monitor(float _x, float _y) {
x=_x;
y=_y;
w=random(50,100);
h=(w*9)/16;
}
void display() {
rect(x,y,w,h);
}
但这并没有像预期的那样起作用,因为(我认为)当我调用super(x,y)时,OldMonitor上的w变量会得到一个新的随机值,然后在h上设置值的表达式取决于这个新值。我希望它取决于母类上生成的值,我希望这是清楚的理解 我可以通过仍然使用super()构造函数来实现它吗
谢谢,我希望文本代码写得很好,我不知道如何在这里设置样式 代码按预期工作 我相信大家可能会对
super()
的工作原理感到困惑。
超级构造函数中的指令几乎在子类中“复制”并执行:子类实例和任何其他实例(即使是超级类)之间没有连接
将超级构造函数转换为以下内容:
Monitor(float _x, float _y) {
x=_x;
y=_y;
w=random(50, 100);
h=(w*9)/16;
println(this,w,h);
}
请注意,它打印出如下内容:
$Monitor@1dd41b82 85.673935 48.19159
$OldMonitor@40ceef5d 56.606354 31.841074
它不会打印Monitor@
两次
第二个问题与逻辑有关:即使将超类中的指令复制/粘贴到子类中,宽度也会初始化为一个随机值:每次调用它都会有所不同
就行为而言,听起来像是希望sublcass实例复制超类实例的宽度,并使用其自身的纵横比更新高度。
有一种方法可以实现这一点:
void updateFromWidth(Monitor otherMonitor){
w = otherMonitor.w;
h=(w*2)/3;
}
完整示例:
Monitor m1;
OldMonitor m2;
void setup(){
size(300,300);
m1 = new Monitor(10,10);
m2 = new OldMonitor(120,10);
// set w from monitor, maintaining aspect ratio
m2.updateFromWidth(m1);
}
void draw(){
background(0);
m1.display();
m2.display();
}
class Monitor {
float x; // pos x
float y; // pos y
float w, h; // width and height
Monitor(float _x, float _y) {
x=_x;
y=_y;
w=random(50, 100);
h=(w*9)/16;
println(this,w,h);
}
void display() {
rect(x, y, w, h);
}
}
class OldMonitor extends Monitor {
OldMonitor(float _x, float _y) {
super(_x, _y);
h=(w*2)/3;
}
// copy w from another monitor, but maintain aspect ratio
void updateFromWidth(Monitor otherMonitor){
w = otherMonitor.w;
h=(w*2)/3;
}
}
代码按预期工作 我相信大家可能会对
super()
的工作原理感到困惑。
超级构造函数中的指令几乎在子类中“复制”并执行:子类实例和任何其他实例(即使是超级类)之间没有连接
将超级构造函数转换为以下内容:
Monitor(float _x, float _y) {
x=_x;
y=_y;
w=random(50, 100);
h=(w*9)/16;
println(this,w,h);
}
请注意,它打印出如下内容:
$Monitor@1dd41b82 85.673935 48.19159
$OldMonitor@40ceef5d 56.606354 31.841074
它不会打印Monitor@
两次
第二个问题与逻辑有关:即使将超类中的指令复制/粘贴到子类中,宽度也会初始化为一个随机值:每次调用它都会有所不同
就行为而言,听起来像是希望sublcass实例复制超类实例的宽度,并使用其自身的纵横比更新高度。
有一种方法可以实现这一点:
void updateFromWidth(Monitor otherMonitor){
w = otherMonitor.w;
h=(w*2)/3;
}
完整示例:
Monitor m1;
OldMonitor m2;
void setup(){
size(300,300);
m1 = new Monitor(10,10);
m2 = new OldMonitor(120,10);
// set w from monitor, maintaining aspect ratio
m2.updateFromWidth(m1);
}
void draw(){
background(0);
m1.display();
m2.display();
}
class Monitor {
float x; // pos x
float y; // pos y
float w, h; // width and height
Monitor(float _x, float _y) {
x=_x;
y=_y;
w=random(50, 100);
h=(w*9)/16;
println(this,w,h);
}
void display() {
rect(x, y, w, h);
}
}
class OldMonitor extends Monitor {
OldMonitor(float _x, float _y) {
super(_x, _y);
h=(w*2)/3;
}
// copy w from another monitor, but maintain aspect ratio
void updateFromWidth(Monitor otherMonitor){
w = otherMonitor.w;
h=(w*2)/3;
}
}