简体   繁体   English

是否可以调用没有名称的函数?

[英]is it possible to call a function without it's name?

I tried calling a function in my code, but i get a blank screen only. 我尝试在代码中调用一个函数,但仅出现空白屏幕。

class Addition
{
    public void add(){
    int a=20;
    int b=30;
    int c=a+b;
    System.out.println("Addition of a and b is: " +c);
    }
 }
public class Methodcall
{
    public static void main(String args[])
    {
        Addition call=new Addition();

    }
}

I expect the addition of a & b But all i get is just a blank screen only 我希望添加a&b但我得到的只是一个空白屏幕而已

The code wouldn't do what you're expecting it to do: 该代码不会执行您期望的操作:

In your main function, you're only instantiating the Addition object. 在您的main功能中,您仅实例化Addition对象。 Think of it as creating the object: 将其视为创建对象:

Addition call = new Addition();

If you want it to do something , in your case to add a and b and print the result, you will need to call the method: 如果您希望它做某事 (在您的情况下)添加ab并打印结果,则需要调用方法:

call.add();

As others have mentioned, you could put it in the constructor, but that's not what the constructor is meant for. 正如其他人提到的,您可以将其放在构造函数中,但这不是构造函数的目的。

 Addition call=new Addition();
 call.add();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不调用call方法的情况下调用functor对象的函数 - How to call functor objects's function without invoking call method 是否可以通过名称调用方法? - Is it possible to call methods by name? 是否可以在不使用 AsyncTask 完成另一个之后调用一个 function? - Is it possible to call one function after finishing another without using AsyncTask? Java按名称动态调用函数的最惯用的方式是什么? - What's the most idiomatic way in Java to dynamically call a function by name? 无需等待即可调用函数 - Call a function without waiting for it 是否可以使用findbugs的代码分析检索在java中的函数调用中传递的变量的名称 - Is it possible to retrieve the name of the variable that is passed in a function call in java using code analysis of findbugs 是否可以从jdbc调用pl / sql函数并按名称注册返回值? - Is it possible to call pl/sql function from jdbc and register the return value by name? 是否可以获取该对象的名称? - Is it possible to get this object's name? Spark的通话功能 - Spark's call function 有什么方法可以通过子类对象调用超类的方法,而不必通过同名的子类方法吗? - Is there any way to call superclass's method on a subclass object without going through the subclass method of the same name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM