猿问

如何使用整数参数(路径占位符)定义方法?

前言

我想说两点:

  1. 我不知道如何用几句话来表达这个问题。所以我在搜索时找不到我要找的东西(在stackoverflow上)。本质上,如果这是重复的,我很抱歉。

  2. 我只用了一个月左右的时间一直在编写 Java。因此,如果我问了一个明显的问题,我深表歉意。

问题

我想有一个方法,其参数包含(路径)一个integer

这种方法是如何在 Java 代码中实现的?

限制

参数应该是通用的。因此,当有多个整数变量时,正确的一个可以用作方法的参数,当它被调用时(在运行时)

我的想法是伪代码

这是我想要的想法(在伪代码中)。这个想法基本上由3部分组成:

  • 带参数的方法

  • 持有整数值的变量

  • 具有具体值的方法调用

(一个方法

. 以下是我的方法的定义,该方法使用名为typehey泛型参数命名:

pathToAnyIntegergenericPathToInt


class main {

    method hey(genericPathToInt pathToAnyInteger) {

        System.out.println(pathToAnyInteger);

    }

}

(B) 多个整数变量

以下是多个 整数变量(例如A和B; 每个都持有一个整数):


class A {

    myInt = 2;

}


class B {

    myInt = 8;

}

(C) 运行时的方法调用

以下是我在程序运行时执行的主要方法。hey因此,在运行时,使用 (2) 每个持有不同整数值的变量调用 (1) 先前定义的方法:


class declare {

    main() {

        hey("hey " + A.myInt);

        hey("hey " + B.myInt);

   }

}

预期产出

//output

hey 2

hey 8

个人言论

再次,如果这是重复的,对不起,如果这是一个愚蠢的问题,对不起。如果您需要进一步澄清,我愿意提供帮助。任何帮助表示赞赏。嘿,如果你在回答中不友善(主要是侮辱,但也暗示语气),即使你有解决方案,也不要回答。不需要你的帮助。谢谢!:)


白衣染霜花
浏览 125回答 3
3回答

qq_遁去的一_1

Java 中的整数是原始类型,按值传递。所以你并没有真正将“路径”传递给整数,而是传递实际值。另一方面,对象是通过引用传递的。您的伪代码只需稍作修改即可在 Java 中运行。该代码假定所有类都在同一个包中,否则您需要将所有内容公开(或其他访问修饰符,具体取决于用例)。// First letter of a class name should be uppercaseclass MainClass {    // the method takes one parameter of type integer, who we will call inputInteger     // (method-scoped only)    static void hey(int inputInteger) {        System.out.println("hey " + inputInteger);    }}class A {    // instance variable    int myInt = 2;}class B {    // instance variable    int myInt = 8;}class Declare {    public static void main() {        // Instantiate instances of A and B classes        A aObject = new A();        B bObject = new B();        // call the static method        MainClass.hey(aObject.myInt);        MainClass.hey(bObject.myInt);    }}//outputhey 2hey 8此代码首先定义了 MainClass 类,其中包含您的方法hey。我将方法设为静态,以便能够将其称为MainClass.hey(). 如果它不是静态的,则需要在 Declare 类中实例化 MainClass 对象,然后调用该对象的方法。例如:...MainClass mainClassObject = new MainClass();mainClassObject.hey(aObject.myInt);...

哔哔one

Java(从 Java 8 开始)包含函数式编程的元素,它允许与您正在寻找的东西类似的东西。您的hey方法可能如下所示:void hey(Supplier<Integer> integerSupplier) {&nbsp; &nbsp; &nbsp;System.out.printl("Hey" + integerSupplier.get());}此方法声明了一个参数,该参数可以是“将返回整数的方法调用”。您可以调用此方法并将其传递给所谓的 lambda 表达式,如下所示:hey(() -> myObject.getInt());或者,在某些情况下,您可以使用所谓的方法引用,例如:Hey(myObject::getInt)在这种情况下,两者都意味着“调用 hey 方法,当它需要一个整数时,调用 getInt 来检索它”。lambda 表达式还允许您直接引用字段,但公开字段被认为是一种不好的做法。

九州编程

如果我正确理解了您的问题,您需要使用继承来实现您正在寻找的东西。让我们从创建层次结构开始:class SuperInteger {&nbsp; &nbsp;int val;&nbsp; &nbsp;//additional attributes that you would need.&nbsp; &nbsp;public SuperInteger(int val) {&nbsp; &nbsp; &nbsp; this.val = val;&nbsp; &nbsp;}&nbsp; &nbsp;public void printValue() {&nbsp; &nbsp; &nbsp; System.out.println("The Value is :"+this.value);&nbsp; &nbsp;}}&nbsp;&nbsp;class SubIntA extends SuperInteger {&nbsp; &nbsp; //this inherits "val" and you can add additional unique attributes/behavior to it&nbsp;&nbsp; &nbsp;public SubIntA(int val) {&nbsp; &nbsp; &nbsp; super(val);&nbsp; &nbsp;}&nbsp; &nbsp;@override&nbsp; &nbsp;public void printValue() {&nbsp; &nbsp; &nbsp; System.out.println("A Value is :"+this.value);&nbsp; &nbsp;}}class SubIntB extends SuperInteger {&nbsp; &nbsp; //this inherits "val" and you can add additional unique attributes/behavior to it&nbsp;&nbsp; &nbsp;public SubIntB(int val) {&nbsp; &nbsp; &nbsp; super(val);&nbsp; &nbsp;}&nbsp; &nbsp;@override&nbsp; &nbsp;public void printValue() {&nbsp; &nbsp; &nbsp; System.out.println("B Value is :"+this.value);&nbsp; &nbsp;}}现在,您的方法 Signature 可以接受 SuperInteger 类型的参数,并且在调用该方法时,您可以传递 SubIntA/SuperInteger/SubIntB,因为 Java 为您隐式向上转换。所以:public void testMethod(SuperInteger abc) {&nbsp; &nbsp; &nbsp;a.val = 3;&nbsp; &nbsp; &nbsp;a.printValue();}可以从 main 调用:public static void main(String args[]){&nbsp; &nbsp;testMethod(new SubIntA(0));&nbsp; &nbsp;testMethod(new SubIntB(1));&nbsp; &nbsp;testMethod(new SuperInteger(2));}获得如下输出:A Value is :3B Value is :3The Value is :3
随时随地看视频慕课网APP

相关分类

Java
我要回答