zoukankan      html  css  js  c++  java
  • 用Delphi 、VB.net以及C#混合编程

    转载至:http://www.cnblogs.com/zhenyulu/articles/53834.html

    这个礼拜三晚上的.net讲座被取消了,原因是与我的课程冲突,并且近20天内不允许停调课。所以只能在这里将我讲座中的部分演示内容预先公布一下了。

    演示内容之一便是“同一平台、多种语言”。在.net的CLR平台上不同语言编写的程序可以相互调用。其UML图如下:



    我们使用Delphi 8编写Person类,并编译成DLL文件。代码如下:

    unit TPerson;
    interface
    type
      Person 
    = class
      
    private
        { 
    Private Declarations }
      
    public
        Name : 
    string;
        Age : 
    integer;
        constructor Create;
      
    end;
    implementation
    constructor Person.Create;
    begin
      inherited Create;
    end;
    end.


    在VB.NET添加对Delphi编写的DLL的引用,并编写继承自Person类的Employee类。

    Imports System

    Public Class Employee
        
    Inherits TPerson.Person

        
    Public Salary As Int32

        
    Public Sub Show()
            Console.
    WriteLine("The Name is: " & Me.Name)
            Console.
    WriteLine("The Age is:" & Me.Age)
            Console.
    WriteLine("The Salary is:" & Me.Salary)
        
    End Sub


    End Class


    下面的工作就是用C#编写代码调用Delphi与VB.NET生成的DLL。分别将两个DLL的引用添加到项目中,然后编写调用程序:

    using System;
    using TEmployee;

    public class Client
    {
      
    public static void Main()
      
    {
        Employee e 
    = new Employee();
        e.Name 
    = "Tom";
        e.Age 
    = 22;
        e.Salary 
    = 1500;
        e.Show();
      }

    }

    到此为止,程序编写完成,看看效果吧。完整的程序代码可以从这里下载。

  • 相关阅读:
    jmeter压力测试
    反射【类Class、成员变量Field、方法Method】
    模块十 python标准库
    模块五 python常用数据结构
    模块四 python函数
    模块三 python控制流语法
    模块二 python基本数据类型与操作
    第四章后总结文档
    第六章节练习
    第五章节练习
  • 原文地址:https://www.cnblogs.com/jshchg/p/2122123.html
Copyright © 2011-2022 走看看