亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

該頁面無法處理請求。在本地主機上運行 php

該頁面無法處理請求。在本地主機上運行 php

PHP
侃侃無極 2023-03-26 15:29:33
我在本地主機上運行這段代碼。在http://localhost:8000/Polymorphism.php上運行時出現錯誤。這是php上的一個簡單的多態代碼。其他代碼運行起來很輕松,但是運行這段代碼就出錯了。此頁面不工作本地主機當前無法處理此請求。HTTP 錯誤 500<?phppublic interface Shape{   public function calculateArea();}  public class Circle implements Shape{  private $radius;  public function __construct($r){   $this->radius=$r;  }  public function calculateArea(){  echo 'Area of circle = '.pi()* $this->radius*$this->radius.'<br>';  }}class Rectangle implements Shape{ private $height; private $width; public function __construct($h,$w){  this->height=$h;  this->width=$w; } public function calculateArea(){ echo 'Area of a Rectangle=' .$this->height.$this->width.'<br>'; } } $circle= new Circle(5); $rect= new Rectangle(10,20); $circle->calculateArea(); $rect->calculateArea();?>
查看完整描述

1 回答

?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

您永遠不會為類或接口分配訪問修飾符。它們僅用于指定方法和屬性。你在課堂上還有兩個錯誤,Rectangle你應該提到高度和寬度


  $this->height=$h;

  $this->width=$w;

將您的整體代碼更改為


<?php

interface Shape{


 public function calculateArea();



class Circle implements Shape{


  private $radius;



  public function __construct($r){

   $this->radius=$r;

 }


 public function calculateArea(){


  echo 'Area of circle = '.pi()* $this->radius*$this->radius.'<br>';


}

}


class Rectangle implements Shape{


 private $height;

 private $width;


 public function __construct($h,$w){

  $this->height=$h;

  $this->width=$w;

}


public function calculateArea(){

 echo 'Area of a Rectangle=' .$this->height.$this->width.'<br>';

}


}


$circle= new Circle(5);

$rect= new Rectangle(10,20);


$circle->calculateArea();

$rect->calculateArea();

?>


查看完整回答
反對 回復 2023-03-26
  • 1 回答
  • 0 關注
  • 107 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號