如何在此代码中创建新实例? [英] How do I create a new instance in this code?

查看:132
本文介绍了如何在此代码中创建新实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我已经获得了一些代码,其中包含一个带有函数的类。我理解其中一些但是我在下面的示例代码中创建一个新实例(一个人)时遇到了麻烦。



我想知道是否有人可以告诉我如何它是使用下面代码中其他示例中使用的代码以及代码将在哪一行上完成的。



useanimal.php:



Hi,

I have been given some code that has a class with functions in it. I understand some of it but I'm having trouble creating a new instance (a person) in the example code below.

I was wondering if somebody could show me how it is done using the code that is used in the other examples in the code below, and on which line your code would go.

useanimal.php:

<?php

// read in the class definition file
require_once('animal.class.php');

// create an new instance of the dog class
$dog = new Dog("Spot");
$dog->setAge(5);
// use both the dog methods and the dog attributes - notice when you run this that the dog method 'getAge()' will say what the equivalent in human years is whereas the cat won't.
echo "<p>my name is ".$dog->getName()." and I am a ".$dog->type. ", ".$dog->getAge()." and ".$dog->speak()."</p>";

// create a new instance of the cat class
$cat = new Cat("Tigger");
$cat->setAge(2);
// use the cat methods and attributes
echo "<p>my name is ".$cat->getName()." and I am a ".$cat->type. ", ".$cat->getAge(). " and ".$cat->speak()."</p>";

// make yet another cat, quite different from the first cat - the two cats are separate instances
$anotherCat = new Cat("Tibbles");
$anotherCat->setAge( 7 );
// make this cat say who and what it is
echo "<p>my name is ".$anotherCat->getName()." and I am a ".$anotherCat->type. ", ".$anotherCat->getAge(). " and ".$anotherCat->speak()."</p>";

?>





和其他文件是animal.class.php:





and the other file is animal.class.php:

<?php

abstract class Animal {
    protected $sound;
    protected $movement;
    public $type;
    protected $name;
    protected $age;

    protected function __construct( $name = null, $type=null, $movement=null, $sound=null ) {
        // assign to the class variables the value in the local variable
        $this->setName( $name );
        $this->type = $type;
        $this->movement = $movement;
        $this->sound = $sound;
    }

    // common methods that the concrete classes can override or use as is.
    function move() {
        return "I move by {$this->movement}";
    }

    function speak() {
        return "I say {$this->sound}";
    }

    public function setName( $name ) {
        $this->name = $name;
    }
    public function getName() {
        return $this->name;
    }

    public function setAge( $age ) {
        $this->age = $age;
    }

    public function getAge() {
        return "I am {$this->age} years old";
    }
}

/**
 *
 * @package UseClasses
 * A concrete class that will inherit from animal
 */
class Dog extends Animal {

    function __construct( $name = null ) {
        /** call the parent constructor rather than re-write it.  Notice in the Cat class
         * we re-write the constructor - not normally good practice of course to duplicate the code
         * I do it in the Cat class just to demonstrate that you can
         */
        parent::__construct( $name, 'Dog', 'walking and running', 'woof, woof' );
    }

    /** override the animal function to add how many human years the age is equivalent to.
     * This is the advantage of using a method to get/set an attribute - you can convert, manipulate
     * and alter what is actually stored within the class, the client only sees the 'view' the get/sets provide
     */
    public function getAge() {
        return "I am {$this->age} years old, that's " . $this->age * 7 . " human years";
    }
}





/**
 *
 * @package UseClasses
 */
class Cat extends Animal {

    function __construct( $name = null ) {
        /** here we re-write the parent constructor - normally you only do this if
         * there are particular things you want this concrete class to do.
         * Frequently a specialised class will call the parent constructor (parent::__construct)
         * and then, following that, do any other initialisation it needs to
         */
        $this->setName( $name );
        $this->type = 'Cat';
        $this->movement = "slinking and sliding";
        $this->sound = "meiou";
    }
}

?>





非常感谢任何指导



Any guidance is much appreciated

推荐答案

dog = new Dog( Spot);
dog = new Dog("Spot");


dog-> ; setAge(5);
// 同时使用dog方法和dog属性 - 当你运行狗方法时注意' getAge()'会说出人类年份中的等价物,而cat则不会。
echo < p>我的名字是
dog->setAge(5); // use both the dog methods and the dog attributes - notice when you run this that the dog method 'getAge()' will say what the equivalent in human years is whereas the cat won't. echo "<p>my name is ".


dog-> getName()。 我是
dog->getName()." and I am a ".


这篇关于如何在此代码中创建新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆