我不确定您想要实现什么。考虑到this other question,我试图弄清楚这一点,但它可能有助于详细了解您的父母、孩子和父母 p = new Child() 应该做什么功能/行为。
我汇总了几种处理继承的常用方法的简要概述。但您可能正在寻找更具体的案例。
package Inheritance
import SI = Modelica.SIunits;
connector PositivePin "Positive pin of an electrical component"
SI.ElectricPotential v "Potential at the pin";
flow SI.Current i "Current flowing into the pin";
end PositivePin;
partial model OnePort
"Component with two electrical pins p and n and current i from p to n"
SI.Voltage v "Voltage drop of the two pins (= p.v - n.v)";
SI.Current i "Current flowing from pin p to pin n";
Inheritance.PositivePin p "Positive electrical pin";
Modelica.Electrical.Analog.Interfaces.NegativePin n "Negative electrical pin";
equation
v = p.v - n.v;
0 = p.i + n.i;
i = p.i;
end OnePort;
model Resistor "Ideal linear electrical resistor"
parameter SI.Resistance R = 1
"Resistance at temperature T_ref";
extends Inheritance.OnePort;
equation
v = R*i;
end Resistor;
model Circuit
Resistor r( R=10);
Modelica.Electrical.Analog.Basic.Ground ground;
Modelica.Electrical.Analog.Sources.ConstantVoltage constantVoltage(V=1);
equation
connect(constantVoltage.n, ground.p);
connect(constantVoltage.p, r.n);
connect(r.p, ground.p);
end Circuit;
end Inheritance;
这样连接器类PositivePin的实例p
Inheritance.PositivePin p;
在部分模型 OnePort 中引入变量 i、v(请参阅language spec 了解潜在和流量变量)。
在模型类 Resistor
extends Inheritance.OnePort;
实例化变量
- i, v
- p.i, p.v 通过 PositivePin 类的实例 p
- n.i, n.v 通过 NegativePin 类的实例 n
还有方程式
- v = p.v - n.v;
- 0 = p.i + n.i;
- i = p.i;
最后,在模型类Circuit中
Resistor r(R=10);
用所谓的修饰符实例化Resistor类,使实例r的电阻值R=10,而不是默认的类值R=1