[英]How to solve undefined variable and undefined property warnings?
我在set_name()
和set_age
中定義了$name
和$age
。 請檢查 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;
}
}
.....
編輯:此問題已修復。 不需要新的答案!
您的類需要構造函數,並且不要在 Pet class static
中執行makeSound
方法。 嘗試這個:
public Cat(){
makeSound(sound);
}
public Dog(){
makeSound(sound);
}
你的方法聲明是錯誤的。 如果父類有一個 function 的實現版本,則不需要在子類中聲明它。 但即使您願意,正確的方法(從而覆蓋父函數)也是:
void makeSound(String sound) {
System.out.println("Hi, I make a " + sound);
}
使用 static 關鍵字沒有多大意義。 static 方法是屬於 class 而不是創建的對象的方法。 這意味着您可以像這樣調用方法Pets.makeSound(...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.