如何在主类中引入字符串方法? [英] How induce string method in main class?

查看:52
本文介绍了如何在主类中引入字符串方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我怎样才能在大班中引出qwe()方法?



我尝试了什么:



Hi how i can induce qwe() method in main class?

What I have tried:

public class czas {
 
    public static void main(String[] av) {
       
 
        
    }
    public String qwe(){
    SimpleDateFormat a = new SimpleDateFormat("EEE MMM d kk:mm:ss");
        
        String time=a.format(new Date());
        return time;
    }
}

推荐答案

你不能在main函数中执行qwe函数,(我认为那是你的问题),原因是你的主要功能是静态的,而你的qwe功能是实例功能。为此,请将函数的签名更改为,

You cannot execute qwe function in the main function, (I assume that is your problem), the reason for that is that your main function is static, whereas your qwe function is instance function. To do so, either change the signature of your function to,
public static String qwe(){



或创建一个新的czas类实例,然后调用该函数,例如,


Or create a new instance of your czas class and then call that function, like,

public class czas { 
    public static void main(String[] av) {
        System.out.println(new czas().qwe()); 
    }

    public String qwe(){
        SimpleDateFormat a = new SimpleDateFormat("EEE MMM d kk:mm:ss");    
        String time=a.format(new Date());
        return time;
    }
}



这会有效,并会给你结果。你需要了解静态和实例函数的区别 - 许多初学者都不知道,所以我不会责怪你。


This would work and will give you the result. You need to understand the difference in static and instance function — which many beginners do not know, so I won't blame you here.


这篇关于如何在主类中引入字符串方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆