我已经在程序上工作了很长时间,从来没有自己上课。制作我自己的功能但从不反对。类对我来说太混乱了,我现在在代码中迷失了。我不明白他们应该如何让事情变得更容易,更有效率。根据我在这里编码的方式,有人可能会指出我正在做错的方向吗?
在上一个例子中,我正在制作一个盒子,并且要投入一些东西。图书。书籍有不同的尺寸方式来安排将它们放在盒子里,所以我想要在它放入时将它对齐,以确定你可以适应多少。 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);
?>
答案 0 :(得分:7)
面向对象的代码是一种非常不同的程序化思维方式,我相信你已经熟悉了。本质上,“真正的”OO背后的想法(这主要是我在这里的解释)是你创造的每个对象“做”事物,并且“知道”事物。通常情况下,您可以通过这种对象是否真正了解或执行您要求的内容来确定您是否以合乎逻辑的,可理解的方式处理事物。
例如,框可以 storeThings ,您可以 setDimensions 框。您甚至可以在框中 getThings ,但 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并且正确地穿过我的Ts),可以这样运行:
$absoluteMaxCapacity = $myNewBox->getMaxCapacity( $myNewBook );
最后,语义在这里绝对非常重要。我花了大约10%的时间编写思考调用方法或变量的最佳方法。另外,尽可能避免代码重复。在上面的例子中,我们实现了getCapacity函数,后来getMaxCapacity使用它来节省时间并简化getMaxCapacity函数。
希望有所帮助!
答案 1 :(得分:1)