104.私有类型(Private Type)和有限专用类型(Limited Private Type)

私有类型(Private Type)和有限专用类型(Limited Private Type)的区别在于
在程序包外,对私有类型数据只能执行 := 和 = 操作如果嫌私有类型的 := 和 = 这两个默认操作也多余,则使用有限专用类型,也就是说被声明为 limited private类型的话,其私有变量是不能进行 判等,赋值操作的。也即

If the user were to declare two variables of this type, the compiler would not allow him to compare them for equality or inequality, nor could he assign one variable to the other

three.ads

-- This package uses a data structure composed of three INTEGER
-- variables.  It allow the user to add two structures, component
-- by component, or subtract component by component.  Provision is
-- also made to build a structure from three numbers, or decompose
-- a structure into its components.

package Three is

type DATA_STRUCTURE is limited private;

function "+"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE;
function "-"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE;
function Build_Structure(Val1, Val2, Val3 : INTEGER) return
                                                   DATA_STRUCTURE;
procedure Decompose(Data1 : DATA_STRUCTURE;
                    Val1, Val2, Val3 : out INTEGER);
procedure Assign_Struct(Data1 : out DATA_STRUCTURE;
                        Data2 : in DATA_STRUCTURE);
function Compare(Data1, Data2 : DATA_STRUCTURE) return BOOLEAN;

private
   type DATA_STRUCTURE is
      record
         Value1 : INTEGER;
         Value2 : INTEGER;
         Value3 : INTEGER;
      end record;
end Three;

three.adb

package body Three is

function "+"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE is
Temp : DATA_STRUCTURE;
begin
   Temp.Value1 := Data1.Value1 + Data2.Value1;
   Temp.Value2 := Data1.Value2 + Data2.Value2;
   Temp.Value3 := Data1.Value3 + Data2.Value3;
   return Temp;
end "+";


function "-"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE is
Temp : DATA_STRUCTURE;
begin
   Temp.Value1 := Data1.Value1 - Data2.Value1;
   Temp.Value2 := Data1.Value2 - Data2.Value2;
   Temp.Value3 := Data1.Value3 - Data2.Value3;
   return Temp;
end "-";


function Build_Structure(Val1, Val2, Val3 : INTEGER) return
                                                   DATA_STRUCTURE is
Temp : DATA_STRUCTURE;
begin
   Temp.Value1 := Val1;
   Temp.Value2 := Val2;
   Temp.Value3 := Val3;
   return Temp;
end Build_Structure;


procedure Decompose(Data1 : DATA_STRUCTURE;
                    Val1, Val2, Val3 : out INTEGER) is
begin
   Val1 := Data1.Value1;
   Val2 := Data1.Value2;
   Val3 := Data1.Value3;
end Decompose;



procedure Assign_Struct(Data1 : out DATA_STRUCTURE;
                        Data2 : in DATA_STRUCTURE) is
begin
   Data1 := Data2;
end Assign_Struct;



function Compare(Data1, Data2 : DATA_STRUCTURE) return BOOLEAN is
begin
   return Data1 = Data2;
end Compare;

end Three;



main.adb

-- This program exercises the package Three as an illustration.

with Ada.Text_IO;   use Ada.Text_IO;
with Three;         use Three;

procedure LimPriv is

   My_Data, Extra_Data : DATA_STRUCTURE;

begin

   Assign_Struct(My_Data, Build_Structure(3, 7, 13));
   Assign_Struct(Extra_Data, Build_Structure(-4, 77, 0));
   Assign_Struct(My_Data, My_Data + Extra_Data);

   if not Compare(My_Data, Extra_Data) then
      Put_Line("The two structures are not equal.");
   end if;

   Assign_Struct(My_Data, Extra_Data);

   if Compare(My_Data, Extra_Data) then
      Put_Line("The two structures are equal now.");
   end if;

--       The following line is illegal with the limited private type
--  My_Data.Value1 := My_Data.Value1 + 13;

end LimPriv;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值