Showing posts with label Static. Show all posts
Showing posts with label Static. Show all posts

Tuesday, November 04, 2008

静态成员类与非静态成员类的区别

/**
* 静态成员类与非静态成员类的区别
*/
public class StaticMemberType {

// Interfaces, enumerated types, 和annotation types 无论是否声明static,它们都是static的。
static class StaticInnerClass {
public void test() {
System.out.println("Static Nested Class Method.");

}
}

public void test() {
new NonStaticInnerClass().test();
}

public class NonStaticInnerClass {
// 非静态成员类不可以包含任何static字段、methods或者类型,除非同时使用了static和final的常量字段之外。
// static String CONST1 = "TEST"; // error
final static String CONST2 = "TEST";
public void test() {
System.out.println("Non Static Inner Class.");
}
}

public static void main(String[] args) {
new StaticMemberType.StaticInnerClass().test();

StaticMemberType staticMemberType = new StaticMemberType();
staticMemberType.test();
// new StaticMemberType.NonStaticInnerClass().test(); // error
}
}

Monday, October 27, 2008

Java in a nutshell 泛型学习笔记

泛型的类型变量只可以作用于该类中的实例成员,而不可以用于静态成员。但是,跟实例方法一样,静态方法也可以使用通配符,不过无法使用所处泛型类的泛型实例变量,但可以声明自己的类型变量。当一个方法(不管是实例方法还是静态方法)只要明它自己的类型变量,这个方法就是一个泛型方法(generic method)。

import java.util.ArrayList;
import java.util.List;

public class Tree<V> {

V value;

List<Tree<? extends V>> branches = new ArrayList<Tree<? extends V>>();

public Tree(V value) {
this.value = value;
}

V getValue() {
return this.value;
}

void setValue(V value) {
this.value = value;
}

int getNumBranches() {
return this.branches.size();
}

Tree<? extends V> getBranch(int n) {
return branches.get(n);
}

void addBranch(Tree<V> branch) {
branches.add(branch);
}

// public static <N extends V> V sum(Tree<N> t) {}
// can not make a static reference to the non-static type V
// 静态方法无法使用它们所在类的类型变量,但它们可以声明自己的类型变量
// 静态成员不能使用泛型的类型变量
// 这个不是 generic method
public static double sum(Tree<? extends Number> t) {
double total = t.value.doubleValue();
for (Tree<? extends Number> b : t.branches)
total += sum(b);
return total;
}

// 当类型变量是用来表示两个参数或一个参数与一个返回值之间的关系时,就需要使用到generic method
// 在方法返回值前面先声明自己的类型变量,如下的N。
public static <N extends Number> Tree<N> max(Tree<N> t, Tree<N> u) {
double ts = sum(t);
double us = sum(u);
if (ts > us)
return t;
else
return u;
}

public static void main(String[] args) {
Tree<String> s1 = new Tree<String>("Tree");
Tree<String> s2 = new Tree<String>("Tree");
Tree<? extends Number> i1 = new Tree<Integer>(3);
Tree<? extends Number> i2 = new Tree<Integer>(3);
s1.addBranch(s2);
// i1.addBranch(i2); // compile error.
System.out.println(i1.getValue());
System.out.println(s1.getValue());
System.out.println(i1.getNumBranches());
System.out.println(s1.getNumBranches());
}
}

Saturday, July 14, 2007

static variable of Javascript


<script type="text/javascript" charset="utf-8">
function doc (argument) {
document.write(argument);
document.write("<br />\n");
}

// Define a static variable to hold the running static_var over all calls
// Can't claim static variable with key "var", this is function Object's properties
function doSome (a) {
doSome.static_var ++;
return a + doSome.static_var;
}
doc(typeof doSome); //function Object
doSome.static_var = 0; // var doSome.static_var = 0 will error.
doc(doSome(1)); //2
doc(doSome(1)); //3
doc(doSome(1)); //4
doSome.static_var = 0;
doc(doSome(1)); //2
for (i in doSome)
{
doc(i);
/*static_var
prototype
bind
bindAsEventListener*/
}
</script>

Class Properties
In addition to instance properties and properties of prototypes, JavaScript allows you to define class properties (also known as static properties), properties of the type rather than of a particular object instance. An example of a class property is Number.MAX_VALUE. This property is a type-wide constant, and therefore is more logically located in the class (constructor) rather than individual Number objects. But how are class properties implemented?
Because constructors are functions and functions are objects, you can add properties to constructors. Class properties are added this way. Though technically doing so adds an instance property to a type’s constructor, we’ll still call it a class variable. Continuing our example,

doSome.static_var = 0;

defines a class property of the doSome object by adding an instance variable to the constructor. It is important to remember that static properties exist in only one place, as members of constructors. They are therefore accessed through the constructor rather than an instance of the object.
As previously explained, static properties typically hold data or code that does not depend on the contents of any particular instance. The toLowerCase() method of the String object could not be a static method because the string it returns depends on the object on which it was invoked. On the other hand, the PI property of the Math object (Math.PI) and the parse() method of the String object (String.parse()) are perfect candidates, because they do not depend on the value of any particular instance. You can see from the way they are accessed that they are, in fact, static properties. The isMetallic property we just defined is accessed similarly, as doSome.static_var.

Sunday, March 25, 2007

References with global and static variables

The Zend Engine 1, driving PHP 4, implements the static and global modifier for variables in terms of references. For example, a true global variable imported inside a function scope with the global statement actually creates a reference to the global variable. This can lead to unexpected behaviour which the following example address.


<?php
function global_ref()
{
//在function中用global,static引入的变量其实是对此变量建立了一个引用reference,
//在function执行完之后会对应变量的值不会丢失。
global $obj;
var_dump($obj);
echo "<br/>";
$obj = &new stdclass;
}
global_ref();
global_ref();
?>

<br/>

<?php
function global_noref()
{
global $obj;
var_dump($obj);
echo "<br/>";
if(!isset($obj))
{
$obj = new stdclass;
}
else
{
echo "has set variable: \$obj. <br/>";
}
}
global_noref();
global_noref();
?>
A similar behaviour applies to the static statement. References are not stored statically:

<?php
function static_ref()
{
static $sta;
var_dump($sta);
if(!isset($sta))
{
$sta = &new stdclass;
}
$sta->num++;
}
static_ref();
static_ref();
static_ref();
?>

<?php
function static_noref()
{
static $sta;
var_dump($sta);
echo "<br>";
if(!isset($sta))
{
$sta = new stdclass;
}
$sta->num++;
}
static_noref();
static_noref();
static_noref();
?>

Using the global keyword inside a function to define a variable is essentially the same as passing the variable by reference as a parameter:

somefunction(){
global $var;
}

is the same as:

somefunction(& $a) {

}

The advantage to using the keyword is if you have a long list of variables needed by the function - you dont have to pass them every time you call the function.

Saturday, March 24, 2007

Using static variables

Another important feature of variable scoping is the static variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.
static variable只作用于本地function内,在程序执行完后此变量值不会丢失,此方法再次调用,将使用上次操作完成后的此变量值。在singleton pattern里,也是利用static variable,使程序在运行期间只产生一个实例。


<?php
function Test_static_var()
{
static $a = 0;
echo $a, "<br />";
$a++;
}
Test_static_var();
Test_static_var();
Test_static_var();
?>
<?php
function Test()
{
static $count = 0;

$count++;
echo $count;
for($i = 0; $i < $count; $i++)
{
echo " ";
}
echo " before recursive <br />\n";
if ($count < 10) {
Test();
}
$count--;
echo $count;
for($i = 0; $i < $count; $i++)
{
echo " ";
}
echo " after recusive <br />\n";
}
Test();
// 1 before recursive
// 2 before recursive
// 3 before recursive
// 4 before recursive
// 5 before recursive
// 6 before recursive
// 7 before recursive
// 8 before recursive
// 9 before recursive
// 10 before recursive
// 9 after recusive
// 8 after recusive
// 7 after recusive
// 6 after recusive
// 5 after recusive
// 4 after recusive
// 3 after recusive
// 2 after recusive
// 1 after recusive
// 0 after recusive
?>

PHP5 static property and static function


<?php
class Visibility
{
public $a = "a";
// var is synmic of public.
protected $b = "b";
private $c = "c";
const T = "const";
public static $s = "static";

public function public_func()
{
echo "public function";
echo "<br>";
}

protected function protected_func()
{
echo "protected function";
echo "<br>";
}

private function private_func()
{
echo "private function.";
echo "<br>";
}

public function static_var()
{
echo self::$s;
echo " self::\$s<br><br>";
}

public static function static_func()
{
echo "static function.";
echo "<br>";
echo "can't use \$this.";
echo "<br>";
// echo $this->a;
// Fatal error: Using $this when not in object context
}
}

$v = new Visibility();
echo $v->a;
// echo $v->b; // Fatal error
// echo $v->c; // Fatal error
echo "<br>";
$v->public_func();
// $v->protected_func(); // Fatal error
// $v->private_func(); // Fatal error
// echo Visibility::$a;
// Fatal error: Access to undeclared static property: Visibility::$a
echo Visibility::$s;
echo "<br>";
// echo Visibility::s; //Undefined class constant 's'
echo Visibility::T;
echo "<br>";
echo $v->static_var();
// echo $v->s; // output nothing.
// Static properties cannot be accessed through the object using the arrow operator ->.
$v->static_func(); // Static methods can be accessed through the object using ->.
Visibility::static_func();
// Because static methods are callable without an instance of the object created,
// the pseudo variable $this is not available inside the method declared as static.
echo "<br>==============================<br>";
?>

<?php
class ExVisibility extends Visibility
{
public static $s = "e static";
public function ex_func()
{
echo parent::$s;
echo " parent::\$s<br>";
echo self::$s;
echo " self \$s<br>";
}

}
$e = new ExVisibility();
$e->ex_func();
echo ExVisibility::$s;
echo "<br />";

?>