I have $name
and $age
defined in set_name()
and set_age
. Please check index.php:
<?php
// This part needs some fixing.
class Pet {
public $name;
public $age;
// get() and set() functions for name
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->$name;
}
// get() and set() functions for age
function set_age($age) {
$this->age = $age;
}
function get_age() {
return $this->$name;
}
}
.....
Edit: This problem is fixed. No need for new answers!
You need constructor for your classes and don't do the makeSound
method in your Pet class static
. Try this:
public Cat(){
makeSound(sound);
}
public Dog(){
makeSound(sound);
}
Your method declaration is wrong. If a parent has an implemented version of a function, you do not need to declare it in the subclass. But even if you would want to, the correct way to do it (thus overriding the parent function) would be:
void makeSound(String sound) {
System.out.println("Hi, I make a " + sound);
}
The use of a static keyword would not make much sense. A static method is a method that is part of the class and not of the created objects. Meaning you could call the method like this Pets.makeSound(...)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.