实现继承的几种方式?

13 篇文章 0 订阅

构造函数继承

原理:利用call,apply,bind等方法,在构造函数内部去调用别的构造函数,为己所用.
缺点:原型对象的函数无法复用

        function People(name,age) {
            this.name = name;
            this.age = age;
        } 
        People.prototype.action = function () {
            console.log("复习");
        }

        function Student(name,age) {
            People.call(this,name,age);
        }

        var student1 = new Student("mark",21);
        console.log(student1);

原型继承

优点:实现了原型对象内的函数复用.
缺点:虽然得到了属性值,但无法在实例对象内灵活的传参,根本不能用.

        function People(name,age) {
            this.name = name;
            this.age = age;
        } 
        People.prototype.action = function () {
            console.log("复习");
        }
        function Student(name,age,banji) {
            this.banji = banji;
        }
        // People的实例对象一定可以使用People的所有功能,
        //new People中需要传参。
        // 沿着原型链向上寻找属性值和方法
        Student.prototype = new People("jack",25);
        Student.prototype.constructor = Student;
        Student.prototype.trouble = function () {
            console.log("考试");
        }
        var student1 = new Student("mark",20,"2班");
        console.log(student1.name, student1.age);//jack,25
        student1.action();//复习
        student1.trouble();//考试

组合继承

原理:构造函数继承与原型链继承的组合使用.
优势:既实现了原型对象内的函数复用,又可以在实例对象内灵活的传参.

        function People(name,age) {
            this.name = name;
            this.age = age;
        } 
        People.prototype.action = function () {
            console.log("复习");
        }
        function Student(name,age) {
            People.call(this,name,age);
        }
        // 由于使用借用构造函数继承的方式给Student设置了自身的属性name和age,
        // 所以new People中的参数不需要传入。
        Student.prototype = new People();
        // 由于使用了覆盖方式设置原型,需要补全constructor属性
        Student.prototype.constructor = Student;
        // 注意:单独设置的功能需要在继承操作后设置。
        Student.prototype.trouble = function () {
            console.log("考试");
        }
        var student1 = new Student("mark",20);
        console.log(student1.name);//mark
        student1.action();//复习
        student1.trouble();//考试
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Python 中创建线程有以下几种方式: 1. 使用 `threading` 模块: 使用这种方式需要创建一个 `Thread` 类的实例, 然后调用它的 `start()` 方法来启动新线程。 2. 使用 `_thread` 模块: 这种方式与使用 `threading` 模块类似, 也需要调用 `start_new_thread()` 函数来启动新线程。 3. 使用多进程: Python 中的 `multiprocessing` 模块可以轻松地创建新的进程。 4. 使用其他第三方库: 例如 `gevent` 和 `concurrent.futures` 等库也可以用来创建线程。 ### 回答2: 在Java中,创建线程有以下几种方式: 1. 继承Thread类:继承Thread类并重写其run方法,然后实例化该类的对象,调用start方法启动线程。示例代码如下: ``` public class MyThread extends Thread { @Override public void run() { // 线程要执行的代码 } } // 创建并启动线程 MyThread thread = new MyThread(); thread.start(); ``` 2. 实现Runnable接口:实现Runnable接口,并实现其run方法,然后创建Thread对象并将Runnable实例作为参数传递给Thread对象,最后调用start方法启动线程。示例代码如下: ``` public class MyRunnable implements Runnable { @Override public void run() { // 线程要执行的代码 } } // 创建并启动线程 MyRunnable runnable = new MyRunnable(); Thread thread = new Thread(runnable); thread.start(); ``` 3. 使用Callable和Future:使用Callable接口代替Runnable接口,重写call方法,然后创建ExecutorService对象,通过submit方法提交Callable对象,最后得到Future对象并通过get方法获取结果。示例代码如下: ``` public class MyCallable implements Callable<Integer> { @Override public Integer call() throws Exception { // 线程要执行的代码 return result; } } // 创建线程池并提交Callable对象 ExecutorService executor = Executors.newFixedThreadPool(1); MyCallable callable = new MyCallable(); Future<Integer> future = executor.submit(callable); // 获取线程结果 Integer result = future.get(); // 关闭线程池 executor.shutdown(); ``` 通过以上几种方式,可以灵活地创建和启动线程,并执行相应的任务。 ### 回答3: 创建线程的方式有以下几种: 1. 使用继承Thread类:创建一个新的类,继承自Thread类,并重写run()方法,在run()方法中定义线程要执行的任务。然后实例化这个新类的对象,并调用对象的start()方法来启动线程。 2. 实现Runnable接口:创建一个新的类,实现Runnable接口,并实现其中的run()方法。然后实例化这个新类的对象,并将对象作为参数传递给Thread类的构造方法创建Thread对象。最后调用Thread对象的start()方法启动线程。 3. 使用Callable和Future接口:创建一个新的类,实现Callable接口,并实现其中的call()方法。然后利用ExecutorService线程池的submit()方法提交Callable对象,得到一个Future对象,通过Future对象的get()方法获取线程的返回值。 4. 使用线程池:通过ExecutorService线程池来管理线程的创建和执行。可以使用ThreadPoolExecutor类来自定义线程池的大小和其他相关参数。 这些方式可以根据实际需求选择不同的方法来创建线程。使用继承Thread类或实现Runnable接口比较简单,适合简单的线程任务。使用Callable和Future接口可以获取线程的返回值。使用线程池可以有效管理线程的创建和执行,提高系统资源的利用率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值