package example;//说明所写类所属的包名,可有可无。没有的话,系统自动使用缺省的包。import java.util.Date;//将系统包util中的Date类调入所写的程序,这样就可以使用了。 public class Example{ public static void main(String arg[]){ Date dt=new Date();//定义Date类型变量,不把Date类包含进来,Date类型以及Date类中的 //方法如下面的toString将不可以使用。 System.out.println(dt.toString());//输出 } }
调用很简单 假设你有一个类Test 类中有两个方法aMethod,bMethodpublic class Test{ public void aMethod(){ System.out.println("a方法"); } public void bMethod(){ System.out.println("b方法"); } public static void main(String args[]){ Test t = new Test();//先创建类Test的对象t引用 t.aMethod();//调用了a方法 t.bMethod();//调用了b方法 } }
public class Example{
public static void main(String arg[]){
Date dt=new Date();//定义Date类型变量,不把Date类包含进来,Date类型以及Date类中的
//方法如下面的toString将不可以使用。
System.out.println(dt.toString());//输出
}
}
假设你有一个类Test
类中有两个方法aMethod,bMethodpublic class Test{
public void aMethod(){
System.out.println("a方法");
}
public void bMethod(){
System.out.println("b方法");
}
public static void main(String args[]){
Test t = new Test();//先创建类Test的对象t引用
t.aMethod();//调用了a方法
t.bMethod();//调用了b方法
}
}
public void aMethod(String str){
System.out.println("a方法");
}在调用方法时
String str="fighting";
t.aMethod(str);//调用了a方法
很简单的写多了就明白了new出类的实例
用实例调用方法一般用“.”来调用方法