javaScript对象及初始面向对象 第四章

第一题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>第一题</title>
</head>
<div id="intro"></div>
<body>
<script>
    var student=new Object();
    student.name="高乐乐";
    student.age="15";
    student.introduction="我叫高乐乐,我是一个初中三年级的学<br>生,我非常喜欢音乐和打篮球";
    student.intro=function () {
        var str="姓名:"+this.name+"<br>年龄:"+this.age+"<br>自我介绍:"+this.introduction;
        document.getElementById("intro").innerHTML=str;
    }
    student.intro();
</script>
</body>
</html>

第二题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>第二题</title>
</head>
<body>
<div>
    <p>姓名:<span id="name"></span></p>
    <p>年龄:<span id="age"></span></p>
    <p>自我介绍:<span id="intro"></span></p>
    <script>
        function Person(name,age){
            this.name=name;
            this.age=age;
        }
        Person.prototype.showName=function(){
            return this.name;
        }
        Person.prototype.showAge=function () {
            return this.age;
        }
        function Student(name,age,intro){
            Person.call(this,name,age);    //继承属性
            this.intro=intro;
        }
        Student.prototype=new Person();
        Student.prototype.showIntro=function () {
            return this.intro;
        }
        var Student1=new Student("王小明",16,"我是高中一年级的学生,身高1.8米,很帅,我喜欢学习语文和英语。");
        document.getElementById("name").innerHTML=Student1.showName();
        document.getElementById("age").innerHTML=Student1.showAge();
        document.getElementById("intro").innerHTML=Student1.showIntro();
    </script>
</div><br>
<div>
    <p>姓名:<span id="name1"></span></p>
    <p>年龄:<span id="age1"></span></p>
    <p>自我介绍:<span id="intro1"></span></p>
    <script>
        function Person(name1,age1){
            this.name1=name1;
            this.age1=age1;
        }
        Person.prototype.showName1=function(){
            return this.name1;
        }
        Person.prototype.showAge1=function () {
            return this.age1;
        }
        function Student(name1,age1,intro1){
            Person.call(this,name1,age1);    //继承属性
            this.intro1=intro1;
        }
        Student.prototype=new Person();
        Student.prototype.showIntro1=function () {
            return this.intro1;
        }
        var Student1=new Student("黄妞妞",6,"我今年6岁了,非常可爱,马上就可以上小学了,就可能有好多好多的小朋友了。");
        document.getElementById("name1").innerHTML=Student1.showName1();
        document.getElementById("age1").innerHTML=Student1.showAge1();
        document.getElementById("intro1").innerHTML=Student1.showIntro1();
    </script>
</div>
</body>
</html>

第三题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>第三题</title>
</head>
<body>
<div>
    <p>我是一只<span id="color"></span>的<span id="name"></span>,我已经<span id="age"></span>岁了,我有<span id="leg"></span>条腿</p>
    <script>
        function Animal(color,name,age) {
            this.color=color;
            this.name=name;
            this.age=age;
        }
        Animal.prototype.showColor=function () {
            return this.color;
        }
        Animal.prototype.showName=function () {
            return this.name;
        }
        Animal.prototype.showAge=function () {
            return this.age;
        }
        function Poultry(name,age,color,leg) {
            Animal.call(this,name,age,color);
            this.leg=leg;
        }
        Poultry.prototype=new Animal();
        Poultry.prototype.showLeg=function () {
            return this.leg;
        }
        var Poultry1=new Poultry("灰色","小狗狗","1","4");
        document.getElementById("color").innerHTML=Poultry1.showColor();
        document.getElementById("name").innerHTML=Poultry1.showName();
        document.getElementById("age").innerHTML=Poultry1.showAge();
        document.getElementById("leg").innerHTML=Poultry1.showLeg();
    </script>
</div>
<div>
    <p>我是一只<span id="color1"></span>的<span id="name1"></span>,我已经<span id="age1"></span>岁了,我有<span id="leg1"></span>条腿</p>
    <script>
        function Animal1(color1,name1,age1) {
            this.color1=color1;
            this.name1=name1;
            this.age1=age1;
        }
        Animal1.prototype.showColor1=function () {
            return this.color1;
        }
        Animal1.prototype.showName1=function () {
            return this.name1;
        }
        Animal1.prototype.showAge1=function () {
            return this.age1;
        }
        function Poultry(name1,age1,color1,leg1) {
            Animal1.call(this,name1,age1,color1);
            this.leg1=leg1;
        }
        Poultry.prototype=new Animal1();
        Poultry.prototype.showLeg1=function () {
            return this.leg1;
        }
        var Poultry2=new Poultry("白色","茶杯猫","2","4");
        document.getElementById("color1").innerHTML=Poultry2.showColor1();
        document.getElementById("name1").innerHTML=Poultry2.showName1();
        document.getElementById("age1").innerHTML=Poultry2.showAge1();
        document.getElementById("leg1").innerHTML=Poultry2.showLeg1();
    </script>
</div>
<div>
    <p>我是一只<span id="color2"></span>的<span id="name2"></span>,我已经<span id="age2"></span>岁了,我有<span id="leg2"></span>条腿</p>
    <script>
        function Animal2(color2,name2,age2) {
            this.color2=color2;
            this.name2=name2;
            this.age2=age2;
        }
        Animal2.prototype.showColor2=function () {
            return this.color2;
        }
        Animal2.prototype.showName2=function () {
            return this.name2;
        }
        Animal2.prototype.showAge2=function () {
            return this.age2;
        }
        function Poultry(name2,age2,color2,leg2) {
            Animal2.call(this,name2,age2,color2);
            this.leg2=leg2;
        }
        Poultry.prototype=new Animal2();
        Poultry.prototype.showLeg2=function () {
            return this.leg2;
        }
        var Poultry3=new Poultry("红色","母鸡","1","2");
        document.getElementById("color2").innerHTML=Poultry3.showColor2();
        document.getElementById("name2").innerHTML=Poultry3.showName2();
        document.getElementById("age2").innerHTML=Poultry3.showAge2();
        document.getElementById("leg2").innerHTML=Poultry3.showLeg2();
    </script>
</div>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/liyanghahahhaha/article/details/81080400