如何在此代码中创建新实例?

编程入门 行业动态 更新时间:2024-10-28 20:18:09
本文介绍了如何在此代码中创建新实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经获得了一些代码,其中包含一个带有函数的类。我理解其中一些但是我在下面的示例代码中创建一个新实例(一个人)时遇到了麻烦。 我想知道是否有人可以告诉我如何它是使用下面代码中其他示例中使用的代码以及代码将在哪一行上完成的。 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 ".

更多推荐

如何在此代码中创建新实例?

本文发布于:2023-11-14 21:43:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588575.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在此   建新   实例   中创   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!