首页 \ 问答 \ 为单独的类文件编写方法(writing a method for a seperate class file)

为单独的类文件编写方法(writing a method for a seperate class file)

我有一个名为UBT.class的.class文件(我无法访问源代码)。 我需要从UBT.class文件中检索数据。 我可以从UBT类(不使用TreeNode类中的方法)访问诸如.getRoot()和.getLeft()&。getRight()之类的方法。

我尝试使用像这样的递归来编写一个inOrder遍历方法,但它给出了我下面的错误,虽然我指定它是UBT而不是TreeNode

错误:不兼容的类型:TreeNode无法转换为UBT

//From main method

     public static void inOrder(UBT root)
      {
        if(root.getRoot() != null)
        {
          inOrder(root.getRoot().getLeft());
          System.out.println(root.getRoot().getData() + " ");
          inOrder(root.getRoot().getRight());
        }
      }

class TreeNode
{
  private int data;
  private TreeNode left, right;

  public TreeNode(int data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }

  public int getData() {
    return data;
  }

  public void setData(int newData){
    this.data = newData;
  }

  public TreeNode getLeft() {
    return left;
  }

  public TreeNode getRight() {
    return right;
  }

  public void setLeft(TreeNode left) {
    this.left = left;
  }

  public void setRight(TreeNode right) {
    this.right = right;
  }
}

class BST // Typical BST implementation

I have a .class file named UBT.class (which i do not have access to the source code). I need to retrieve data from the UBT.class file. I have access to methods such as .getRoot() & .getLeft() & .getRight() from the UBT class (Not using the methods from the TreeNode class).

I tried writing an inOrder traversal method using recursion like this but its giving me errors like below although i specifiy it to be UBT not TreeNode

Error: incompatible types: TreeNode cannot be converted to UBT

//From main method

     public static void inOrder(UBT root)
      {
        if(root.getRoot() != null)
        {
          inOrder(root.getRoot().getLeft());
          System.out.println(root.getRoot().getData() + " ");
          inOrder(root.getRoot().getRight());
        }
      }

class TreeNode
{
  private int data;
  private TreeNode left, right;

  public TreeNode(int data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }

  public int getData() {
    return data;
  }

  public void setData(int newData){
    this.data = newData;
  }

  public TreeNode getLeft() {
    return left;
  }

  public TreeNode getRight() {
    return right;
  }

  public void setLeft(TreeNode left) {
    this.left = left;
  }

  public void setRight(TreeNode right) {
    this.right = right;
  }
}

class BST // Typical BST implementation

原文:https://stackoverflow.com/questions/35531533
更新时间:2023-10-20 22:10

最满意答案

您可以使用头等模块。 以下是一些示例代码,显示了一种可能性。

module type Sampler = sig
    type t
    val create : unit -> t
    val process_file : t -> string -> unit
end
module HashSampler : Sampler = struct
    type t = unit
    let create () = ()
    let process_file () file = ()
end
module MapSampler : Sampler = struct
    type t = unit
    let create () = ()
    let process_file () file = ()
end

let choose_sampler : string -> (module Sampler) = function
    | "Hashtable" -> (module HashSampler)
    | "Map" -> (module MapSampler)

let process representation file =
    let (module M) = choose_sampler representation in
    let matrix = M.create () in M.process_file matrix file

You can use first class modules. Here's some example code that shows one possibility.

module type Sampler = sig
    type t
    val create : unit -> t
    val process_file : t -> string -> unit
end
module HashSampler : Sampler = struct
    type t = unit
    let create () = ()
    let process_file () file = ()
end
module MapSampler : Sampler = struct
    type t = unit
    let create () = ()
    let process_file () file = ()
end

let choose_sampler : string -> (module Sampler) = function
    | "Hashtable" -> (module HashSampler)
    | "Map" -> (module MapSampler)

let process representation file =
    let (module M) = choose_sampler representation in
    let matrix = M.create () in M.process_file matrix file

相关问答

更多
  • 您可能会定义一个模块类型来抽象所有实现。 例如 module type DB = sig type t type results val execute : t -> string -> results ... end 然后,您将编写代码以将此模块类型的实现作为参数: module MyProg (D : DB) = struct let run db = let r = D.execute db "SELECT ..." in ... ...
  • 您可以使用头等模块。 以下是一些示例代码,显示了一种可能性。 module type Sampler = sig type t val create : unit -> t val process_file : t -> string -> unit end module HashSampler : Sampler = struct type t = unit let create () = () let process_file () file = () e ...
  • 不幸的是,据我所知,这是不可能的。 你必须这样做 module type Y = functor (A : ModuleA) -> sig include I with type t := A.t val blah : A.t -> int end 希望其他人可以详细说明为什么您尝试使用的功能未实现。 可能有一个很好的理由。 编辑: 如果您已经拥有X类型的模块XX (一个实例),那么您可以这样做 module type Y = functor (A : ModuleA) -> ...
  • 只是为了让您感受到OCaml的味道,直接(句法)翻译将是: let tea_type = GraphQL.Object.{ name = "Tea"; fields = fun () -> QraphQL.Field.[{ name = GraphQL.Type.{name : GraphQL.string } steeping_time = GraphQL.Type.{name : QraphQL.int } }] ...
  • 我看到两个机会:直接从OCaml标准库中删除模块,或者通过使用具有不同(可能为空)签名的模块重载来隐藏它们。 第一个变体需要编辑OCaml分发Makefile。 实际上,使用opam并不是那么可怕,因为您可以非常轻松地修补OCaml并将每个修补的OCaml作为单独的编译器进行分发。 要从stdlib存档中删除模块,您需要编辑stdlib/Makefile.shared , stdlib/StdlibModules和stdlib.mllib 。 删除不必要的模块后,您可以执行以下操作: ./configure ...
  • 如果您正在运行顶层,则需要在其路径中使用exstring.cmo (简单的方法是在与extstring.cmo相同的目录中运行ocaml )。 然后,你可以这样做: # #load "extstring.cmo";; # Extstring.split "a.b" '.' 2;; [..] If you are running the toplevel, you need to have exstring.cmo in its path (the simple way is to run ocaml in ...
  • 你准备做什么? 您希望通过广告类型 (例如Ads.Tt )或广告模块 (例如Ads.T )对您的功能进行参数化吗? 在这两种情况下,您都应该将这些泛型函数包装在模块中: module Generic (Ad : Ads.T) : sig val search : int -> Ad.t -> int list end = struct let search _ _ = assert false end 然后,您可以轻松地实例化它们,例如。 使用Conrete_ads模块: module AT = ...
  • 如果您使用的是toplevel,则需要使用#mod_use 而不是#use : #use 只读取当前作用域内文件名的内容,而#mod_use 使用该文件在当前范围中定义新模块 。 但是, #mod_use和#mod_use都是简单的文本指令,不能与ml和mli文件对mli 。 您将需要在某个时候阅读OCaml构建系统。 对于简单的学校项目,ocamlbuild可能是一个好主意:编译整个项目可能就像ocaml ...
  • 看起来你希望模块是类,但我建议你考虑更习惯的解决方案。 你有没有考虑过使用管道操作员? T.create() |> T.push(2) |> T.push(3) |> T.push(5) |> T.postorder;; 或者使用本地开放(如果您的模块名称比T更长,那么更有意义)您甚至可以这样做 T.( create() |> push(2) |> push(3) |> push(5) |> postorder ); 你所要求的将需要引入全局可变状态,这不仅仅是“一些改变”,而是一个 ...
  • OCaml在每个源文件的顶层为您提供免费模块。 因此,您的第一个模块实际上名为Testmoda.Testmoda ,该函数名为Testmoda.Testmoda.greeter ,依此类推。 如果您的文件只包含函数定义,那么事情会更好。 作为ocamlc -i ,如果你要使用ocamlc -i生成的接口,你真的不需要mli文件。 没有mli文件的接口与ocamlc -i生成的ocamlc -i相同。 如果您不想使用默认界面,使用ocamlc -i可为您的mli文件提供一个良好的起点。 但是对于这样一个简单的 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)