Showing posts with label Extends. Show all posts
Showing posts with label Extends. Show all posts

Tuesday, November 18, 2008

Java 多态学习笔记

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

Sunday, August 05, 2007

prototype javascript library String and Object extend example


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

Object.extend(String, {
win: doc('this is object of Window ? ' + (this instanceof Window)), // true
size: function (str) {
return arguments.callee.print(str);
}
});

String.size.print = function (arg) {
//doc('String.size.print() = ' + arg.length);
return arg.length;
};

var s = "string size";

doc('String.size = ' + String.size(s));
doc(String.size.print(s));

var wStr = $w('a b c d e f g');
doc(wStr.inspect());
</script>

prototype javascript library Object extend example


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

Object.extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}

var o = {
test: 'test it',
sex: 'man',
info: function () {
return this.test + " and " + this.sex;
},
ivk: function () {
return this.info.apply(this); // invoke this.info function
}
}

for(var p in Object) {
doc('Object["' + p + '"] = ' + Object[p]);
}

doc("=============================================================");

Object.extend(Object, o);

for(var p in Object) {
doc('Object["' + p + '"] = ' + Object[p]);
}

alert(Object.ivk());

</script>

prototype javascript library Number extends example


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

doc((255).toColorPart());
doc((3).succ());
(5).times(function (i) {
doc(i);
})
</script>

prototype javascript library Form extend example


<form action="form_submit" id="f1" method="post" accept-charset="utf-8">
<p><input type="text" name="test1" value="test1" id="test1"></p>
<p><input type="text" name="test2" value="test2" id="test2"></p>
<p>
<select id="s1" name="s1">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</p>
<p><input type="submit" value="submit"></p>
</form>
<script type="text/javascript" charset="utf-8">
function doc(argument) {
document.write("<p>\n");
document.write(argument);
document.write("</p>\n");
}

//Form.disable('f1');
Form.focusFirstElement('f1');
doc($('s1').type);// select-one
</script>

prototype javascript library Element extend example


<div id="d1">replace</div>
<div id="d11" class="c1 c2 c3">x</div>
<div id="d10">xx</div>
<div id="d20">y</div>
<div id="d3">test</div>
<form action="index" method="post" id="f1" accept-charset="utf-8" style="background: #ddd; border: #ccc double 1px;">
<input type="text" name="input1" value="origin is enabled" id="input1">element.disable()<br/>
<input type="text" name="input2" value="origin is disabled" id="input2" disabled="true">element.enable()<br/>
<input type="text" name="input3" value="test activate()" id="input3">element.activate()<br/>
<input type="text" name="input4" value="test clear" id="input4">element.clear()<br/>
<input type="text" name="input5" value="test present(id input5)" id="input5">element.present()<br/>
<input type="text" name="input6" value="test serialize()" id="input6">element.serialize()<br/>
<input type="text" name="input7" value="test focus()" id="input7">element.focus()<br/>
<input type="text" name="input8" value="test select()" id="input8">element.select()<br/>
<input type="submit" value="Continue →"><br/>
</form>
<div id="d4">test Element scrollTo()</div>
<div id="d5">test element scrollTo()</div>
<div id="image">
<%= image_tag 'rails' %>
</div>
<script type="text/javascript" charset="utf-8">
function doc(argument) {
document.write("<p>\n");
document.write(argument);
document.write("</p>\n");
}
$('input1').disable();
$('input2').enable();
//$('input3').activate();
$('input4').clear();
doc('$("input5").present() = ' + $('input5').present());
doc('$("input6").serialize() is: ' + $('input6').serialize());
//$('input7').focus();
$('input8').select();

doc('form action is: ' + $('f1').readAttribute('action'));
doc('style of form: ' + $('f1').readAttribute('style'));
doc('getStyle background of form: ' + $('f1').getStyle('background'));

var cn = new Element.ClassNames('d11');
cn.add('c4');
cn.remove('c2');

Element.update('d10', 'test<strong> Element.update(element, "HTML") </strong>');
$('d20').update('test<strong> element.update("HTML") </strong>');

Element.replace('d1', '<div id="d2"><span>replaced div</span></div>');
doc(Element.inspect('d2'));
doc(Element.ancestors('d2').inspect().escapeHTML());
doc(Element.descendants('d2').inspect().escapeHTML());
doc(Element.previousSiblings('d3').inspect().escapeHTML());
doc(Element.nextSiblings('d3').inspect().escapeHTML());

Element.setOpacity('image', 0.6);
doc($('image').getOpacity());

$H(Element.Methods).each (function (m) {
doc(m);
});

doc($H(Element.getDimensions('d4')).inspect().escapeHTML());
// Element.scrollTo('d4');
$('f1').scrollTo();
doc("$('d5').empty() is: " + $('d5').empty());

var timer1 = setTimeout(function () {
$('d5').replace('<div id="d5">replace timer1</div>');
}, 5000);
var timer2 = setTimeout(function () {
$('d5').replace('<div id="d5">timeout d5</div>');
}, 15000);

$('f1').request({onComplete: function (xhr) {
alert(xhr.responseText);
}});
</script>

prototype javascript library Class example


<script type="text/javascript" charset="utf-8">
function doc (argument) {
document.write(argument);
document.write("<br />\n");
}
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}

var MyClass = Class.create();
MyClass.prototype = {
initialize: function (msg) {
this.msg = msg;
return msg;
},

showMsg: function () {
doc(this.msg);
}
}

var c = new MyClass('test'); // Class.create('test')
c.showMsg();

var o = {
name: 'test it',
sex: 'man',
info: function () {
return this.name + " and " + this.sex;
},
ivk: function () {
return this.info.apply(this); // invoke this.info function
}
}

doc(o.info());
doc(o.ivk());

/*function TestThis () {
doc(this); // object Window
this.name = 'name test';
this.sex = 'name man';
}
var t = TestThis();
doc(name);
doc(self.name);
doc(this.sex);
for (var p in this) {
doc(this['p']);
}*/
</script>

The Global object holds the global properties and methods listed. These properties and methods do not need to be referenced or invoked through any other object. Any variables and functions you define in your own top-level code become properties of the Global object. The Global object has no name, but you can refer to it in top-level code (i.e. outside of methods) with the this keyword. In client-side JavaScript, the Window object serves as the Global object. It has quite a few additional properties and methods, and can be referred to as window.

Friday, August 03, 2007

prototype javascript library Function extends example


<script type="text/javascript" src="prototype.js" charset="utf-8"></script>

<input type="checkbox" id="myChk" value="1"/> Test Function.prototype.bindAsEventListener

<input type="button" id="myBut1" value="bind element test" onclick="doc.bind(this)('arg1', 'arg2');"/> Test Function.prototype.bind

<input type="button" id="myBut2" value="bind window test" onclick="doc.bind(self)('arg1');"/> Test Function.prototype.bind

<script type="text/javascript" charset="utf-8">
function doc(){
// this 根据调用它的对象不同而不同,可以是element对象, 也可以是当前window对象
alert([this].concat($A(arguments)).inspect());
}

//declaring the class
var CheckboxWatcher = Class.create();
//defining the rest of the class implementation
CheckboxWatcher.prototype = {
initialize: function(chkBox, message) {
this.chkBox = $(chkBox);
this.message = message;
//this.chkBox.onclick
// = this.showMessage.bindAsEventListener(this, ' other extraInfo')
// => function(event) { return __method.apply(object, [event || window.event].concat(args));}

//将showMessage方法做为this object的方法来调用并返回showMessage方法的结果
//既然是将showMessage做为this object的方法来调用,那showMessage中的this是指向当前调用它的object(即为CheckboxWatcher的实例对象,如watcher)。
//this.chkBox.onclick = this.showMessage.apply(this, ['none event', ' other extraInfo']);
//assigning our method to the event
this.chkBox.onclick = this.showMessage.bindAsEventListener(this, ' other extraInfo');
},

showMessage: function(evt, extraInfo) {
//this是指向watcher这个实例对象的
//showMessage这个方法一般是不会从外部调用的
alert(this.message + ' (' + evt + ')' + extraInfo);
},

bindExample: function() {
alert(this.message + "\n" + $A(arguments).inspect);
}
};

var watcher = new CheckboxWatcher('myChk', 'Changed');
</script>