1

我已经进行了很长时间的程序化,从不自己上课。制作了我自己的功能,但从不制作对象。课程让我很困惑,我现在迷失在我的代码中。我不明白他们应该如何让事情变得更容易和更有效率。有人可能会根据我在这里的编码方式指出我做错了什么的正确方向吗?

在前面的例子中,我正在制作一个盒子,里面放了一些东西。图书。书籍有不同的尺寸方式来安排它们放入盒子中,所以我想要在放入时以任何方式对齐它以确定您可以放入多少。LimitDeterminer 确定如果它们都放在相同的方向上可以容纳多少,然后将与那里已经存在的东西进行比较。只是一个简报,代码不完整,因为我很沮丧和困惑,这让我在这里纠正我的错误。我也认为我做错了什么,因为我必须不断地为我的函数指定对象......

谢谢

<?php

function feetToInches($feet){ //Converts feet to inches
    //$feet = $feet * 12;
    return $feet;
}



class Book{
    var $l = 7;
    var $w = 5;
    var $h = 1;
    var $name = "Book";

    function getDimensions($x){ //Return specified dimension, input as string.
        $dimensionArray = array(
                                "l" => $this->l,
                                "w" => $this->w,
                                "h" => $this->h
                                );
        return $dimensionArray[$x];
    }
}

class Box{
    //This is a box. It has length, width, and height, and you can put things in it.
    var $length = 0;
    var $width = 0;
    var $height = 0;
    var $storedArray = array();
    var $totalPerms = array();

    //Set length,width,height
    function setDimensions($l, $w, $h){
        $this->length = feetToInches($l);
        $this->width = feetToInches($w);
        $this->height = feetToInches($h);
    }



    function storedPerms($array){
        $this->totalPerms[] = $array;
        //results in order
        //lhw
        //hlw
        //wlh
        //whl
        //hwl
        //lwh
    }


    function storeThings($thing, $way){
        $ori = $this->totalPerms[$way];
        var_dump($ori);
        $this->storedArray[] = $thing;
        $this->CalcArrange($ori[0],$ori[1],$ori[2],$thing);
    }

    function getThings($key = null) {
        if ($key) {
            return isset($this->storedArray[$key]) ? $this->storedArray[$key] : null;
        } else {
            return $this->storedArray;
        }
    }

    //Set what's against box length to x, width to y, and height to z. Possible values are "l","w","h".
    function CalcArrange($x,$y,$z,$Thing){
        $lengthDiv = 0;
        $widthDiv = 0;
        $heightDiv = 0;


        $lengthDiv = $this->length / $Thing->getDimensions($x); // 24/7   5    5     7      
        $widthDiv = $this->width / $Thing->getDimensions($y);  //14/5     7    1      1
        $heightDiv = $this->height / $Thing->getDimensions($z); //10/1    1    7      5

        $lengthDiv = intval($lengthDiv);
        $widthDiv = intval($widthDiv);
        $heightDiv = intval($heightDiv);

        echo "<br />" . "Length " . $lengthDiv . " times " . "width " . $widthDiv . " times" . " height " . $heightDiv . " is " . ($lengthDiv * $widthDiv * $heightDiv) . "<br />";
    }



}



$thatBook = new Book;
$BookBox = new Box;
$amount = 96;

$BookBox->setDimensions(24,14,10);



//Put objects in box
function putInBox($obj, $box){
    $box->storeThings($obj, 3);
}



$books = $BookBox->getThings();



foreach ($books as $v){
    echo $v->name . "<br />";
}

function CalcTotalLWH($books){
    foreach($books as $book){
        $lengthSum += $book->l;
        $widthSum += $book->w;
        $heightSum += $book->h;
    }


    echo $lengthSum . "<br />";
    echo $widthSum . "<br />";
    echo $heightSum . "<br />";
    //echo $BookBox->length;

}



echo "<br />";


/*
for($i=0; $i < $possible; $i++){
    $addVal = array_pop($dimArray);
    array_unshift($dimArray, $addVal);
    //var_dump($dimArray);
    $BookBox->CalcArrange($dimArray[0],$dimArray[1],$dimArray[2], $thatBook);
}
*/



function findPerm($array, $map){
    $tmp = $array[$map[2]];
    $array[$map[2]] = $array[$map[1]];
    $array[$map[1]] = $tmp;
    return $array;
 //012   
}

function echoAllArray($array){
    echo "Now trying with ";
    foreach($array as $v){
        echo $v . " ";
    }
}

//Determine possible limits of Box to Object
function LimitDetermine($Obj, $Box){
    $dimArray = array("l","w","h");
    $possible = (count($dimArray)) * 2; //for possibilities. DOING IT RONG USE FACTORIALS.
    $map = array(0,1,2);

    foreach($dimArray as $k => $try){

        //012
        //021
        for($i=0; $i < 2; $i++){
            $dimArray = findPerm($dimArray,$map);
            echoAllArray($dimArray);
            $Box->CalcArrange($dimArray[0],$dimArray[1],$dimArray[2], $Obj);
            $Box->storedPerms($dimArray);
            $addVal = array_pop($map);
            array_unshift($map, $addVal);
        }

        //120
        //102

        //201
        //210
    }
}

LimitDetermine($thatBook,$BookBox);

putInBox($thatBook, $BookBox);

/*
$BookBox->CalcArrange("l","w","h", $thatBook); //Face up flat length to length 60
$BookBox->CalcArrange("w","l","h", $thatBook); //Face up flat width to length 80
$BookBox->CalcArrange("w","h","l", $thatBook); //Width to length, standing. 56
$BookBox->CalcArrange("l","h","w", $thatBook); //The way I usually do it. 84
$BookBox->CalcArrange("h","w","l", $thatBook);
*/

var_dump($BookBox->totalPerms);
?>
4

2 回答 2

7

面向对象的代码是一种与过程非常不同的思维方式,我相信你已经熟悉了。本质上,“真正的”面向对象背后的理念(这主要是我在这里的解释)是您创建的每个对象都“做”事情,并且“知道”事情。通常,您可以通过该对象是否真正知道或执行您要求的事情来确定您是否以合乎逻辑的、可理解的方式处理事情。

例如,一个盒子可以存储东西,你可以设置盒子的维度。您甚至可以getThings inside a box,但storedPerms没有多大意义,CalcArrange也没有。稍后我会谈到这些。


您可以使用 __construct 函数让您的生活更轻松一些。每当您创建书籍或盒子时,您都可以设置其初始尺寸/配置,而无需调用辅助函数。

class Box {

    // Define dimension attributes
    public $length;
    public $width;
    public $height;
    public $books;

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

}

而对于书...

class Book {

    // Define dimension attributes
    public $length;
    public $width;
    public $height;
    public $name;

    public function __construct( $name='Book', $length=0, $width=0, $height=0 ) {
        $this->length = feetToInches( $length );
        $this->width =  feetToInches( $width );
        $this->height = feetToInches( $height );
        $this->name = $name;
    }

}

这使您可以像这样创建书籍和盒子:

$myNewBox = new Box(60, 12, 24);
$myNewBook = new Book('All Quiet on the Western Front', 12, 4, 8);

从这里,你可能会使用类似的东西:

class Box {

    ...

    public function getCapacity( $book ) {

        // Determine how many full books can fit within each dimension
        $lengthCapacity = floor( $this->length / $book->length );      
        $widthDiv =       floor( $this->width / $book->width );
        $heightDiv =      floor( $this->height / $book->height );

        return $lengthCapacity * $widthCapacity * $heightCapacity;
    }
}

然后,你可以通过说:

// How many of my new book can fit in my new box?
$bookCapacity = $myNewBox->getCapacity( $myNewBook );

如果您想尝试以不同方式旋转图书以确定绝对最大容量,那就这样做吧!

class Book {

    ...

    public function rotate( $axis ) {

        $startingLength = $this->length;
        $startingWidth = $this->width;
        $startingHeight = $this->height;

        if( $axis == 'x' ) {
            $this->height = $startingLength;
            $this->length = $startingHeight;

        } elseif( $axis == 'y' ) {
            $this->width = $startingLength;
            $this->length = $startingWidth;

        } elseif( $axis == 'z' ) {
            $this->width = $startingHeight;
            $this->height = $startingWidth;

        }

    }
}

然后你可以编写你的 getCapacity 函数的变体来利用它......

class Box {

    ...

    public function getMaxCapacity( $book ) {

        // There are 6 unique ways a book can be positioned
        $axesToRotate = array( 'x', 'y', 'z', 'x', 'y', 'z' );

        $maxCapacity = 0;
        foreach( $axesToRotate as $axisToRotate ) {
            $book->rotate($axisToRotate);
            $maxCapacity = max( $maxCapacity, $this->getCapacity( $book ) );
        }

    }

瞧!您的新功能(假设我已经点了我的 I 并正确地穿过了我的 T),可以像这样运行:

$absoluteMaxCapacity = $myNewBox->getMaxCapacity( $myNewBook );

最后,语义在这里绝对是非常重要的。我花了大约 10% 的时间编写代码来思考调用方法或变量的最佳方式。此外,尽可能避免代码重复。在上面的示例中,我们实现了 getCapacity 函数,然后 getMaxCapacity 使用该函数来节省时间并简化我们的 getMaxCapacity 函数。

希望有帮助!

于 2012-06-03T03:20:31.367 回答
1

本教程适合初学者...

http://net.tutsplus.com/tutorials/php/object-oriented-php-for-beginners/

于 2012-06-03T03:27:01.287 回答