javascript物件導向特徵

來源:互聯網
上載者:User

標籤:javascript

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>無標題文檔</title>
</head>
<script type="text/javascript">
document.write("js物件導向:封裝");
function Person(name,sal){
this.name=name; //公開
var sal=sal;//私人

this.showInfo=function(){ //公開
document.write("<br/>"+this.name+"--"+sal);
}
}
var per=new Person("tom",100);
per.showInfo();
document.write("<br/>js物件導向:繼承");
//1.抽取共有屬性,寫一個父類
function Stu(name,age){
this.name=name;
this.age=age;
this.show=function(){
document.write("<br/>"+this.name+"--"+this.age);
}
}
function MidStu(name,age){
//這裡相當於把Stu建構函式(類)賦值給我們的屬性this.stu
this.stu=Stu;
//這個表示初始化MidStu,相當於執行Stu(name,age),這句話必須有,否則無法實現整合的效果
this.stu(name,age);
//可以寫自己的函數
this.pay=function(fee){
document.write("<br/>學費是:"+fee*0.8);
}
}
var midstu=new MidStu("小白",15);
midstu.show();
midstu.pay(100);
document.write("<br/>js物件導向:重載");
//js通過判斷參數的個數來實現重載
function Person1(){
this.test1=function (){

if(arguments.length==1){
this.show1(arguments[0]);
}else if(arguments.length==2){
this.show2(arguments[0],arguments[1]);
}else if(arguments.length==3){
this.show3(arguments[0],arguments[1],arguments[2]);
}


}
this.show1=function(a){
document.write("show1()被調用"+a);
}


this.show2=function(a,b){
document.write("show2()被調用"+"--"+a+"--"+b);
}


function show3(a,b,c){
document.write("show3()被調用");
}
}
var p1=new Person1();
//js中不支援重載.
//p1.test1("a","b","c");
p1.test1("a","b");
p1.test1("a");
document.write("<br/>js物件導向:覆蓋");
function Fu(){
this.fu=function(){
document.write("<br/>父類的方法");
}
}
function Zi(){
this.stu=Fu;
this.stu();
this.fu=function(){
document.write("<br/>子類的方法");
}
}
var zi=new Zi();
zi.fu();
document.write("<br/>js物件導向:多態");
// Master類
function Master(name){
this.nam=name;
//方法[給動物餵食物]
}
//原型法新增成員函數
Master.prototype.feed=function (animal,food){

document.write("<br>給"+animal.name+" 喂"+ food.name);
}
function Food(name){
this.name=name;
}
//魚類
function Fish(name){
this.food=Food;
this.food(name);
}
//骨頭
function Bone(name){
this.food=Food;
this.food(name);
}
//桃子
function Peach(name){
this.food=Food;
this.food(name);
}
//動物類
function Animal(name){
this.name=name;
}
//貓貓
function Cat(name){
this.animal=Animal;
this.animal(name);
}
//狗狗
function Dog(name){
this.animal=Animal;
this.animal(name);
}
//猴子
function Monkey(name){
this.animal=Animal;
this.animal(name);
}
var cat=new Cat("大花貓");
var fish=new Fish("黃花魚");
var dog=new Dog("大花狗");
var bone=new Bone("豬骨頭");
//建立一個主人
var master=new Master("韓順平");
master.feed(dog,bone);
//擴充
var monkey=new Monkey("金絲猴");
var peach=new Peach("仙桃");
master.feed(monkey,peach);
</script>
<body>
</body>
</html>

著作權聲明:博主原創文章,轉載請說明出處。http://blog.csdn.net/dzy21

javascript物件導向特徵

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.