为单独的类文件编写方法(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
最满意答案
您可以使用头等模块。 以下是一些示例代码,显示了一种可能性。
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 ...
-
OCaml签名函数(OCaml Signature Functors)[2022-10-10]
不幸的是,据我所知,这是不可能的。 你必须这样做 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 /原因设计模块(OCaml/reason design modules)[2023-12-07]
只是为了让您感受到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 = ...
-
OCaml中的个人模块(Personal modules in OCaml)[2022-04-15]
如果您使用的是toplevel,则需要使用#mod_use而不是#use : #use 只读取当前作用域内文件名的内容,而#mod_use 使用该文件在当前范围中定义新模块 。 但是, #mod_use和#mod_use都是简单的文本指令,不能与ml和mli文件对mli 。 您将需要在某个时候阅读OCaml构建系统。 对于简单的学校项目,ocamlbuild可能是一个好主意:编译整个项目可能就像ocaml ... -
BST使用模块 - OCaml(BST using modules - OCaml)[2023-05-25]
看起来你希望模块是类,但我建议你考虑更习惯的解决方案。 你有没有考虑过使用管道操作员? 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文件提供一个良好的起点。 但是对于这样一个简单的 ...