在Delphi中,如何仅在类的函数/方法之间创建类型

时间:2015-08-17 23:08:59

标签: class delphi types private

我目前有以下代码;

<?php
@$upload_Name = $_FILES['Resume']['name'];
@$upload_Size = $_FILES['Resume']['size'];
@$upload_Temp = $_FILES['Resume']['tmp_name'];
@$upload_Mime_Type = $_FILES['Resume']['type'];

function RecursiveMkdir($path)
 {
   if (!file_exists($path)) 
   { 
      RecursiveMkdir(dirname($path));
      mkdir($path, 0777);
    }
}
// Validation
// check only if file
if( $upload_Size > 0)
{

if( $upload_Size == 0)
{
header("Location: error.html");
}
if( $upload_Size >200000)
{
//delete file 
unlink($upload_Temp);
header("Location: error.html");
}
if( $upload_Mime_Type != "application/msword" AND $upload_Mime_Type !=         "application/pdf" AND $upload_Mime_Type != "application/vnd.openxmlformats-    officedocument.wordprocessingml.document")
{
unlink($upload_Temp);
header("Location: error.html");
}
}//end wrapper of no file
// Where the file is going to be placed 
$target_path = “../../XXXX/uploads/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['Resume']['name']);

if(move_uploaded_file($_FILES['Resume']['tmp_name'], $target_path)) {
echo "The file ".  basename( $_FILES['Resume']['name']). 
" has been uploaded";
} else{
    echo "";
}
?><?php
if(isset($_POST['email'])) {
require_once 'Mail.php'; // PEAR Mail package
require_once 'Mail/mime.php';

$email_to = “name@yoursite.com”;  //Enter the email you want to send the form     to
$email_subject = "Employment Application";  // You can put whatever subject     here 
$host = "mail.yourdomain.com";  // The name of your mail server. (Commonly     mail.yourdomain.com if your mail is hosted with xxx)
$username = "yoursite.com";  // A valid email address you have setup 
$from_address = "name@yoursite.com";  // If your mail is hosted with Site this     has to match the email address above 
$password = “XXX”;  // Password for the above email address
$reply_to = “XXX@yoursite.com";  //Enter the email you want customers to reply to
$port = "50"; // This is the default port. Try port 50 if this port gives you issues and your mail is hosted with Site

function died($error) {
// your error code can go here 
echo "We are very sorry, but there were error(s) found with the form you     submitted. "; 
echo "These errors appear below.<br /><br />"; 
echo $error."<br /><br />"; 
echo "Please go back and fix these errors.<br /><br />";
die();
}

// Validate expected data exists
if(!isset($_POST['Position_Applying']) || !isset($_POST['Position_type']) ||         !isset($_POST['First_name']) || !isset($_POST['Last_name']) ||         !isset($_POST['Street']) || !isset($_POST['City']) || !isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}

$hdw_id = $_POST['hdw_id'];
$hdw_Country = $_POST['hdw_Country'];
$hdw_IP = $_POST['hdw_IP'];
$hdw_Referer = $_POST['hdw_Referer'];
$hdw_ServerTime = $_POST['hdw_ServerTime'];
$hdw_Browser = $_POST['hdw_Browser'];
$hdw_UserAgent = $_POST['hdw_UserAgent'];
$Position_Applying = $_POST['Position_Applying'];
$Position_one = $_POST['Position_one'];
$Position_two = $_POST['Position_two'];
$Position_three = $_POST['Position_three'];
$Position_type = $_POST['Position_type'];
$Shift_type = $_POST['Shift_type'];
$First_name =$_POST['First_name'];
$Middle_name = $_POST['Middle_name'];
$Last_name = $_POST['Last_name'];
$Street = $_POST['Street'];
$City = $_POST['City'];
$State = $_POST['State'];
$Zip = $_POST['Zip'];
$One_Phone = $_POST['One_Phone'];
$crlf = "n";
 // required 
$error_message = ""; 
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
if(!preg_match($email_exp,$email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.            <br />';
} 
$string_exp = "/^[A-Za-z .'-]+$/"; 
if(!preg_match($string_exp,$First_name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Employment Application Details Below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Position Applying: ".clean_string($Position_Applying)."\n";
$email_message .= "Position Type: ".clean_string($Position_type)."\n";
$email_message .= "\n";
$email_message .= "First name: ".clean_string($First_name)."\n";
$email_message .= "Last Name: ".clean_string($Last_name)."\n";
$email_message .= "\n"; 
$email_message .= "Street: ".clean_string($Street)."\n";
$email_message .= "City: ".clean_string($City)."\n";
$email_message .= "State: ".clean_string($State)."\n";
$email_message .= "email: ".clean_string($email)."\n";
$email_message .= "Phone: ".clean_string($One_Phone)."\n"; 
$email_message .= "\n"; 
$email_message .= "Referred By: ".clean_string($Referred_by )."\n";
$email_message .= "Older than 18: ".clean_string($eighteen )."\n";
$email_message .= "US Citizen: ".clean_string($US_citizen)."\n";
$email_message .= "Crime Conviction: ".clean_string($Crime_convict)."\n";
$email_message .= "NYS Professional License:     ".clean_string($NYS_professional_lic)."\n";
$email_message .= "Other License:     ".clean_string($Other_professional_lic)."\n";
$email_message .= "\n"; 
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($Resume,'application/pdf'); 

    // This section creates the email headers
    $auth = array('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password);
    $headers = array('From' => $from_address, 'To' => $email_to, 'Subject' =>     $email_subject, 'Reply-To' => $reply_to);

    // This section send the email
    $smtp = Mail::factory('smtp', $auth);
    $mail = $smtp->send($email_to, $headers, $email_message);

    // This section creates the email headers
    $auth = array('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password);
    $headers = array('From' => $from_address, 'To' => $email, 'Subject' =>             $email_subject, 'Reply-To' => $reply_to);
// This section send the email
    $smtp = Mail::factory('smtp', $auth);
    $mail = $smtp->send($email, $headers, $email_message);



if (PEAR::isError($mail)) {?>
<!-- include your own failure message html here -->
  Unfortunately, the message could not be sent at this time. Please try again later.

<!-- Uncomment the line below to see errors with sending the message -->
<!-- <?php echo("<p>". $mail->getMessage()."</p>"); ?> -->

<?php } else { ?>

<!-- include your own success message html here -->


<?php } } ?>


<style type="text/css">
<!--
.style2 {font-size: 14px}
.style3 {   font-size: 14px;
font-family: Verdana;
}
-->
</style>
<link href=“XXXDatabaseB/js.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
body {
background-image: url(XXXDatabaseB/images/green100px.jpg);
background-color: #FFF09F;
}
.style4 {color: #A20246}
a {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #FFFFFF;
font-weight: bold;
padding: 10px;
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #FFFFFF;
}
a:hover {
text-decoration: underline;
color: #FFF09F;
}
a:active {
text-decoration: none;
color: #FFFFFF;
}
.style5 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: small;
color: #FFFFFF;
}
.style6 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10px;
    color: #FFFFFF;
    }
-->
</style>
    </head>
    <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
    <!-- ImageReady Slices (xxx_square_slice.psd) -->
    <table width="830" border="0" align="center" cellpadding="0" cellspacing="0"     bgcolor="#FFFFFF" id="Table_01">
    <tr valign="top">
    <td height="258" colspan="2"><?php include 'header.php'; ?></td>
  </tr>
    <tr>
      <td width="100%" valign="top">     
    <?php
    $host = “xxxxx.net";
    $username = “xxxxx”;
    $password = “xxxxx”;
       $dbname = “xxxxxx”;

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$dbname")or die("cannot select DB");
$hdw_id = $_POST['hdw_id'];
$hdw_Country = $_POST['hdw_Country'];
$hdw_IP = $_POST['hdw_IP'];
$hdw_Referer = $_POST['hdw_Referer'];
$hdw_Browser = $_POST['hdw_Browser'];
$hdw_UserAgent = $_POST['hdw_UserAgent'];
$Position_Applying = $_POST['Position_Applying'];
$Position_one = $_POST['Position_one'];
$Position_two = $_POST['Position_two'];
$Position_three = $_POST['Position_three'];
$Position_type = $_POST['Position_type'];
$Shift_type = $_POST['Shift_type'];
$First_name =$_POST['First_name'];


$sql = "INSERT INTO `new_app`(`hdw_id`, `hdw_Country`, `hdw_IP`, `hdw_Referer`, `hdw_ServerTime`, `hdw_Browser`, `hdw_UserAgent`, `Position_Applying`, `Position_one`, `Position_two`, `Position_three`, `Position_type`, `Shift_type`, `First_name`) 

 VALUES ('$hdw_id', '$hdw_Country', '$hdw_IP', '$hdw_Referer', CURRENT_TIMESTAMP, '$hdw_Browser', '$hdw_UserAgent', '$Position_Applying', '$Position_one', '$Position_two', '$Position_three', '$Position_type', '$Shift_type', '$First_name')";
$result=mysql_query($sql);

if($result){
echo "";
}

else {
echo "ERROR";
}
mysql_close();
?>
    <table width="100%" height="508" border="0" align="left" cellpadding="20"     cellspacing="5">
      <tbody>
        <tr>
          <td colspan="2" valign="middle">
            <h2>Thank you for your Application!</h2>
</p>

  </td></tr></tbody></table></td>
</tr>
    <tr>
    <td colspan="2" valign="top"><?php include 'footer.php'; ?></td>
  </tr>
</table>
<!-- End ImageReady Slices -->
</body>
</html>

据我所知,这意味着TMyRecord是整个程序可见的全局类型。类型只需要在类中可见,尽管类型的对象确实需要传递并作为参数返回到&#34; private&#34;班级的职能/程序。我怎样才能做到这一点?我无法在&#34; private&#34;下声明类型。类接口的一部分,如果我在实现中声明它,那么我不相信它可以在接口函数原型中使用。此外,我认为实现/接口更多地涉及单元内的可见性而不是类。我是否需要以其他方式声明GetChildren()函数?

2 个答案:

答案 0 :(得分:1)

Per Uwe,只需在私人部分声明。在XE8中测试过。以下作品

In [14]: arr
Out[14]:
array([[ 0.7732126 ,  0.48649481,  0.29771819,  0.91622924],
       [ 0.7732126 ,  0.48649481,  0.29771819,  1.91622924],
       [ 0.58294263,  0.32025559,  0.6925856 ,  0.0524125 ],
       [ 0.58294263,  0.32025559,  0.6925856 ,  0.05      ],
       [ 0.58294263,  0.32025559,  0.6925856 ,  1.7       ],
       [ 0.3239913 ,  0.7786444 ,  0.41692853,  0.10467392],
       [ 0.12080023,  0.74853649,  0.15356663,  0.4505753 ],
       [ 0.13536096,  0.60319054,  0.82018125,  0.10445047],
       [ 0.1877724 ,  0.96060999,  0.39697999,  0.59078612]])

In [15]: import pandas as pd

In [16]: pd.DataFrame(arr)
Out[16]:
          0         1         2         3
0  0.773213  0.486495  0.297718  0.916229
1  0.773213  0.486495  0.297718  1.916229
2  0.582943  0.320256  0.692586  0.052413
3  0.582943  0.320256  0.692586  0.050000
4  0.582943  0.320256  0.692586  1.700000
5  0.323991  0.778644  0.416929  0.104674
6  0.120800  0.748536  0.153567  0.450575
7  0.135361  0.603191  0.820181  0.104450
8  0.187772  0.960610  0.396980  0.590786

In [17]: pd.DataFrame(arr).groupby([0,1,2]).max().reset_index()
Out[17]:
          0         1         2         3
0  0.120800  0.748536  0.153567  0.450575
1  0.135361  0.603191  0.820181  0.104450
2  0.187772  0.960610  0.396980  0.590786
3  0.323991  0.778644  0.416929  0.104674
4  0.582943  0.320256  0.692586  1.700000
5  0.773213  0.486495  0.297718  1.916229

答案 1 :(得分:1)

如其他答案中所述,在支持嵌套类型的Delphi版本中,您只需在所需范围和可见性内声明类型。

对于旧版本的Delphi,您可以使用无类型的 var 参数来获得类似的结果,以避免在您的界面部分引用“私有类型”单元:

TSDIAppForm = class(TForm)
  ..
  procedure GetChildren(Sheet : TTabSheet; var aRecord);
  ..
end;

为了方便实现中的方便和声明式强制,您可以使用absolute声明创建一个局部变量,作为无类型参数的占位符:

procedure TSDIAppForm.GetChildren(    Sheet : TTabSheet; 
                                  var aRecord);
var
  result: TMyRecord absolute aRecord;
begin
  result.List := ...;
  // etc
end;

在这种情况下,由于函数没有直接返回值并且以直接类似的方式使用var param,因此您可以选择使用名称result,如图所示。或者当然,您可以使用您喜欢的本地变量的任何其他名称。

在使用中,您只需使用var参数中的适当变量调用此方法即可:

var
  myRec: TMyRecord;
begin
  ..
  sdiForm.GetChildren(someSheet, myRec);
  ..
end;

通过这种方式,您可以将实施详细信息的类型真正限制在设备的实施部分。

注意: 此类技术在类型化var参数可能会导致编译器抱怨“正式var参数类型不匹配”的情况下也很有用。

当然,您应该始终仔细考虑它们是否是正确的方法。尤其是因为无论何时使用无类型参数,您都要承担更大的责任,以确保代码中的类型安全。滥用的可能性应该是显而易见的,但它们有时也会提供优势(在这种情况下,从接口部分完全删除一种类型,可以说最完全局限于实现部分)。

如果您创建的代码可能会让Delphi旧版本的用户无法使用私有类型等,那么它们也可以成为一个有用的工具。