autojs实现抽象类的继承

autojs实现抽象类的继承

作者: 牙叔

使用情景

在java中, 抽象类必须继承才能使用, 那么在autojs中怎样实现继承抽象类呢?

java中的实现

  1. 创建一个抽象类

    package com.yashu;
    
    public abstract class Employee
    {
          
          
        private String name;
        private String address;
        private int number;
    
        public abstract double computePay(Double pay);
    }
    
  2. 继承抽象类

    package com.yashu;
    
    public class Salary extends Employee
    {
          
          
        private double salary; // Annual salary
    
        public double computePay(Double pay)
        {
          
          
            System.out.println("Computing salary pay for " + "Lei Li");
            return pay * 2;
        }
    }
    
  3. 测试代码

    package com.yashu;
    
    public class ExtendsTest {
          
          
        public static void main(String[] args) {
          
          
            System.out.println("this is com.yashu.ExtendsTest");
            Salary s = new Salary();
            Double d = s.computePay(10.10);
            System.out.println(d);
        }
    }
    
    

上面没问题, 我测试过了, 我的测试环境,

生成dex

  • 软件: idea
  • 方法参照: 百度搜索github autojsUseDex

autojs中抽象类的继承

  1. 导入dex, 我用的是以autojs项目方式运行代码, main.jsextendstest.dex在同一个文件夹

    let dexFilePath = files.join("./extendstest.dex");
    runtime.loadDex(dexFilePath);
    
  2. 导入抽象类Employee

    importClass(com.yashu.Employee);
    
  3. 实例化Salary, 并调用抽象方法computePay

    s = new Salary();
    d = s.computePay(10.1);
    log(d);
    
  4. 实现继承抽象类的关键代码JavaAdapter, 第一个参数是抽象类, 第二个参数是抽象类方法的具体实现

    function Salary() {
      let salary = new JavaAdapter(Employee, {
        computePay: function (pay) {
          log("Computing salary pay for " + "Lei Li");
          return pay * 2;
        },
      });
      return salary;
    }
    

QQ群

747748653
AutoJsPro

微信

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/snailuncle2/article/details/113613466