zoukankan      html  css  js  c++  java
  • 原型式继承

    原型式继承:对象的构造函数可以从其他对象中继承方法,他创建出一个原型对象后,所有其他的新对象都可以基于这个原型对象来构建。

    这种继承方式难于掌握,主要是由于原型本身并不会从其他原型或构造函数中继承属性,属性都是从实际对象继承来的。

     <script language="javascript" type="text/javascript">
            //为Person对象创建构造函数
            function Person(name) {
                this.name = name;
            }
            //给Person对象添加方法
            Person.prototype.getName = function() {
                return this.name;
            };
    
            //一个新的对象
            function Student(name, age) {
                this.name = name;
                this.age = age;
            }
    
            Student.prototype = new Person(); //Student对象继承所有Person对象的方法!
            Student.prototype.getAge = function() {
                return this.age;
            }
            var rxm = new Student("ruxiaomeng", 27);
            alert(rxm.getName());//从父类继承的方法,
            alert(rxm.getAge());//子类自己的方法。
        </script>
  • 相关阅读:
    使用IOCP完成端口队列做任务队列
    对Zlib单元进行再封装
    XmlReader/XmlWriter 类
    TextReader/TextWriter 的类
    LINQ to XML
    Xml序列化
    动态Linq(结合反射)
    设计模式总结
    深入了解.Net上下文
    应用程序域
  • 原文地址:https://www.cnblogs.com/hometown/p/3230644.html
Copyright © 2011-2022 走看看