该页面无法处理请求。在本地主机上运行 php

我在本地主机上运行这段代码。在http://localhost:8000/Polymorphism.php上运行时出现错误。这是php上的一个简单的多态代码。其他代码运行起来很轻松,但是运行这段代码就出错了。

此页面不工作本地主机当前无法处理此请求。HTTP 错误 500

<?php



public 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();



?>


侃侃无极
浏览 91回答 1
1回答

蓝山帝景

您永远不会为类或接口分配访问修饰符。它们仅用于指定方法和属性。你在课堂上还有两个错误,Rectangle你应该提到高度和宽度&nbsp; $this->height=$h;&nbsp; $this->width=$w;将您的整体代码更改为<?phpinterface Shape{&nbsp;public function calculateArea();}&nbsp;class Circle implements Shape{&nbsp; private $radius;&nbsp; public function __construct($r){&nbsp; &nbsp;$this->radius=$r;&nbsp;}&nbsp;public function calculateArea(){&nbsp; echo 'Area of circle = '.pi()* $this->radius*$this->radius.'<br>';}}class Rectangle implements Shape{&nbsp;private $height;&nbsp;private $width;&nbsp;public function __construct($h,$w){&nbsp; $this->height=$h;&nbsp; $this->width=$w;}public function calculateArea(){&nbsp;echo 'Area of a Rectangle=' .$this->height.$this->width.'<br>';}}$circle= new Circle(5);$rect= new Rectangle(10,20);$circle->calculateArea();$rect->calculateArea();?>
打开App,查看更多内容
随时随地看视频慕课网APP