
分享兴趣,传播快乐,增长见闻,留下美好!
亲爱的您,这里是LearningYard新学苑。
今天小编为大家带来
“C与Java之间的异同(二)”
欢迎您的访问。
Share interest, spread happiness, increase knowledge, and leave beautiful.
Dear, this is the LearingYard Academy!
Today, the editor brings the
Similarities and Differences between C and Java (2),
Welcome to visit!

三、函数与方法
Java中的方法和C中的函数差不多,只是有些略微的差别。
Methods in Java are similar to functions in C, with some slight differences.
在C中,函数的定义较简单,在主函数的前面定义。定义格式如图所示。先定义函数返回类型,如我这的方法不需要返回值,那么我就直接用void,后面不用写return语句。后面则是函数名,由自己命名,比如这里是用来打印字符串的函数,那么我可以用“_printf”来命名。括号中是否需要定义值跟你定义函数的用途有关,比如我定义的函数是为了打印字符串,那我就定义char型的数组。大括号中写上实现函数作用的代码,比如我这里要打印字符串,那么我就用printf语句将我定义的str[]打印出来。这样,一个简单的函数就写好了。调用函数方式很简单,函数名加括号加你想带入的数据。这里我输入一个字符数组a[],我调用刚刚定义的打印函数,在括号中写上刚刚输入的变量a就可以完成方法的调用了。需要注意的是,括号中的变量类型要与定义的函数中的类型一致。
In C, the definition of a function is simpler, defined before the main function. The definition format is shown in the figure. Let's define the return type of the function, so if my method here doesn't need a return value, then I'll just use void and I don't have to write a return statement after that. And then I'll have the name of the function, and I'll give it my own name, so in this case it's a function that prints a string, so I'll use _printf. Whether you need to define a value in parentheses depends on what you're defining the function for. For example, if I'm defining a function to print a string, I'm defining an array of char. The code to implement the function in the braces, for example, if I want to print a string here, then I will use the printf statement to print out the str[] that I defined. So, a simple function is written. The way to call a function is very simple, the function name with parentheses and the data you want to bring in. Here I enter a character array a[], I call the just defined print function, in parentheses just input variable a can complete the method call. Note that the type of the variable in parentheses is the same as the type in the defined function.

在Java中,定义方法较复杂。因为Java的方法必须在类中创建。若想要像在C中一样直接调用方法的话,要在方法名前面加static修饰。其他部分和C相似。值得注意的是,在static修饰后的方法用类名加点加方法名来调用,如图(类名为Text,方法名为_printf)。
In Java, defining methods is more complex. Because Java methods must be created in the class. If you want to call a method directly, as in C, add the static modifier to the method name. Other parts are similar to C. It is worth noting that the method after static modification is called with the class name plus the method name, as shown in the figure (class name Text, method name _printf).


四、总结
看到了这里,相信大家不难发现,Java的语言比C语言要冗杂,而且Java中的概念也比C多。但Java也因此能处理更复杂的问题,进行复杂的操作。小编也是才开始学习Java,感受到了Java与C的不同,分享一下我的经验。这次就分享到这吧,后面我会继续学习更多编程知识与大家分享,感谢你的关注!
Seeing here, I believe that it is not difficult to find that the Java language is more verbose than the C language, and the concepts in Java are more than C. But it also allows Java to handle more complex problems and perform more complex operations. Xiao Bian also just began to learn Java, feel the difference between Java and C, share my experience. This time to share it, I will continue to learn more programming knowledge to share with you, thank you for your attention!
今天的分享就到这里了,
如果您对文章有独特的想法,
欢迎给我们留言。
让我们相约明天,
祝您今天过得开心快乐!
That's all for today's sharing.
If you have a unique idea about the article,
please leave us a message,
and let us meet tomorrow.
I wish you a nice day!
参考资料
翻译:网易有道翻译
本文由LearningYard学苑整理并发出,如有侵权请后台留言沟通.
文案|Dongyang
排版|Dongyang
审核|yue
Learning Yard 新学苑