What is an object, varargs, overloading, recursion

1. Classes and Objects

  • 1. A method can have 0 parameters or multiple parameters, separated by
  • 2. The parameter type can be any type, basic data type and reference data type
  • 3. When calling the parameter method, it must correspond to our parameter list and pass in the same and compatible
  • 4. It is called a line parameter when it is defined, and it is called an actual parameter when it is called
  • 6. Just call the method directly in the same class
  • 7. To call methods in different classes, you need to use the object name to call
  • 8. Cross-class method calls are related to access modifiers
public class Class04 {
    
    
    public static void main(String[] args) {
    
    
        //如何去创建对象
        //1.先声明在创建
        AA aa;//先声明aa
        aa=new AA();//创建
        //2.直接创建
        AA aa1=new AA();
        //如何访问属性? 对象名.属性名
    }
}
class AA{
    
    
    int id;
}

2. Variable parameters:

  • Basic syntax: access modifier return type method name (data type... row parameter name) {}
  • Precautions:
  • 1. The actual parameter of the variable parameter can be 0 or more
  • 2. The actual parameter of the variable parameter can be an array
  • 3. The essence of variable parameters is an array
  • 4. Variable parameters at the end
  • 5. Only one variable parameter can appear in a line parameter list
/**
 *有三个方法,分别实现返回姓名和两门成绩总分,返回姓名和三门成绩的总分,返回姓名和五门成绩的总分,封装成一个可变参数的方法!
 */
public class Print02 {
    
    
    public static void main(String[] args) {
    
    
        MeThed meThed = new MeThed();
        System.out.println(meThed.showScore("张鑫", 80, 90));
        System.out.println(meThed.showScore("张鑫", 100, 90, 120));
        System.out.println(meThed.showScore("张鑫", 100, 120, 130, 140, 150));
    }
}

class MeThed {
    
    
    public String showScore(String name, double... scores) {
    
    
        double MyScores = 0;
        for (int i = 0; i < scores.length; i++) {
    
    
            MyScores += scores[i];
        }
        return name + "有" + scores.length + "门课成绩的总分为" + MyScores;
    }
}

3. Method overloading

  • Ease the trouble of naming Ease the trouble of registering

  • In the same class, more than one method with the same name is allowed

  • Precautions and usage details

    • 1. The method names must be the same
    • 2. Line parameter list must be different
    • 3. It has nothing to do with the return value, just look at the parameter list.
    • 4. Return type is not required
    public class Overload01 {
          
          
        public static void main(String[] args) {
          
          
            MyCalculator myCalculator = new MyCalculator();
            System.out.println(myCalculator.num(4, 5));
            System.out.println(myCalculator.num(7, 5));
            System.out.println(myCalculator.num(5, 7.8));
            System.out.println(myCalculator.num(1, 2, 3));
        }
    }
    
    class MyCalculator {
          
          
        //两个整数的和
        public int num(int n1, int n2) {
          
          
            System.out.println("int num(int n1,int n2)会被调用");
            return n1 + n2;
        }
    
        //一个double和一个int的和
        public double num(double n1, int n2) {
          
          
            System.out.println("double num(double n1,int n2)会被调用");
            return n1 + n2;
        }
    
        //一个int和一个double的和
        public double num(int n1, double n2) {
          
          
            System.out.println("double num(int n1,double n2)会被调用");
            return n1 + n2;
        }
    
        //三个int的值相加
        public int num(int n1, int n2, int n3) {
          
          
            System.out.println("int num(int n1,int n2,int n3)会被调用");
            return n1 + n2 + n3;
        }
    }
    

    4. Method recursion

  • 1. When a method is executed, a new protected independent space is created

  • 2. The local variables of the method are independent and will not affect each other.

  • 3. If the reference data type used in the method (array, object), the data of the reference type will be shared

  • 4. The recursive must be recursive conditional approximation, otherwise there will be infinite recursion and dead recursion

  • 5. After the execution is completed, if return is encountered, it will return.

  • 6. Two conditions must be met

    ​ (1) There is a boundary, that is, the termination condition

    ​ (2) Need to call yourself

    Return is not necessary in recursion, such as

    public void test(int n) {
          
          
            if (n > 1) {
          
          
                test(n - 1);
            }
            System.out.println(n);
        }
    

    recursive instance

    Monkey eating peach problem: There is a pile of peaches, the monkey ate half of them on the first day, and ate one more! After that, the monkeys ate half of them every day, and then ate one more. When it was the 10th day, when I wanted to eat again (that is, I hadn't eaten), I found that there was only one peach. Question: How many peaches were there initially?

    Idea: Reverse push
    1.day=10 has 1 peach
    2.day=9 has (day10+1)* 2=4
    3.day=8 has (day9+1)* 2= 10
    4. The rule is the previous day Peaches = (Peaches of the next day + 1) * 2
    5. Recursion

    //求第day天有多少个桃子
	public int peach(int day) {
    
    
        if (day == 10) {
    
    //-1
            return 1;
        } else if (day >= 1 && day <= 9) {
    
    
            return (peach(day + 1) + 1) * 2;
        } else {
    
    
            System.out.println("day在1-10");
            return -1;
        }
    }

Guess you like

Origin blog.csdn.net/weixin_52682014/article/details/127504508