如何使用静态方法运行构造函数

时间:2018-06-03 17:24:27

标签: php oop

当我运行我的脚本时,我看到空白页面意味着没有任何价值。请检查我错误的代码以及发生这种情况的原因。

 void FrameGrabber(object sender, EventArgs e)
    {
        label6.Text = "0";
        //label4.Text = "";
        NamePersons.Add("");

        MinNeighbors = int.Parse(comboBoxMinNeigh.Text);  // the 3rd 
parameter
        WindowsSize = int.Parse(textBoxWinSiz.Text);   // the 5th parameter
        ScaleIncreaseRate = Double.Parse(comboBoxScIncRte.Text); //the 2nd 
parameter


        //Get the current frame form capture device
        currentFrame = grabber.QueryFrame().Resize(320, 240, 
Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);

        //Convert it to Grayscale
        gray = currentFrame.Convert<Gray, Byte>();

        //Face Detector
        MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
             face,
             ScaleIncreaseRate, MinNeighbors,
             Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
             new Size(WindowsSize, WindowsSize));
 //Action for each element detected
        foreach (MCvAvgComp f in facesDetected[0])
        {
            t = t + 1;
            result = currentFrame.Copy(f.rect).Convert<Gray, byte> 
().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
            //draw the face detected in the 0th (gray) channel with blue 
color
            currentFrame.Draw(f.rect, new Bgr(Color.Red), 2);

            if (trainingImages.ToArray().Length != 0)
            {
                //TermCriteria for face recognition with numbers of trained 
images like maxIteration
                MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 
0.001);

                //Eigen face recognizer
                EigenObjectRecognizer recognizer = new 
EigenObjectRecognizer(
                   trainingImages.ToArray(),
                   labels.ToArray(),
                   3000,
                   ref termCrit);

                name = recognizer.Recognize(result);

                //Draw the label for each face detected and recognized
                currentFrame.Draw(name, ref font, new Point(f.rect.X - 2, 
f.rect.Y - 2), new Bgr(Color.LightGreen));

            }
            NamePersons[t - 1] = name;
            NamePersons.Add("");

            //Set the number of faces detected on the scene
            label6.Text = facesDetected[0].Length.ToString();
   }
t = 0;

        //Names concatenation of persons recognized
        for (int nnn = 0; nnn < facesDetected[0].Length; nnn++)
        {
            names = names + NamePersons[nnn] + ", ";
        }

        //Show the faces procesed and recognized
        imageBoxFrameGrabber.Image = currentFrame;
        label4.Text = names;
        names = "";
        //Clear the list(vector) of names
        NamePersons.Clear();

    }

所以,这是我的代码我使用的构造函数用于检查,但没有任何工作。这怎么可能?

2 个答案:

答案 0 :(得分:0)

构造函数仅在对象实例化时运行。 首先实例化您的对象,然后运行__construct方法,然后设置$size$width

$h = new house(4,10);
$myHouseSize = house::getsize();
echo $myHouseSize; // will echo 10

如果您不想先实例化某个对象,那么请不要使用构造函数,而是使用常用方法设置您的var并检查getsize() if $size$width已设置或未设置,然后根据需要设置它们。

答案 1 :(得分:0)

你可能误解了PHP中OOP的一些基本方面。

首先,在实例化一个新对象时调用类的构造函数,如下所示:

$house = new house('4','10').

您正在做的是使用两个未使用的参数调用类的静态方法。

其次,为什么要使用静态字段和方法并不是很清楚,因为它们属于类本身而不属于单个实例。

你可能想做的事情是这样的:

class House{
   private $width;
   private $size;

   public function __construct($width,$size){
     $this->width = $width;
     $this->height = $size;
   }

   public function getSize(){
     return $this->size;
   }

   public function getWidth(){
     return $this->width;
   }
}

$house = new House(4,10);
echo $house->getSize();