Showing posts with label Private. Show all posts
Showing posts with label Private. Show all posts

Tuesday, November 18, 2008

Java 多态学习笔记

在面向对象的程序设计语言中,多态是继数据抽象和继承之后的第三个基本特征。
将一个方法调用同一个方法主体关联起来称作绑定,程序在运行时能从对象提供的信息中获取对象的类型,从而调用正确的方法体,所以也称为运行时绑定,后期绑定。
多态意味着“不同的形态”,就是对于继承自同一基类的不同子类对象,在相同的接口方法被调用时,会在运行时找到方法相对应的方法体。多态是跟继承协同工作的,是以继承为基础的。
在java中,除了static方法和final方法(private方法也是属于final方法)之后,其他所有的方法都是后期绑定的,通过后期绑定实现多态。这也就是说对于普通的方法调用可以是多态的,而对于静态方法和属性字段则不存在多态。
在继承时,只有public、protected和包可见的方法会被重载,对于基类中的private的方法是不会被子类覆盖的,对于子类而言,是不知道父类中是否有同名的private方法的,子类中同名的private方法就是一个完全不相关的全新方法。

Thursday, July 17, 2008

how to protect private field from reflect get and set in java


public class ReflectField {

protected String test = "test string in class ReflectField!";

}


/**
* subclass of ReflectField
*/
import java.lang.reflect.Field;

public class ReflectExtendsField extends ReflectField {

// protected String test = "test string in class ReflectExtendsField!";
private String test = "test string in class ReflectExtendsField!";

public static void main(String[] args) throws Exception {
// Class ref = Class.forName("ReflectExtendsField");
Class ref = ReflectExtendsField.class;

Field[] fields = ref.getFields();
System.out.println(fields.length);
for (Field field : fields) {
field.setAccessible(true);
System.out.println(field.get(ref));
}

Field testField = ref.getDeclaredField("test");
testField.setAccessible(true);
System.out.println(testField);
System.out.println(testField.get(ref));
}
}

It is just a hack, and usally protect private field from access using security manage.

Friday, August 10, 2007

Private Members in JavaScript

Private Members in JavaScript


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

function Container(param) {

function dec() {
// private function
if (secret > 0) {
// encloures
secret -= 1;
return true;
} else {
return false;
}
}

this.member = param;
var secret = 3; // private variables only be made in constructor function
var that = this;

this.service = function () {
// privileged function
if (dec()) {
//return that.member;
return this.member;
} else {
return null;
}
};
}

var myContainer = new Container('abc');
doc(myContainer.service()); // abc
doc(myContainer.service()); // abc
doc(myContainer.service()); // abc
doc(myContainer.service()); // null
</script>

Friday, March 23, 2007

PHP5 Visibility Public Protected Private

The visibility of a property or method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere. Protected limits access to inherited and parent classes (and to the class that defines the item). Private limits visibility only to the class that defines the item.

Members Visibility

Class members must be defined with public, private, or protected.


Example 19-10. Member declaration

<?php
/**
* Define MyClass
*/
class MyClass
{
public $public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';

function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}

$obj = new MyClass();
echo $obj->public; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private


/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
// We can redeclare the public and protected method, but not private
protected $protected = 'Protected2';

function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}

$obj2 = new MyClass2();
echo $obj->public; // Works
echo $obj2->private; // Undefined
echo $obj2->protected; // Fatal Error
$obj2->printHello(); // Shows Public, Protected2, not Private

?>

Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning.

Method Visibility

Class methods must be defined with public, private, or protected. Methods without any declaration are defined as public.

Example 19-11. Method Declaration

<?php
/**
* Define MyClass
*/
class MyClass
{
// Contructors must be public
public function __construct() { }

// Declare a public method
public function MyPublic() { }

// Declare a protected method
protected function MyProtected() { }

// Declare a private method
private function MyPrivate() { }

// This is public
function Foo()
{
$this->MyPublic();
$this->MyProtected();
$this->MyPrivate();
}
}

$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work


/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
// This is public
function Foo2()
{
$this->MyPublic();
$this->MyProtected();
$this->MyPrivate(); // Fatal Error
}
}

$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Private
?>