1、新建一个工程,在工程下新建一个类Method01(勾选创建main函数)
package ClassStudy;class Person
{String name; //默认为nullint age; //默认为0//构造函数public Person(){name=null;age=0;}//方法public void say(){System.out.println("姓名:"+name);}//方法重载public void say(String hString) {System.out.println(hString+"姓名"+name);}
}public class Method01 {public static void main(String[] args) {Person p=new Person();//p.name="张三";p.say();p.say("你好!");}
}
2、使用new关键字初始化类对象时申请内存空间示例
public class Method01 {public static int sum(int n){if(n==1)return 1;return n+sum(n-1);}public static void main(String[] args) {System.out.println(sum(100));}
}