小编use*_*817的帖子

防止C#子类覆盖方法

假设我有一个名为"Parent"的抽象父类,它实现了一个名为"DisplayTitle"的方法.我希望这个方法对于继承"Parent"的每个子类都是相同的 - 如果子类试图实现他们自己的"DisplayTitle"方法,我想要编译错误.我怎样才能在C#中实现这一目标.我相信Java,我只是将该方法标记为"最终",但我似乎无法在C#中找到替代方案.我一直在搞"密封"和"覆盖",但我无法得到我正在寻找的行为.

例如,在此代码中:

using System;

namespace ConsoleApplication1
{
  class Parent
  {
      public void DisplayTitle() { Console.WriteLine("Parent's Title"); }
  }

  class ChildSubclass : Parent
  {
      public void DisplayTitle() { Console.WriteLine("Child's Own Implementation of Title"); 
  }

    static void Main(string[] args)
    {
        ChildSubclass myChild = new ChildSubclass();
        myChild.DisplayTitle();
        Console.ReadLine();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我想收到一个编译错误,说"ChildSubClass"不能覆盖"DisplayTitle".我目前得到一个警告 - 但似乎这是我应该能做的事情,我不知道标记方法的正确属性.

c# inheritance object

6
推荐指数
1
解决办法
1487
查看次数

试图理解PHP password_hash

我正在尝试更多地了解PHP安全性最佳实践,我遇到了Anthony_ Fersh 和Anthony Ferrara 的password_compat项目.我想我理解如何实现它,但在测试中,我遇到了一个奇怪的行为,这与我对密码散列的新手理解相矛盾.

如果我将password_hash函数的结果保存到MySQL数据库用户记录中,然后使用password_verify检索该哈希以进行验证,则它会按预期工作.但是,如果我做了完全相同的事情,而不是从数据库中提取,我只是通过数据库中的复制/粘贴硬编码密码哈希,password_verify函数失败.

代码如下:

// Get the Username and password hash from the MySQL database.  GetPassTestuser routine returns an array where
//   position[0][0] is the username and position[0][1] is the password hash.
$arrUser = GetPassTestuser("mike24");
echo("User Name: ".$arrUser[0][0]."<br/>");
echo("Password hash: ".$arrUser[0][1]."<br/>");

// Run password_verify with the password hash collected from the database.  Compare it with the string "mytest"
//  (This returns true in my tests).
if (password_verify("mytest",$arrUser[0][1])){
    echo("Password verified");
} else {
    echo("Password invalid");
} …
Run Code Online (Sandbox Code Playgroud)

php security hash

2
推荐指数
1
解决办法
1065
查看次数

标签 统计

c# ×1

hash ×1

inheritance ×1

object ×1

php ×1

security ×1