如何在Parent类的方法中调用重写函数

时间:2016-12-15 09:21:44

标签: php

我有一个抽象类,它定义了三种方法:actcalculateStatcalculateRelationship。在act方法中,我调用了calculateStatcalculateRelationship

理想情况下,我希望能够在子类中覆盖这些方法,并通过父{4}方法中的act方法调用它们,但是我实现了试图这样做似乎不起作用;它只会使用父级的actcalculateStat方法。

calculateRelationship

在子类中,它不会调用重写的方法:

abstract class Action {

    protected $name = 'Generic';
    protected $acts_on = Person::STAT_GENERIC;
    protected $action_points = 1;

    public function act($performer, $performee) {
        // Get Perfomer and Performee stats
        $performer_stat = $performer->getStat($this->acts_on);
        $performee_stat = $performee->getStat($this->acts_on);

        // Calculate values
        $new_stat = $this->calculateStat($performer_stat, $performee_stat);

        // Apply values
        $performee->setStat($this->acts_on, $new_stat);

        $stat_diff = $new_stat - $performee_stat;
        $relationship = $performee->getRelationship($performer->getID());

        $new_relationship = $this->calculateRelationship($relationship, $stat_diff);

        $performee->setRelationship($performer->getID(), $new_relationship);

        // Output
        Output::getInstance()->addOutput($performer->getName() . ' performed ' . $this->name . ' on ' . $performee->getName() . ' making their stat change from ' . $performee_stat . ' to ' . $new_stat);
        Output::getInstance()->addOutput($performee->getName() . '\'s relationship with ' . $performer->getName() . ' changed from ' . $relationship . ' to ' . $new_relationship);

    }

    private function calculateStat($performer_stat, $performee_stat) {
        $performer_multiplier = 2 * $performer_stat / Person::STAT_MAX;
        $new_stat = $performee_stat + ($this->action_points * $performer_multiplier);

        if ($new_stat > Person::STAT_MAX) {
            $new_stat = Person::STAT_MAX;
        }

        return $new_stat;
    }

    private function calculateRelationship($relationship, $stat_diff) {
        if ($stat_diff < 0) {
            $relationship_multiplier = -1;
        } else {
            $relationship_multiplier = 1;
        }

        $new_relationship = $relationship_multiplier * ($relationship + ($stat_diff / 2));

        return $new_relationship;
    }
}

1 个答案:

答案 0 :(得分:0)

我通过将方法范围从import { Routes, RouterModule } from '@angular/router'; import { homeComponent } from './home.component'; const routes: Routes = [ { path: 'home', component: homeComponent } ]; export const routing = RouterModule.forChild(routes); 更新为private来解决此问题。