1.无参数(void)无返回值

    1. public class Person {
    2. public void born(){
    3. System.out.println("我出生啦");
    4. }
    5. }
    1. public class Test {
    2. public static void main(String[] args) {
    3. Person p = new Person();
    4. p.born();
    5. }
    6. }
    7. >>> 我出生啦

    2.无参数有返回值

    1. public class Person {
    2. public String thing(){
    3. System.out.println("你今天做了什么?");
    4. return "答:我今天学了6节课程";
    5. }
    6. }
    1. public class Test {
    2. public static void main(String[] args) {
    3. Person p = new Person();
    4. // 接返回值
    5. String thing = p.thing();
    6. System.out.println(thing);
    7. // 不接返回值
    8. p.thing();
    9. }
    10. }
    11. >>> 你今天做了什么?
    12. >>> 答:我今天学了6节课程
    13. >>> 你今天做了什么?

    3.有参数无返回值

    1. public class Person {
    2. public void day(String day){
    3. System.out.println("今天是:"+day);
    4. }
    5. }
    1. public class Test {
    2. public static void main(String[] args) {
    3. Person p = new Person();
    4. p.day("星期一");
    5. }
    6. }
    7. >>> 今天是:星期一

    4.有参数有返回值

    1. public class Person {
    2. public String judge(String weather){
    3. if(weather == "sunny"){
    4. return "出去野炊";
    5. }else {
    6. return "在家学习";
    7. }
    8. }
    9. }
    1. public class Test {
    2. public static void main(String[] args) {
    3. Person p = new Person();
    4. String weather = p.judge("aaaa");
    5. System.out.println(weather);
    6. }
    7. }
    8. >>> 在家学习