java中的實現建立一個抽象類
package com.yashu; public abstract class Employee { private String name; private String address; private int number; public abstract double computePay(Double pay); }
繼承抽象類
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; } }
測試程式碼 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 autojsUseDexautojs中抽象類的繼承 匯入dex, 我用的是以autojs專案方式執行程式碼, main.js和extendstest.dex在同一個資料夾let dexFilePath = files.join("./extendstest.dex"); runtime.loadDex(dexFilePath);
匯入抽象類Employee
importClass(com.yashu.Employee);
例項化Salary, 並呼叫抽象方法computePay s = new Salary(); d = s.computePay(10.1); log(d);
實現繼承抽象類的關鍵程式碼JavaAdapter, 第一個引數是抽象類, 第二個引數是抽象類方法的具體實現
function Salary() { let salary = new JavaAdapter(Employee, { computePay: function (pay) { log("Computing salary pay for " + "Lei Li"); return pay * 2; }, }); return salary; }
最新評論