Chinaunix

标题: PHP 和 Ruby 的基本常量变量、类的简单书写 [打印本页]

作者: 三里屯摇滚    时间: 2011-12-18 17:27
标题: PHP 和 Ruby 的基本常量变量、类的简单书写
PHP 和 Ruby 的基本常量变量、类的简单书写











PHP 变量、常量
变量:$var = "1000";
常量:define("ROOT","/tmp");
类常量:const aaa = 10;
类变量:同样还是$xxx;
全局变量: global $xxx; $GLOBALS['xxxx']

Ruby 变量、常量
变量:aa = 10 或者 _a = 10 小写字母或者下划线开头
常量:Ma = 10
实例变量:@hello = 10
类变量:@@t = 20
全局变量:$aa = 10

PHP基本的类实例化
Php代码
  1. <?php   
  2. class demo {   
  3.     function __construct($name) {   
  4.         $this -> name = $name;   
  5.     }   
  6.     function say() {   
  7.         echo $this -> name;   
  8.     }   
  9.     function __destruct() {   
  10.     }   
  11. }   
  12. $p = new demo("yang");   
  13. $p -> say();  

  14. <?php
  15. class demo {
  16.     function __construct($name) {
  17.         $this -> name = $name;
  18.     }
  19.     function say() {
  20.         echo $this -> name;
  21.     }
  22.     function __destruct() {
  23.     }
  24. }
  25. $p = new demo("yang");
  26. $p -> say();
复制代码
Ruby基本的类实例化
Ruby代码
  1. class Hello   
  2.     def initialize( name )   
  3.         @name = name   
  4.     end  
  5.     # php function __construct   
  6.   
  7.     def hello_rb   
  8.         puts "hello"+@name  
  9.     end  
复制代码
end
  1. hi = Hello.new("phper.yang")   
  2. hi.hello_rb  

  3. class Hello
  4.     def initialize( name )
  5.         @name = name
  6.     end
  7.     # php function __construct

  8.     def hello_rb
  9.         puts "hello"+@name
  10.     end
复制代码
end
  1. hi = Hello.new("phper.yang")
  2. hi.hello_rb
复制代码
PHP类的简单继承
Php代码
  1. class my {   
  2.     function say() {   
  3.         echo "hello ";   
  4.     }   
  5. }   
  6.   
  7. class hhy extends my {   
  8.     function yang() {   
  9.         echo "yang";   
  10.     }   
  11. }   
  12. $p = new hhy();   
  13. $p -> say();   
  14. $p -> yang();  

  15. class my {
  16.     function say() {
  17.         echo "hello ";
  18.     }
  19. }

  20. class hhy extends my {
  21.     function yang() {
  22.         echo "yang";
  23.     }
  24. }
  25. $p = new hhy();
  26. $p -> say();
  27. $p -> yang();
复制代码
Ruby类的简单继承
Ruby代码
  1. class Hello   
  2.   def t1   
  3.      yang = "hello ruby"  
  4.      puts yang   
  5.   end  
  6. end  
复制代码
  1. class Newhello < Hello   
  2.    def t2   
  3.       yphp = "hello php"  
  4.       puts yphp   
  5.    end  
  6. end  
  7.   
  8. p = Newhello.new  
  9. p.t1   
  10. p.t2  

  11. class Hello
  12.   def t1
  13.      yang = "hello ruby"
  14.      puts yang
  15.   end
  16. end

  17. class Newhello < Hello
  18.    def t2
  19.       yphp = "hello php"
  20.       puts yphp
  21.    end
  22. end

  23. p = Newhello.new
  24. p.t1
  25. p.t2
复制代码
ruby 的类名首字母必须大写
作者: 梦境照进现实    时间: 2011-12-19 11:25
学习了..谢谢分享了




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2