【问题标题】:Modelica: How to assign an instance of type Child into variable of type Parent?Modelica:如何将 Child 类型的实例分配给 Parent 类型的变量?
【发布时间】:2020-06-08 07:26:54
【问题描述】:

我有课:

class Parent 
end parent;

class Child 
  extends Parent; 
end Child;

在另一个类中,我需要声明一个 Parent 类型的变量 并通过 Child 类型的实例对其进行初始化; 类似于 Java 语句的东西:

Parent p = new Child();

如何在 Modelica 中做到这一点?

谢谢

【问题讨论】:

    标签: inheritance modelica


    【解决方案1】:

    Modelica 的类概念是 Java 无法比拟的。例如,你不创建新对象,所以没有什么像

    Parent p = new Child();
    

    相反,您只需通过编写实例化一个类

    Parent p;
    

    因此,您不能将子对象分配给父基类。相反,Modelica 的继承特性可以用于重新声明。这意味着,您可以在某个地方实例化一个可替换的基类,以后可以使用redeclare 语句进行更改。

    请参阅下面的简单示例。 Example2cls 重新声明为子类,所以我们模拟它时得到cls.c=2

    package Demo
    
      class Parent
        constant Real c=1;
      end Parent;
    
      class Child
        extends Parent(c=2);
      end Child;
    
      model Example1
        replaceable Parent cls;
      end Example1;
    
      model Example2
         extends Example1(redeclare Child cls);
      end Example2;
    
    end Demo;
    

    总而言之,可以说 Modelica 的面向对象简化为

    • 继承,使用extends
    • 重新声明,使用redeclare

    【讨论】:

      【解决方案2】:

      我不确定您想要实现什么。考虑到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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-27
        • 1970-01-01
        相关资源
        最近更新 更多