为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

白衣染霜花 2022-07-27 20:28:24
前言我想说两点:我不知道如何用几句话来表达这个问题。所以我在搜索时找不到我要找的东西(在stackoverflow上)。本质上,如果这是重复的,我很抱歉。我只用了一个月左右的时间一直在编写 Java。因此,如果我问了一个明显的问题,我深表歉意。问题我想有一个方法,其参数包含(路径)一个integer。这种方法是如何在 Java 代码中实现的?限制该参数应该是通用的。因此,当有多个整数变量时,正确的一个可以用作方法的参数,当它被调用时(在运行时)。我的想法是伪代码这是我想要的想法(在伪代码中)。这个想法基本上由3部分组成:带参数的方法持有整数值的变量具有具体值的方法调用(一个方法. 以下是我的方法的定义,该方法使用名为typehey的泛型参数命名:pathToAnyIntegergenericPathToIntclass 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);   }}预期产出//outputhey 2hey 8个人言论再次,如果这是重复的,对不起,如果这是一个愚蠢的问题,对不起。如果您需要进一步澄清,我愿意提供帮助。任何帮助表示赞赏。嘿,如果你在回答中不友善(主要是侮辱,但也暗示语气),即使你有解决方案,也不要回答。不需要你的帮助。谢谢!:)
查看完整描述

3 回答

?
qq_遁去的一_1

TA贡献1725条经验 获得超8个赞

Java 中的整数是原始类型,按值传递。所以你并没有真正将“路径”传递给整数,而是传递实际值。另一方面,对象是通过引用传递的。


您的伪代码只需稍作修改即可在 Java 中运行。该代码假定所有类都在同一个包中,否则您需要将所有内容公开(或其他访问修饰符,具体取决于用例)。


// First letter of a class name should be uppercase

class 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);

    }

}


//output

hey 2

hey 8

此代码首先定义了 MainClass 类,其中包含您的方法hey。我将方法设为静态,以便能够将其称为MainClass.hey(). 如果它不是静态的,则需要在 Declare 类中实例化 MainClass 对象,然后调用该对象的方法。例如:


...

MainClass mainClassObject = new MainClass();

mainClassObject.hey(aObject.myInt);

...


查看完整回答
反对 回复 2022-07-27
?
哔哔one

TA贡献1854条经验 获得超8个赞

Java(从 Java 8 开始)包含函数式编程的元素,它允许与您正在寻找的东西类似的东西。您的hey方法可能如下所示:


void hey(Supplier<Integer> integerSupplier) {

     System.out.printl("Hey" + integerSupplier.get());

}

此方法声明了一个参数,该参数可以是“将返回整数的方法调用”。


您可以调用此方法并将其传递给所谓的 lambda 表达式,如下所示:


hey(() -> myObject.getInt());

或者,在某些情况下,您可以使用所谓的方法引用,例如:


Hey(myObject::getInt)

在这种情况下,两者都意味着“调用 hey 方法,当它需要一个整数时,调用 getInt 来检索它”。lambda 表达式还允许您直接引用字段,但公开字段被认为是一种不好的做法。


查看完整回答
反对 回复 2022-07-27
?
九州编程

TA贡献1785条经验 获得超4个赞

如果我正确理解了您的问题,您需要使用继承来实现您正在寻找的东西。


让我们从创建层次结构开始:


class SuperInteger {

   int val;

   //additional attributes that you would need.


   public SuperInteger(int val) {

      this.val = val;

   }

   public void printValue() {

      System.out.println("The Value is :"+this.value);

   }

}  


class SubIntA extends SuperInteger {

    //this inherits "val" and you can add additional unique attributes/behavior to it 

   public SubIntA(int val) {

      super(val);

   }

   @override

   public void printValue() {

      System.out.println("A Value is :"+this.value);

   }

}


class SubIntB extends SuperInteger {

    //this inherits "val" and you can add additional unique attributes/behavior to it 

   public SubIntB(int val) {

      super(val);

   }

   @override

   public void printValue() {

      System.out.println("B Value is :"+this.value);

   }

}

现在,您的方法 Signature 可以接受 SuperInteger 类型的参数,并且在调用该方法时,您可以传递 SubIntA/SuperInteger/SubIntB,因为 Java 为您隐式向上转换。


所以:


public void testMethod(SuperInteger abc) {

     a.val = 3;

     a.printValue();

}

可以从 main 调用:


public static void main(String args[]){

   testMethod(new SubIntA(0));

   testMethod(new SubIntB(1));

   testMethod(new SuperInteger(2));

}

获得如下输出:


A Value is :3

B Value is :3

The Value is :3


查看完整回答
反对 回复 2022-07-27
  • 3 回答
  • 0 关注
  • 124 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号