PHP帮助手册拾遗三:类与对象

PHP帮助手册拾遗:类与对象

1、__autoload函数
__autoload函数定义时必须包含一个参数,否则会显示如下错误:
Fatal error: __autoload() must take exactly 1 argument

在 __autoload 函数中抛出的异常不能被 catch 语句块捕获并导致致命错误。

2、父类的构造方法调用
如果子类中定义了构造函数则不会暗中调用其父类的构造函数。要执行父类的构造函数,需要在子类的构造函数中调用 parent::__construct()。这里python和php一样

3、父类的析构方法调用
和构造函数一样,父类的析构函数不会被引擎暗中调用。要执行父类的析构函数,必须在子类的析构函数体中显式调用 parent::__destruct()。
析构函数在脚本关闭时调用,此时所有的头信息已经发出。
试图在析构函数中抛出一个异常会导致致命错误。

4、访问限制
var与public在类中的变量定义相同,但是在php5的php5.1.3会生成一个E_STRICT警告
方法如果没有设置访问控制,则将默认设置为public

5、构造方法
PHP提交两种构造方法,以类名和__construct,当类中没有__construct方法时,PHP会调用类名函数,但是如果存在__construct时,不管其访问控制是什么,都不会调用类名函数。
如下所示代码:

1
2
3
4
5
6
7
8
9
10
11
class Demo {
    public function Demo() {
       echo 'Demo function';
    }
 
    private function __construct() {
       echo '__construct function';
    }
}
 
$demo = new Demo();

运行会报错:Fatal error: Call to private Demo::__construct() from invalid context
ps:上面的这个问题是ben前辈提出的,感谢ben前辈昨天的指导

6、静态变量
PHP5.3以后,可以使用变量引用类,但是这个变量不能是关键字(如self,parent,static等)。如下所示代码:

1
2
3
4
5
6
class Demo {
    public static $my_static = 'Demo';
}
 
$classname = 'Demo';
print $classname::$my_static . "\n"; // As of PHP 5.3.0

7、接口
接口可以有常量,不能有变量,并且常量在其子类中无法重载
如下所示代码:

1
2
3
4
5
6
7
8
9
10
11
interface Base {
    const constant = 'constant value';
    public $var = "test var";
}
 
class Foo implements Base {
 
}
 
echo Base::constant;
echo Base::$var;

以上代码报错为:Fatal error: Interfaces may not include member variables
注释掉对变量的操作后可以正常打印常量中存放的值。
8、对象迭代
在PHP5中,使用foreach,遍历一个对象,可以访问这个对象的所有可以访问的属性
如果要自定义迭代器,可以通过实现一个叫Iterator的接口来实现
9、单例模式
单例模式(singleton模式)在实现过程中,需要考虑__clone方法。
10、对象与引用
PHP5之后,对象在默认情况下是以引用的方式传递的。

最小矩阵和

很久很久以前的代码,在某个角落找到,贴了上来,
在杭电acm1081上可以ac,来源应该是Greater New York 2001
貌似作为中南赛区ACM竞赛题

问题描述
【Description】

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.

As an example, the maximal sub-rectangle of the array:

0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2

is in the lower left corner:

9 2
-4 1
-1 8
and has a sum of 15.

【Input】

The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].

【Output】

Output the sum of the maximal sub-rectangle.

【Sample Input】
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1
8 0 -2

【Sample Output】
15

下面的中文来自:http://blog.csdn.net/hym666/archive/2010/08/17/5818591.aspx
此文章处有C++的解答

题目描述:
在一个大的方阵中找出一个子方阵,这个子方阵是所有子方阵和中的最大的一个
问题分析:
问题可以回归到一个数列中的连续字数列的和的最大值。
即把每一行看成一个数列。把每一列转换成一个数。
当然每一列也是一个数列,有很多的连续子数列。所有会有很多中情况。可穷举各种数列来构造一个行向的数列。
再用动态规划计算每一行的最大值,再在各行中选取最大的作为题目的解。

【代码】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.*;
 
public class Main {
 
       /**
        * @param args
        */
 
       public static int maxSum(int[] b, int n){
 
              int max = b[0], sum = 0;
 
              for (int i = 1; i < n ; i++ ){
                     if (max > 0)
                            max += b[i];
                     else
                            max = b[i];
 
                     if (max > sum)
                            sum = max;
              }
              return sum;
       }
 
       public static int maxTotal(int[][] a, int n){
 
              int max = 0;
              int[] b = new int[n];
 
              for (int i = 0; i < n; i++){
                     for (int j = 0; j < n; j++)
                            b[j] = a[i][j];
 
                     int sum = maxSum(b, n);
 
                     if (sum > max)
                            max = sum;
 
                     for (int j = i + 1; j < n; j++){
                            for (int k = 0; k < n; k++)
                                   b[k] += a[j][k];
 
                            sum = maxSum(b, n);
                            if (sum > max)
                                  max = sum;
                     }
              }
              return max;
       }
 
       public static void main(String[] args) {
              // TODO Auto-generated method stub
 
              Scanner cin = new Scanner(System.in);
              int n;
 
              while (cin.hasNext()){
                     n = cin.nextInt();
                     int[][] a = new int[n][n];
                     for (int i = 0; i < n; i++){
                            for (int j = 0; j < n; j++){
                                   a[i][j] = cin.nextInt();                                  
                            }
                     }
                     System.out.println(maxTotal(a, n));
              }
       }
}

如何判断浏览器的javascript版本

如何判断浏览器的javascript版本

话说最近在研究某著名跟踪系统,在其给用户的实施代码中有一段判断浏览器Javascript版本的代码引起了我的注意,于是问了下google如何判断浏览器的javascript版本,他老人家说将所要执行的代码放在如< script language="JavaScript1.2" >所示嵌套下。但是当问到检测javascript版本时,得到如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var JS_ver  = [];
(Number.prototype.toFixed)?JS_ver.push("1.5"):false;
([].indexOf && [].forEach)?JS_ver.push("1.6"):false;
((function(){try {[a,b] = [0,1];return true;}catch(ex) {return false;}})())?JS_ver.push("1.7"):false;
([].reduce && [].reduceRight && JSON)?JS_ver.push("1.8"):false;
("".trimLeft)?JS_ver.push("1.8.1"):false;
JS_ver.supports = function()
{
if (arguments[0])
 
return (!!~this.join().indexOf(arguments[0] +",") +",");
else
 
return (this[this.length-1]);
}
alert("Latest Javascript version supported: "+ JS_ver.supports());
alert("Support for version 1.7 : "+ JS_ver.supports("1.7"));

这个脚本,既能通过检测特征来检测JavaScript版本,还能检查特定的Javascript版本所支持的特性。

得到了结果,我们还是看下此系统是如何检测javascript版本的吧,于是将其代码抽取出来(抽取过程相当纠结),得到如下所示代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<script type="text/javascript">
var n = navigator;
var u = n.userAgent;
 
var apn = n.appName;
var v = n.appVersion;
var ie = v.indexOf('MSIE ')
if(ie > 0){
   apv = parseInt(i = v.substring(ie + 5));
   if(apv > 3) {
       apv = parseFloat(i);
   }
}else{
   apv = parseFloat(v);
}
 
var isie = (apn == 'Microsoft Internet Explorer');
var ismac = (u.indexOf('Mac') >= 0);
 
var javascriptVersion = "1.0";
 
if(String && String.prototype){
   javascriptVersion = '1.1';
 
   if(javascriptVersion.match){
       javascriptVersion = '1.2';
 
       var tm = new Date;
       if(tm.setUTCDate){
           javascriptVersion = '1.3';
 
           if(isie && ismac && apv >= 5) javascriptVersion = '1.4';
 
           var pn = 0;
           if(pn.toPrecision){
               javascriptVersion = '1.5';
 
               a = new Array;
               if(a.forEach){
                   javascriptVersion = '1.6';
 
                   i = 0;
                   o = new Object;
                   tcf = new Function('o','var e,i=0;try{i=new Iterator(o)}catch(e){}return i');
                   i = tcf(o);
                   if(i && i.next) {
                       javascriptVersion = '1.7';
                   }
               }
           }
       }
   }
}
alert(javascriptVersion);
</script>

代码实现原理:根据不同版本的javascript对于一些特定函数的支持不同从而判断其版本所在。其中仅对1.4版本有一个特殊处理。

–EOF–