如何使用面向对象编程进行链式计算?

huangapple go评论64阅读模式
英文:

How to make a chain calculation using OOP?

问题

给定MyCalculator类:

class MyCalculator
{
    public float $a, $b, $c,
    public MyCalculator $result;
            
    public function __construct ($a, $b)
    {
        $this->a = $a;
        $this->b = $b;
        
        $this->result = new MyCalculator($this->c, 0)
    }

    public function add()
    { 
        $this->result->c = $this->a + $this->b;
        return $this->result->c;
    }

    public function divideBy($num)
    { 
        $this->result->c = $this->result->c / $num;
        return $this->result->c;
    }
}

在我的代码中,以下两种用法都有效:

echo $calc->add();  // 显示:15

或者

echo $calc->add()->divideBy(3);   // 显示:5  ((6+9)/3=5)

但是我无法同时让它们都工作!

英文:

Given MyCalculator class

class MyCalculator
{
    public float $a, $b, $c, 
    public MyCalculator $result;
            
    public function __construct ($a, $b)
    {
        $this->a = $a;
        $this->b = $b;
        
        $this->result = new MyCalculator($this->c, 0)
    }

    public function add()
    { 
        $this->result->c = $this->a + $this->b;
        return $this->result->c;
    }

    public function divideBy($num)
    { 
        $this->result->c = $this->result->c / $num;
        return $this->result->c;
    }
}

$calc = new MyCalculator(12, 6);

In my code works good either:

echo $calc->Add()  // Displays: 15

or

echo $calc->Add()->DivideBy(3)   // Displays: 5  ((6+9)/3=5)

But I cannot make them working both!

答案1

得分: -1

根据您问题的描述,您需要设置这个类的定义:

class MyCalculator
{
    private $value1;
    private $value2;
    public $total;

    public function __construct($value1, $value2, $total = null)
    {
        $this->value1 = $value1;
        $this->value2 = $value2;
        $this->total = $total;
    }

    public function add()
    {
        $this->total = $this->value1 + $this->value2;
        return new MyCalculator(0, 0, $this->total);
    }

    public function divideBy($value)
    {
        return $this->total / $value;
    }
}

这个类的作用是在构造函数中设置两个必需的属性值和一个可选的属性值,创建一个 add 方法,该方法返回一个新的类实例,其中 total 匹配构造函数中传递的两个值的总和,并创建一个 divideBy 方法,该方法将当前的总数除以所需的数字。

以下是一个使用示例:

$calc = new MyCalculator(6, 9);
echo $calc->add()->divideBy(3);

Fiddle: https://onlinephp.io/c/498ed

英文:

Based on the description of your problem, you will want to setup this class definition:

class MyCalculator
{
	private $value1;
	private $value2;
	public $total;

    public function __construct($value1, $value2, $total = null)
    {
    	$this->value1 = $value1;
    	$this->value2 = $value2;
    	$this->total = $total;
    }

	public function add()
	{
		$this->total = $this->value1 + $this->value2;
		return new MyCalculator(0, 0, $this->total);
	}

	public function divideBy($value)
	{
		return $this->total / $value;
	}
}

What this does is set two required property values and one optional property value in the constructor, create an add method that returns a new instance of the class where the total matches the sum of the two values passed in the constructor, and creates a divideBy method that divides the current total by the desired number.

Here is an example of using it:

$calc = new MyCalculator(6, 9);
echo $calc->add()->divideBy(3);

Fiddle: https://onlinephp.io/c/498ed

huangapple
  • 本文由 发表于 2023年2月16日 02:42:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定