zoukankan      html  css  js  c++  java
  • 面向对象练习

    1.显示数据

    表和类对应起来,表名是类名,列名是类里面的成员

    表里面的每一条数据对应类实例化的对象

    在Info.class.php中创建类:

    <?php
    
    class Info
    
    {
      public $code;
    
      public $name;
    
      public $sex;
    
      public $nation;
    
      public $birthday;
    }

    在新页面加载Info类:

    <?php
    
    include("Info.class.php");
    
    $attr=array();
    
    $info1=new Info();
    
    $info1->code="p001";
    
    $info1->name="张三";
    
    $info1->sex="男";
    $info1->nation="汉族";
    $info1->birthday="1988-2-3";
    
    //向数组里面追加元素:
    
    array_push($attr,$info1);
    
    $info2= new Info();
    $info2->code="p002";
    $info2->name="李四";
    $info2->sex="女";
    $info2->nation="汉族";
    $info2->birthday="1989-2-3";
    
    array_push($attr,$info2);
    
    $info3= new Info();
    $info3->code="p003";
    $info3->name="王五";
    $info3->sex="男";
    $info3->nation="回族";
    $info3->birthday="1990-2-3";
    
    array_push($attr,$info3);
    
    var_dump($attr);
    
    echo "<table width='100%' border='1' cellpadding='0' cellspacing='0'>";
    echo "<tr><td>代号</td><td>姓名</td><td>性别</td><td>民族</td><td>生日</td></tr>";
    
    foreach($attr as $v)
    
    {
      echo "<tr>
    
          <td>{$v->code}</td>
    
          <td>{$v->name}</td>
    
          <td>{$v->sex}</td>  
    
          <td>{$v->nation}</td>
    
          <td>{$v->birthday}</td>
    
         </tr>";
    }
    
    echo "</table>";
    
    ?> 

    2.求两个圆面积之差

    2.1 面向过程:

    $r1=10;
    
    $r2=5;
    
    $mj=$r1*$r1*3.14-$r2*$r2*3.14
    
    echo $mj; 

    2.2 面向对象

    <?php
    class Yuan
    
    {
      public $r;
    
      function_construct($r)
    
      {
        $this->r=$r;
      }
    
      function MianJi()
    
      {
        return $this->r*$this->r*3.14
      }
    }

    加载Yuan类:

    include("Yuan.class.php");
    
    $y1=new Yuan(10);    //造一个大圆
    
    $y2=new Yuan(5);     //造一个小圆
    
    echo $y1->MianJi() - $y2->MianJi();
  • 相关阅读:
    php 采集程序
    15个实用的PHP正则表达式
    jQuery Mobile优秀网站及资源汇总
    连接ORalce数据库
    Windows Mobile如何观看Webcast
    解决wpf中使用WinformsHost显示滚动条的问题
    利用反射调用类及其属性和方法
    一些实用的vs.net2008 快捷键(已验证)
    WW插件研究
    微软今天公布了下一代Visual Studio和.NET Framework开发工具和平台,该平台将被命名的Visual Studio 2010和.Net Framework 4.0.
  • 原文地址:https://www.cnblogs.com/xinghun/p/5453051.html
Copyright © 2011-2022 走看看