在java中方法应该如何使用,如何去定义,请说的具体点

来源:百度知道 编辑:UC知道 时间:2024/05/31 08:00:21

class A
{

public void method1()
{ }

public static void staticmethod1()
{ }
}
这样就定义了两个方法,java所有方法都是存在于类中
如上,这两个方法存在于A类中。可以写另一个类来用这两个方法。

class usemethod
{
public static void main(Strubg args[])
{
A.staticmethod1();//这里调用了A类的静态方法staticmethod1.
A a = new A();
a.method1();//这里调用了A类的a实例的方法method1
}
}

用java不是光会调用方法就行的。必须从基础入手。

建议先学C语言或者C++,如果想从java开始,推荐Think in java 4th

Fields and methods
When you define a class (and all you do in Java is define classes, make objects of those classes, and send messages to those objects), you can put two types of elements in your class: fields (sometimes called data members), and methods (sometimes called member functions). A field is an object of any type that you can talk to via its reference, or a primitive type. If it is a reference to an object, you must initialize that reference to