iLeichun

当前位置:首页

用Java讲解继承、组合与聚合的关系

分类:Java  来源:网络  时间:2011-3-16 11:30:07

继承与组合的关系:

比如有一个歌星类:
public class MusicStar{
public abstract void singing() ;
}

周杰伦类
public class ZhouJieLun extends MusicStar{
public void singing(){
System.out.println("我边吃橄榄边唱歌") ;
}
}

然后,我也要唱歌,但是我自己不会,那怎么办呢?
第一中办法,我继承周杰伦类,用他的方法唱歌,这样我就也会边吃橄榄边唱歌了,代码如下
public class Me extends ZhouJieLun{
public static void main(String[] args){
new Me().singing() ;//打印出“我边吃橄榄边唱歌”
}
}

另外一个办法,就是组合复用
public class Me{
public void singing(){
new ZhouJieLun().singing() ;//打印出“我边吃橄榄边唱歌”
}
}

 
组合与聚合的关系:

组合和聚合是有很大区别的,这个区别不是在形式上,而是在本质上:
比如A类中包含B类的一个引用b,当A类的一个对象消亡时,b这个引用所指向的对象也同时消亡(没有任何一个引用指向它,成了垃圾对象),这种情况叫做组合,反之b所指向的对象还会有另外的引用指向它,这叫聚合。


在实际中组合方式一般这样写:
A类的构造方法里创建B类的对象,也就是说,当A类的一个对象产生时,B类的对象随之产生,当A类的这个对象消亡时,它所包含的B类的对象也随之消亡。
 

聚合方式则是这样:
A类的对象在创建时不会立即创建B类的对象,而是等待一个外界的对象传给它,传给它的这个对象不是A类创建的。


现实生活中:
人和人和手,脚是组合关系,因为当人死亡后人的手也就不复存在了。人和他的电脑是聚合关系。

  1. class Hand{
  2. }
  3. class Computer{
  4. }
  5. 组合:
  6. class Person{
  7. private Hand hand;
  8. public Person(){
  9. hand = new Hand();
  10. }
  11. }
  12. 聚合:
  13. class Person{
  14. private Computer computer;
  15. public setComputer(){
  16. computer = new Computer();
  17. }
  18. }

可以说聚合是一种强组合的关系,至于什么时候用继承,什么时候用组合,就看具体洞察力

来源:http://blog.sina.com.cn/s/blog_681e57810100jkyb.html

php如何生成随机密码

分类:PHP  来源:网络  时间:2011-3-12 20:28:26

使用PHP开发应用程序,尤其是网站程序,常常需要生成随机密码,如用户注册生成随机密码,用户重置密码也需要生成一个随机的密码。随机密码也就是一串固定长度的字符串,这里我收集整理了几种生成随机字符串的方法,以供大家参考。

php生成随机密码的方法一:

1、在 33 – 126 中生成一个随机整数,如 35,

2、将 35 转换成对应的ASCII码字符,如 35 对应 #

3、重复以上 1、2 步骤 n 次,连接成 n 位的密码

该算法主要用到了两个函数,mt_rand ( int $min , int $max )函数用于生成随机整数,其中 $min – $max 为 ASCII 码的范围,这里取 33 -126 ,可以根据需要调整范围,如ASCII码表中 97 – 122 位对应 a – z 的英文字母,具体可参考 ASCII码表; chr ( int $ascii )函数用于将对应整数 $ascii 转换成对应的字符。

function create_password($pw_length = 8) {
    $randpwd = ¹¹;
    for ($i = 0; $i < $pw_length; $i++) {
        $randpwd .= chr(mt_rand(33, 126));
    }
    return $randpwd;
}

// 调用该函数,传递长度参数$pw_length = 6
echo create_password(6);

php生成随机密码的方法二:

1、预置一个的字符串 $chars ,包括 a – z,A – Z,0 – 9,以及一些特殊字符

2、在 $chars 字符串中随机取一个字符

3、重复第二步 n 次,可得长度为 n 的密码

function generate_password( $length = 8 ) {
    // 密码字符集,可任意添加你需要的字符
    $chars = ¹abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|¹;

    $password = ¹¹;
    for ( $i = 0; $i < $length; $i++ ) {
        // 这里提供两种字符获取方式
        // 第一种是使用 substr 截取$chars中的任意一位字符;
        // 第二种是取字符数组 $chars 的任意元素
        // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }

    return $password;
}

php生成随机密码的方法三:

1、预置一个的字符数组 $chars ,包括 a – z,A – Z,0 – 9,以及一些特殊字符

2、通过array_rand()从数组 $chars 中随机选出 $length 个元素

3、根据已获取的键名数组 $keys,从数组 $chars 取出字符拼接字符串。该方法的缺点是相同的字符不会重复取。

function make_password( $length = 8 ) {
    // 密码字符集,可任意添加你需要的字符
    $chars = array(¹a¹, ¹b¹, ¹c¹, ¹d¹, ¹e¹, ¹f¹, ¹g¹, ¹h¹,¹i¹, ¹j¹, ¹k¹, ¹l¹,¹m¹, ¹n¹, ¹o¹, ¹p¹, ¹q¹, ¹r¹, ¹s¹,¹t¹, ¹u¹, ¹v¹, ¹w¹, ¹x¹, ¹y¹,¹z¹, ¹A¹, ¹B¹, ¹C¹, ¹D¹,¹E¹, ¹F¹, ¹G¹, ¹H¹, ¹I¹, ¹J¹, ¹K¹, ¹L¹,¹M¹, ¹N¹, ¹O¹,¹P¹, ¹Q¹, ¹R¹, ¹S¹, ¹T¹, ¹U¹, ¹V¹, ¹W¹, ¹X¹, ¹Y¹,¹Z¹,¹0¹, ¹1¹, ¹2¹, ¹3¹, ¹4¹, ¹5¹, ¹6¹, ¹7¹, ¹8¹, ¹9¹, ¹!¹,¹@¹,¹#¹, ¹$¹, ¹%¹, ¹^¹, ¹&¹, ¹*¹, ¹(¹, ¹)¹, ¹-¹, ¹_¹,¹[¹, ¹]¹, ¹{¹, ¹}¹, ¹<¹, ¹>¹, ¹~¹, ¹`¹, ¹+¹, ¹=¹, ¹,¹,¹.¹, ¹;¹, ¹:¹, ¹/¹, ¹?¹, ¹|¹);

    // 在 $chars 中随机取 $length 个数组元素键名
    $keys = ($chars, $length);

    $password = ¹¹;
    for($i = 0; $i < $length; $i++) {
        // 将 $length 个数组元素连接成字符串
        $password .= $chars[$keys[$i]];
    }

    return $password;
}

时间效率对比

我们使用以下PHP代码,计算上面的 3 个随机密码生成函数生成 6 位密码的运行时间,进而对他们的时间效率进行一个简单的对比。

<?php
function getmicrotime() {
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}
 
// 开始时间
$time_start = getmicrotime();
  
// 这里放要执行的PHP代码,如:
// echo create_password(6);
 
// 结束时间
$time_end = getmicrotime();
$time = $time_end - $time_start;

 // 输出运行总时间
echo "执行时间 $time seconds";
?>

最终得出的结果是:

方法一:9.8943710327148E-5 秒

方法二:9.6797943115234E-5 秒

方法三:0.00017499923706055 秒

可以看出方法一和方法二的执行时间都差不多,而方法三的运行时间稍微长了点。

PHP/ASP/JSP/ASP.NET/CGI的301重定向代码

分类:网站建设  来源:网络  时间:2011-3-12 20:26:07

301重定向不但可以在Apache服务器或者IIS中配置,也可以使用代码的形式来做301重定向,而且方法很简单,这里icech整理了一个各种语言编写的301重定向代码给大家,各取所需吧!

PHP 301 重定向代码

301重定向也可以在php文件中通过加入php header来实现,代码如下:

    <?php
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://farlee.info/newpage.html");
    exit();
    ?>

ASP 301 重定向代码

    <%@ Language=VBScript %>
    <%
    Response.Status="301 Moved Permanently"
    Response.AddHeader "Location", http://farlee.info
    %>

ASP.NET 301 重定向代码

    <script language="c#" runat="server">
    private void Page_Load(object sender, System.EventArgs e)
    {
    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location",http://farlee.info);
    }
    </script>

ColdFusion 301 重定向代码

    <.cfheader statuscode="301" statustext="Moved permanently">
    <.cfheader name="Location" value="http://farlee.info/newpage.html">

CGI Perl下的301转向代码

    $q = new CGI;
    print $q->redirect("http://farlee.info");

JSP下的301转向代码

    <%
    response.setStatus(301);
    response.setHeader( "Location", "http://farlee.info" );
    response.setHeader( "Connection", "close" );
    %>

 

文章来源:侠客站长站(www.xkzzz.com) 详文参考:http://www.xkzzz.com/zz/netbc/php/201009/22-56332.html

PHP生成同比例的缩略图代码

分类:PHP  来源:网络  时间:2011-3-12 20:24:30

创建图像缩略图需要许多时间,此代码将有助于了解缩略图的逻辑。
 
/**********************
*@filename - path to the image
*@tmpname - temporary path to thumbnail
*@xmax - max width
*@ymax - max height
*/
function resize_image($filename, $tmpname, $xmax, $ymax)
{
    $ext = explode(".", $filename);
    $ext = $ext[count($ext)-1];

    if($ext == "jpg" || $ext == "jpeg")
        $im = imagecreatefromjpeg($tmpname);
    elseif($ext == "png")
        $im = imagecreatefrompng($tmpname);
    elseif($ext == "gif")
        $im = imagecreatefromgif($tmpname);

    $x = imagesx($im);
    $y = imagesy($im);

    if($x <= $xmax && $y <= $ymax)
        return $im;

    if($x >= $y) {
        $newx = $xmax;
        $newy = $newx * $y / $x;
    }
    else {
        $newy = $ymax;
        $newx = $x / $y * $newy;
    }

    $im2 = imagecreatetruecolor($newx, $newy);
    imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
    return $im2;
}

 

文章来源:侠客站长站(www.xkzzz.com) 详文参考:http://www.xkzzz.com/zz/netbc/php/201009/22-56337.html

PHP Zip压缩和解压缩

分类:PHP  来源:网络  时间:2011-3-12 20:22:46

PHP文件 Zip 压缩

/* creates a compressed zip file */
function create_zip($files = array(),$destination = ¹¹,$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo ¹The zip archive contains ¹,$zip->numFiles,¹ files with a status of ¹,$zip->status;

//close the zip -- done!
$zip->close();

//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
/***** Example Usage ***/
$files=array(¹file1.jpg¹, ¹file2.jpg¹, ¹file3.gif¹);
create_zip($files, ¹myzipfile.zip¹, true);


PHP解压缩 Zip 文件

/**********************
*@file - path to zip file
*@destination - destination directory for unzipped files
*/
function unzip_file($file, $destination){
// create object
$zip = new ZipArchive() ;
// open archive
if ($zip->open($file) !== TRUE) {
die (’Could not open archive’);
}
// extract contents to destination directory
$zip->extractTo($destination);
// close archive
$zip->close();
echo ¹Archive extracted to directory¹;
}

 

文章来源:侠客站长站(www.xkzzz.com) 详文参考:http://www.xkzzz.com/zz/netbc/php/201009/22-56339.html

PHP获取客户端真实IP地址

分类:PHP  来源:网络  时间:2011-3-12 20:20:59

该函数将获取用户的真实 IP 地址,即便他使用代理服务器。

function getRealIpAddr()
{
    if (!emptyempty($_SERVER[¹HTTP_CLIENT_IP¹]))
    {
        $ip=$_SERVER[¹HTTP_CLIENT_IP¹];
    }
    elseif (!emptyempty($_SERVER[¹HTTP_X_FORWARDED_FOR¹]))
    //to check ip is pass from proxy
    {
        $ip=$_SERVER[¹HTTP_X_FORWARDED_FOR¹];
    }
    else
    {
        $ip=$_SERVER[¹REMOTE_ADDR¹];
    }
    return $ip;
}

 

文章来源:侠客站长站(www.xkzzz.com) 详文参考:http://www.xkzzz.com/zz/netbc/php/201009/22-56345.html

PHP如何解析XML

分类:PHP  来源:网络  时间:2011-3-12 20:18:04

PHP解析XML数据

//xml string
$xml_string="<?xml version=¹1.0¹?>
<users>
<user id=¹398¹>
<name>Foo</name>
<email>foo@bar.com</name>
</user>
<user id=¹867¹>
<name>Foobar</name>
<email>foobar@foo.com</name>
</user>
</users>";

//load the xml string using simplexml
$xml = simplexml_load_string($xml_string);

//loop through the each node of user
foreach ($xml->user as $user)
{
//access attribute
echo $user[¹id¹], ¹ ¹;
//subnodes are accessed by -> operator
echo $user->name, ¹ ¹;
echo $user->email, ¹<br />¹;
}

 

文章来源:侠客站长站(www.xkzzz.com) 详文参考:http://www.xkzzz.com/zz/netbc/php/201009/22-56347.html

PHP反射机制代码示例

分类:PHP  来源:网络  时间:2011-3-12 20:16:02

演示用代码如下所示:

<?php
class ClassOne {
function callClassOne() {
print "In Class One";
}
}
class ClassOneDelegator {
private $targets;
function __construct() {
$this->target[] = new ClassOne();
}
function __call($name, $args) {
foreach ($this->target as $obj) {
$r = new ReflectionClass($obj);
if ($method = $r->getMethod($name)) {
if ($method->isPublic() && !$method->isAbstract()) {
return $method->invoke($obj, $args);
}
}
}
}
}
$obj = new ClassOneDelegator();
$obj->callClassOne();
?>


输出结果:
In Class One
可见,通过代理类ClassOneDelegator来代替ClassOne类来实现他的方法。
同样的,如下的代码也是能够运行的:

<?php
class ClassOne {
function callClassOne() {
print "In Class One";
}
}
class ClassOneDelegator {
private $targets;
function addObject($obj) {
$this->target[] = $obj;
}
function __call($name, $args) {
foreach ($this->target as $obj) {
$r = new ReflectionClass($obj);
if ($method = $r->getMethod($name)) {
if ($method->isPublic() && !$method->isAbstract()) {
return $method->invoke($obj, $args);
}
}
}
}
}
$obj = new ClassOneDelegator();
$obj->addObject(new ClassOne());
$obj->callClassOne();
?>

解决XHTML1.0与HTML兼容问题

分类:网页设计  来源:网络  时间:2011-3-12 12:50:07

在为大家介绍完如何在XHTML中正确地使用JavaScript和CSS之后,W3CGroup继续为大家带来XHTML与HTML兼容的16条指引!

1.避免将页面声明为XML类型,页面使用UTF-8或者UTF-16字符集。

2.在空元素标签(不能用来包含内容的标签)结束符>前加上斜杠 /,如:<br />,<hr />等等。

3.当一个非空元素(此标签是用来包含内容的,如标题,段落)内容为空时,给它一个空白字符,而不要使用像空元素那样的结束方法,如:当一个没有内容的P标签请书写:<p> </p>而不要写成<p />。

4.当你的style和scripts内容中出现 <, &, ]]>或者两个连续的横杠 --时,请使用外部文件进行引入。

5.避免在元素属性值中出现断行或者多个空格。

6.不要在文档的head部分包含一个以上的isindex元素(最好不使用),此元素不推荐使用。

isindex:使浏览器显示一个对话框,提示用户输入单行文本。

在 HTML 4 中,此元素是不推荐使用的,而推荐使用 INPUT 元素。isIndex 的 tagName 属性将返回 input。

此元素是一个块元素,此元素需要关闭标签。

下面的例子使用 ISINDEX 元素提换了默认的提示:
   
<isindex prompt="输入要搜索的索引关键字" />

7.当要给一个元素指定language时,请使用lang和xml:lang属性,xml:lang的值优先级更高。

8.请使用id属性当做元素标识符,避免使用name属性,尤其在这些元素上更不赞成使用name属性当做它们的标识符:a, applet, form, frame, iframe, img, map。

9.给页面定义文档字符集,给xml文档定义字符集使用
<?xml version="1.0" encoding="UTF-8"?>
给XHTML定义字符集使用
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />

 

10.Boolean类型元素属性值请使用该属性名,如:checked="checked",Boolean类型元素有:compact, nowrap, ismap, declare, noshade, checked, disabled, readonly, multiple, selected, noresize, defer

11.HTML4和XML文档对象模型指定HTML元素和属性名返回大写格式。XHTML中元素和属性名返回小写格式。

12.使用&amp;替代属性值中的&符号,如:
http://www.w3cgroup.com/default.asp?CateID=2&amp;page=2
要比下面的好:
http://www.w3cgroup.com/default.asp?CateID=2&page=2

13.在XHTML中CSS样式标签style及属性名必须使用小写;

在HTML的table中,tbody将会在解析时自动补齐,而在XML中却不行,所以,需要自己添加上tbody元素,如果在CSS选择符中使用到了它;

CSS对某个具有id属性的元素进行选择时,使用#选择符;

CSS对某个具有class属性的元素进行选择时,使用.选择符;

14.如何在解析XML文档时使用Style元素?在HTML4和XHTML中,style元素可以用在文档中定义样式规则,在XML中,XML stylesheet用来定义样式规则,为了兼容这个规则,在解析XML文档时如果需要使用style元素,style元素需要使用id属性作为标示符,并且,要有一个XML stylesheet引用它,如:

<?xml-stylesheet href="http://www.w3.org/StyleSheets/TR/W3C-REC.css" type="text/css"?>
<?xml-stylesheet href="#internalStyle" type="text/css"?>
<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>An internal stylesheet example</title>
<style type="text/css" id="internalStyle">
  code {
    color: green;
    font-family: monospace;
    font-weight: bold;
  }
</style>
</head>
<body>
<p>
  W3CGroup为大家介绍16条兼容XHTML与HTML的指引!
<code>http://www.w3cgroup.com/article.asp?id=252</code>.
</p>
</body>
</html>

15.需要注意HTML和XML中的空白字符。有些在HTML文档中合法的字符,到了XML里可能就不合法了,如,在HTML中,换页符(Formfeed character U+000C)被解析为空格,而在XHTML中,由于XML的字符定义,它变得不合法。

16注意特殊字符&apos;(省略号,U+0027)在XML1.0中有介绍,但却没有出现在HTML中,使用&#39;替换&apos;则可在HTML4中使用。

http://www.w3cgroup.com译文,转载请注明出处!
参见:
http://www.w3.org/TR/xhtml1/#guidelines

 

IE下img多5像素空白的解决方法

分类:网页设计  来源:网络  时间:2011-3-12 12:48:02

越来越觉得 li 元素中包含 a img 元素的时候会比较麻烦,需要注意,当然,问题还是一如既往的出现在 IE 下。以下为其中一例:

html

<ul>
 <li><a href="#"><img src="img/temp.jpg" alt="" /></a></li>
 <li><a href="#"><img src="img/temp.jpg" alt="" /></a></li>
 <li><a href="#"><img src="img/temp.jpg" alt="" /></a></li>
 <li><a href="#"><img src="img/temp.jpg" alt="" /></a></li>
</ul>

css

ul{
 width: 280px;
}
ul li{
 display:block;
 height:57px;
 width:277px;
}

其中 temp.jpg 尺寸为 277×57

Firefox 下的正常表现

 

IE6 下的非正常表现

 

很明显地可以看到 IE 中,li 的表现高度,并非我们设定的 57px,而是比其要高,这是因为 img 下面多出了 5px 的空白。

解决方法 一

使 li 浮动,并设置 img 为块级元素

ul{
 width: 280px;
}
ul li{
 float:left;
 display:block;
 height:57px;
 width:277px;
}
img{
 display: block;
}

解决方法 二

设置 ul 的 font-size:0;

ul{
 width: 280px;
 font-size: 0;
}
ul li{
 display:block;
 height:57px;
 width:277px;
}

解决方法 三

设置 img 的 vertical-align: bottom;

ul{
 width: 280px;
 font-size: 0;
}
ul li{
 display:block;
 height:57px;
 width:277px;
}
img{
 vertical-align:bottom;
}

解决方法 四

设置 img 的 margin-bottom: -5px;

ul{
 width: 280px;
 font-size: 0;
}
ul li{
 display:block;
 height:57px;
 width:277px;
}
img{
 margin-bottom: -5px;
}

  • 60
  • |<
  • <<
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • >>
  • >|