dongzai3139 2016-09-08 23:26
浏览 43

PHP - 使用子类继承。 使用子类更改类中的多个值

I am trying to change the two values, 'first' and 'last' name, using a child class. My textbook only shows how it works if there is only one variable in the main class. I have tried to expand on it and have had to just guess at how it is done when using multiple variables/values. I have tried variations on this and check it with a browser through a server.

<?php
class contactInfo {
    public$first = "Robert";
    public$last = "Heston";
    public$address = "3289 Westmore Blvd.";
    public$city = "Greensboro";
    public$state = "NC";
    public$zip = "27128";

/* The example in the book had the one variable which would be the same as my 'first' and it said the function had to have the same name as the class and it had equivilant of "$ft" in parenthesis and used "$this->first = $ft;" with it. I tried expanding on that and this stuff is just no good. */

    function contactInfo($ft, $lt, $as, $cy, $se, $zp) {
        $this->first = $ft;
        $this->last = $lt;
        $this->address = $as;
        $this->city = $cy;
        $this->state = $se;
        $this->zip = $zp;
    }
    function showMixedInfo() {
    echo "My name is ".$this->first." ".$this -> last."<br/>";
    echo "I live at ".$this ->address."<br/>";
    echo "and my city and state are".$this->city." ".$this->state."<br/>";
    echo "and my zip code is".$this->zip."<br/>";
}
}

/* I created the child class and the variables below the only way I know how. In the book, '$newInfo = new childClass()' had the actual name change in the parenthesis, but I need to change 2 values. */

class childClass extends contactInfo {
    //Maybe there needs to be some code here?
}
$newInfo = new childClass();
$this->first("Graham");
$this->last("Rouse");
$newinfo -> showMixedInfo();
?>

The instructor's "hint" used the child class like this instead and I had no more idea about how to create everything else to go with it than with what I have above:

class childClass extends contactInfo {

function showMixedInfo($ft, $lt) {
    $this->first = $ft;
    $this->last = $lt;
    echo "My name is ".$this->first." ".$this -> last."<br/>";
    echo "I live at ".$this ->address."<br/>";
    echo "and my city and state are".$this->city." ".$this->state."<br/>";
    echo "and my zip code is".$this->zip."<br/>";
}
}

I am at a loss with this one. This instructor gives assignments beyond the scope of the textbook then insists it's in there when no amount of rereading/studying reveals it. I have searched for solutions prior to this.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?