【十六】使用面向对象 写一个个人主页

page.inc.php

  1 <?php
  2     class page{
  3         public $content;
  4         public $title="tla consulting pty ltd";
  5         public $keywords="tla consulting,three latter abbreviation,some of my best friends are search engines";
  6         public $buttons=array("home"=>"home.php",
  7                             "contact"=>"contact.php",
  8                             "services"=>"services.php",
  9                             "site map"=>"map.php"
 10             );
 11         public function __set($name,$value){
 12             $this->name=$value;
 13         }
 14         public function display(){
 15             echo "<html>\n<head>\n";
 16             $this->displaytitle();
 17             $this->displaykeywords();
 18             $this->displaystyles();
 19             echo "</head>\n<body>\n";
 20             $this->displayheader();
 21             $this->displaymenu($this->buttons);
 22             echo $this->content;
 23             $this->displayfooter();
 24             echo "</body>\n</html>\n";
 25         }
 26         public function displaytitle(){
 27             echo "<title>".$this->title."</title>";
 28         }
 29         public function displaykeywords(){
 30             echo "<meta name=\"keywords\" content=\"".$this->keywords."\"/>";
 31         }
 32         public function displaystyles(){
 33             ?>
 34             <style>
 35                 h1{
 36                     color:white;font-size:24pt;text-align:center;
 37                     font-family:arial,sans-serif
 38                 }
 39                 .menu{
 40                     color:white;font-size:12pt;text-align:center;
 41                     font-family:arial,sans-serif;font-weight:bold
 42                 }
 43                 td{
 44                     background:black
 45                 }
 46                 p{
 47                     color:black;font-size:12pt;text-align:justify;
 48                     font-family:arial,sans-serif
 49                 }
 50                 p.foot{
 51                     color:white;font-size:9pt;text-align:center;
 52                     font-family:arial,sans-serif;font-weight:bold
 53                 }
 54                 a:link,a:visited,a:activep{
 55                     color:white
 56                 }
 57             </style>
 58             <?php
 59         }
 60         public function displayheader(){
 61             ?>
 62             <table width=100% cellpadding="12" cellspacing="0" border="0">
 63                 <tr bgcolor="black">
 64                     <td align="left"><img src="logo.jpg"/></td>
 65                     <td>
 66                         <h1>huahua‘s page</h1>
 67                     </td>
 68                     <td align="right"><img src="logo.jpg"/></td>
 69                 </tr>
 70             </table>
 71             <?php
 72         }
 73         public function displaymenu($buttons){
 74             echo "<table width=\"200\" bgcolor=\"white\" cellpadding=\"4\"cellspacing=\"4\">\n";
 75             echo "<tr>\n";
 76             $width=200/count($buttons);
 77             while (list($name,$url)=each($buttons)) {
 78                 //注意: !$this->IsURLCurrentPage
 79                 $this->displaybutton($width,$name,$url,!$this->IsURLCurrentPage($url));
 80             }
 81             echo "</tr>\n";
 82             echo "</table>\n";
 83         }
 84         public function IsURLCurrentPage($url){
 85             //判断这个url是否指向当前页面.
 86             //如果指向当前页面返回true
 87             if (strpos($_SERVER[‘PHP_SELF‘],$url)==false) {
 88                 return false;
 89             }else{
 90                 return true;
 91             }
 92         }
 93         public function displaybutton($width,$name,$url,$active=true){
 94             if ($active) {
 95                 echo "<td width=\"".$width."%\">
 96                 <a href=\"".$url."\">
 97                 <img src=\"s-logo.jpg\" alt=\"".$name."\" border=\"0\"/></a>
 98                 <a href=\"".$url."\"><span class=\"menu\">".$name."</span></a>
 99                 </td>";
100             }else{
101                 echo "<td width=\"".$width."%\">
102                 <img src=\"side-logo.jpg\"><span class=\"menu\">".$name."</span></td>";
103             }
104         }
105         public function displayfooter(){
106             ?>
107             <table width="100%" bgcolor="black" cellpadding="12" border="0">
108                 <tr>
109                     <td>
110                         <p class="foot">&copy;tla consulting pty ltd.</p>
111                         <p class="foot">please see our<a href="">legal infotmation page</a></p>
112                     </td>
113                 </tr>
114             </table>
115             <?php
116         }
117     }
118     ?>

home.php

1 <?php
2     require(‘page.inc.php‘);
3     $homepage=new page();
4     $homepage->title="huahua girl is best!";
5     $homepage->content="huahua girl have good good study,day day up!!!";
6     $homepage->display();
7 ?>

service.php

 1 <?php
 2     require(‘page.inc.php‘);
 3     class servicespage extends page{
 4         private $roe2buttons=array(
 5             "reengineering"=>"reengineering.php",
 6             "standards compliance"=>"standards.php",
 7             "buzzword conliance"=>"buzzword.php",
 8             "mission statements"=>"mission.php"
 9             );
10         //继承了page类 新增了及格菜单
11         public function display(){
12             echo "<html>\n<head>\n";
13             $this->displaytitle();
14             $this->displaykeywords();
15             $this->displaystyles();
16             echo "</head>\n<body>\n";
17             $this->displayheader();
18             $this->displaymenu($this->buttons);
19             $this->displaymenu($this->roe2buttons);
20             echo $this->content;
21             $this->displayfooter();
22             echo "</body>\n</html>\n";
23         }
24     }
25     $service=new servicespage();
26     $service->content="<p>this is service page</p>";
27     $service->display();
28 ?>

界面显示:

时间: 2024-08-29 08:26:39

【十六】使用面向对象 写一个个人主页的相关文章

利用面向对象写一个计算器

本文参考了程杰的<大话设计模式>,使用C#语言利用面向对象的模式来写一个计算器. 如果是我本人来写计算器,也就加减乘除的话,估计我会全部写进控制台里面,写4个if语句出来......或者我会利用switch,但是这样的效果都不好,有更好的方法,就是如下所示的代码啦: 先定义一个Operation类,主要实现运算框架: class Operation { public double Number1 { get; set;} public double Number2 { get; set; }

Java小练习之利用面向对象写一个简单的登录系统

import java.util.Scanner; /** * 采用面向对象的方式 写一个登录系统 * @author Administrator * */ //用户信息 class UserInfo{ public static String[] user = new String[10]; public static String[] passwd = new String[10]; public UserInfo() { this.user[0] = "test"; this.p

第二十六篇 面向对象初识

一. 三大编程范式 前面学完了Python的基本语法,能写Python代码, 而且可以处理工作中的一些问题,今天开始就要进入面向对象的学习了.首先,了解下三大编程范式,编程范式就是编程方法论,表明的是一种编程风格. 切记:三种编程风格没有好坏之分,有分别的是使用不同风格的人. 1. 面向过程编程 核心是过程二字,过程指的是解决问题的步骤,即先干什么再干什么......面向过程的设计就好比精心设计好一条流水线,是一种机械式的思维方式. 优点是:复杂度的问题流程化,进而简单化(一个复杂的问题,分成一

第十六章----面向对象(宠物乱斗之主面板)

学完了面向对象,赵老师让做一个电子宠物,对于我这样的游戏迷来说,就忍不住做一个好玩的小游戏出来,于是,便运用目前所学的知识,经过了大概3到4天的时间,克服了重重困难,无数次修改,不断的修复BUG,不断的实现新的功能,不断的推翻重写,唉,终于做出了一个终极版.在此,记录一下. package com.maya.chongwu; import java.util.Scanner; public class Main { public static String a;/*a,b,c,用来给创建的角色起

十六、面向对象编程

面向对象编程类的概念 : 具有相同属性和技能的一类事物人类 抽象对象 : 就是对一个类的具体的描述具体的人 具体 使用面向对象的好处: 使得代码之间的角色关系更加明确 增强了代码的可扩展性 规范了对象的属性和技能面向对象的特点:结局的不确定性 1 def Person(name,sex,hp,ad): 2 # 人模子 3 self = {'name': name, 'sex':sex, 'hp': hp, 'ad': ad} 4 def attack(dog): # 闭包 5 # 人攻击狗 6

JS基础入门篇(三十六)—面向对象( 三 )

1.class class es6 中 为 关键字用来声明 一个 类 1.只能通过new调用 2.不存在变量提升 3.多个方法之间直接书写,不需要,隔开 4.类的方法是直接定义在类的原型上的 5.定义在类中的方法不可枚举 6.Object.keys() 和 Object.values() 下面是详细解释 1.只能通过new调用 <script> //--------------- class ------------------ class Fn{} //Fn(); //直接调用会报错 var

Java SE 第十六讲----面向对象特征之继承

1.继承(inheritance):Java是单继承的,意味着一个类只能从另一个类继承(被继承的类叫做父类也叫[基类 baseclass]),继承的类叫做子类,java中的继承使用extends关键字. public class Child extends Parent{ public Child(){ System.out.println("child"); } public static void main(String[] args){ Child child = new Chi

Python之路(第二十六篇) 面向对象进阶:内置方法

一.__getattribute__ object.__getattribute__(self, name) 无条件被调用,通过实例访问属性.如果class中定义了__getattr__(),则__getattr__()不会被调用(除非显示调用或引发AttributeError异常) class Foo: ? def __init__(self,x): self.x = x ? def __getattr__(self, item): print("执行__getattr__") ?

python 全栈 python基础 (十六)面向对象编程的 继承 多态与多态性 封装

一.继承顺序: 多继承情况下,有两种方式:深度优先和广度优先 1.py3/py2 新式类的继承:在查找属性时遵循:广度优先 继承顺序是多条分支,按照从左往右的顺序,进行一步一步查找,一个分支走完会走另一个分支(若多条分支汇总一个头,除最后一条分支能走到头,其他的都走到次之位置停止,进行下一条分支的查找),直到查找到头为止. 可以利用 类名.__mro__ 的方法查看类之间的继承关系(经典类没有这种方法) 1 class B(object): 2 def func(self): 3 print('