目录
2.递归的调用机制,实践小案例:一定要认真看图 :栈先进后出
1.递归斐波那契数:1 1 2 3 5 8 13,给你一个数求他对应的斐波那契数是多少;
2.有一堆桃子,猴子每天吃其中的一半,在多吃一个,到第十天,还没吃发现就剩1个了,求一共有多少桃子? 1534
5.八皇后啊:两个皇后不能在同一个列于同一个斜线上(没解决)
判断对象是否为同一个:使用hashCode方法
类是抽象的,概念的,代表一类人一类事物。对象是具体的,实际的代表一个具体事物,即实例。类是对象模板,对象是类的一个个体。
- public class Hello {
- public static void main(String[] args) {
- //实例化一只猫 cat1就是一个对象
- Cat cat1 = new Cat();
- cat1.name="kitti";
- cat1.age=3;
- cat1.color="white";
- //创建第二只猫,并赋值给cat2
- Cat cat2 = new Cat();
- cat2.name="qiqi";
- cat2.age=100;
- cat2.color="red";
- //访问属性
- System.out.println("first cat name is:"+cat1.name+"age is"+cat1.age+"color is"+cat1.color);
- System.out.println("second cat name is:"+cat1.name+"age is"+cat2.age+"color is"+cat2.color);
- }
- }
-
- //定义一个猫类 Cat -> 自定义数据类型
- class Cat{
- //属性
- String name;
- int age;
- String color;
- }

定义:成员变量 = 属性 =filed(字段) 。属性可以是基本数据,也可以是引用数据类型。
声明:访问修饰符 属性类型 属性名;如果不赋初始值,就会和数组一样有默认值
1.先声明在创建; Cat cat;声明对象 cat = new Cat();//创建
2.直接创建 Cat cat = new Cat();
对象名.属性名;

Person p = new Person() p.name="jack" p.age =10

Person的成员方法speak,触发输出I can speak;
- public class Hello {
- public static void main(String[] args) {
- Person p = new Person();
- p.speak();
- }
- }
-
- class Person{
- String name;
- //public:公开 void:无返回值 speak():方法名+形参列表 {}:方法体写执行的带啊吗
- public void speak(){
- System.out.println("i can speak");
- }
- }
- class Person {
- String name;
- //public:公开 void:无返回值 speak():方法名+形参列表 {}:方法体写执行的带啊吗
- public void speak() {
- System.out.println("i can speak");
- }
- public void grade(int n) {
- System.out.println("grade is " + n);
- }
- public int getAge() {
- return age;
- }
- }

案例:输出打印数组
- public class Hello {
- public static void main(String[] args) {
- int[][] arr = {{1, 23, 5}, {6, 2, 4}, {2, 8}, {10}};
- Tools tools = new Tools();
- tools.map(arr);
- }
- }
- class Tools {
- public void map(int[][] arr) {
- for (int i = 0; i < arr.length; i++) {
- for (int j = 0; j < arr[i].length; j++) {
- System.out.print(arr[i][j] + "\t");
- }
- System.out.println();
- }
- }
- }
好处:提高代码复用性,将实现细节封装起来。
访问修饰符 返沪数据类型 方法名 (形参列表){ 方法体 }
返回数据类型需要与return返回的数据兼容或相同
方法名采用小驼峰命名
参数列表,一个方法可以有零个参数,也可以有多个参数,中间用逗号隔开,参数也是兼容或相等即可(byte可以传给int)
public void sum(int a) a:形参 sum(10) 10:实参
同一个类当中,方法调用直接写"方法名()",不同类,先实例化对象,然后 对象.方法名();
1.判断一个数为奇数还是偶数:
- import java.util.*;
-
- public class Hello {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("please input number");
- boolean ood = new Tools().isOod(sc.nextInt());
- if (ood) {System.out.println("it is odd");}else{System.out.println("it isn't odd");}
- }
- }
-
- class Tools {
- public boolean isOod(int num) {
- return num % 2 == 0 ? false : true;
- }
- }
2.输入几行几列就打印几行几列的#
- import java.util.*;
-
- public class Hello {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("please input rows col");
- new Tools().print(sc.nextInt(), sc.nextInt());
- }
- }
-
- class Tools {
- public void print(int rows, int cols) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- System.out.print("#\t");
- }
- System.out.println();
- }
- }
- }
值传递:

引用传递:

对象传递:
8.案例:1.克隆一个对象的属性,但是两个是独立的
- public class Hello {
- public static void main(String[] args) {
- Person p1 = new Person();
- p1.name = "jack";
- p1.age = 18;
- Person p2 =new Tools().copyPerson(p1);
- // hashCode可以判断对象是否为同一个
- System.out.println(p1.name+"---"+p1.age+"---"+p1.hashCode());
- System.out.println(p2.name+"---"+p2.age+"---"+p2.hashCode());
- }
- }
-
- class Person {
- String name;
- int age;
- }
-
- class Tools {
- public Person copyPerson(Person p) {
- Person p2 = new Person();
- p2.name=p.name;
- p2.age = p.age;
- return p2;
- }
- }
自己调用自己的方法,要准备出口哦
打印问题

阶乘:自己画个图很清晰啊:
四、递归案例- import java.util.*;
-
- public class Hello {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("please inpu a number");
- int feibonaqie = new Tools().feibonaqie(sc.nextInt());
- System.out.println(feibonaqie);
- }
- }
-
- class Tools {
- public int feibonaqie(int i) {
- if (i == 1 || i == 2) {
- return 1;
- } else {
- return feibonaqie(i - 1) + feibonaqie(i - 2);
- }
- }
- }
- public class Hello {
- public static void main(String[] args) {
- int peaches = new Tools().eatPeach(10);
- System.out.println(peaches);
- }
- }
-
- class Tools {
- public int eatPeach(int day) {
- if (day <= 1) {
- return 1;
- } else {
- return (eatPeach(day - 1) + 1) * 2;
- }
- }
- }
- public class Noot {
- public static void main(String[] args) {
- //二维数组表示迷宫 int[][] map = new int[8][7];
- //规定map的元素值:0表示可以走,1表示障碍物
- int[][] map = new int[8][7];
- //将最上面一行和最下面一行都变成1
- for (int i = 0; i < map.length; i++) {
- for (int j = 0; j < map[i].length; j++) {
- if (i == 0 || i == map.length-1){
- map[i][j] = 1
- }
- }
- }
-
- //遍历数组,看效果
- for (int i = 0; i < map.length; i++) {
- for (int j = 0; j < map[i].length; j++) {
- System.out.print(map[i][j]+"\t");
- }
- System.out.println();
- }
- }
- }
-
- class T {
- //使用递归回溯的思想来解决老鼠出迷宫
- //如果找到就返回true,否则返回false
- //map为地图,i,j为初始位置,初始化位置为(1,1)
- //因为我们是递归找路,所以我先规定 map数组的各个值的含义
- //map的元素 0表示可以走,1表示障碍物, 2表示可以走,3表示走过是死路
- //当(6,5)位置为2时就说明找到通路了
- //先确定老属找路的策略 下->右->上->左
- public boolean findWay(int[][] map, int i, int j) {
- if (map[6][5] == 2) {
- return true;
- } else {
- if (map[i][j] == 0) { //假设这个走得通就看他的下一个点
- map[i][j] = 2;
- if (findWay(map, i + 1, j)) { //下
- return true;
- } else if (findWay(map, i, j + 1)) { //右
- return true;
- } else if (findWay(map, i - 1, j)) { //上
- return true;
- } else if (findWay(map, i, j - 1)) { //左
- return true;
- }else{
- map[i][j] = 3;
- return false;
- }
- } else {
- return false;
- }
- }
- }
- }
- public class Hello {
- public static void main(String[] args) {
- new Tower().move(5, 'A', 'B', 'C');
- }
- }
-
- class Tower {
- public void move(int num, char a, char b, char c) {
- if (num == 1) {
- System.out.println(a + "-->" + b);
- } else {
- move(num - 1, a, c, b);
- System.out.println(a + "-->" + b);
- move(num - 1, c, b, a);
- }
- }
- }
方法名必须相同,参数列表:必须不同,要么类型不同,要么顺序不同,参数名无要求,返回值类型无要求
- public class Hello {
- public static void main(String[] args) {
- }
- }
-
- class Calculation{
- //两个整数的和
- public int calculate(int n1,int n2){
- return n1+n2;
- }
- //一个整数,一个double的和
- public double calculate(int n1,double n2){
- return n1+n2;
- }
-
- //一个double的和,一个整数,
- public double calculate(double n1,int n2){
- return n1+n2;
- }
-
- //三个整数
- public int calculate(int n1,int n2,int n3){
- return n1+n2+n3;
- }
- }
1.定义:(int... num),传递的参数可以为数组,可变参数实质为数组,可以和普通参数放在一起,可变参数放在最后。最多只有一个可变参数。
案例:使用可变参数返回,1姓名2课程(总分),1姓名3课程(总分)。。。
- public class Hello {
- public static void main(String[] args) {
- new HspMethods().showScore("jack",60,10,90);
- }
- }
-
- class HspMethods {
- public void showScore(String name, int... score) {
- double totalScore = 0.0;
- for (int i = 0; i < score.length; i++) {
- totalScore += score[i];
- }
- System.out.println("he name is " + name + ';' + score.length + "class totalScore is " + totalScore);
- }
- }
全局变量:也就是属性,作用域为整个类体;属性在定义时可以直接赋值。全局变量可以直接使用,不需要赋值,因为有默认值。
局部变量:在成员方法中定义或者是传递的形参,作用域在整个方法中,必须赋值才能使用,因为没有默认值,不能加修饰符
就近原则:局部变量可以和全局变量重名,谁离的近变量指的就是谁,两个局部变量不能重名
- public class Hello {
- public static void main(String[] args) {
- new HspMethods().showScore("jack", 60, 10, 90);
- }
- }
-
- class Person {
- String name; //全局变量
-
- public void showName(String name) { //局部变量
- name = 10; //局部变量
- }
- }
[修饰符] 方法名(形参列表){ 方法体 } ===》1.修饰符可以默认,2构造器没有返回值,3.方法名与类名一样,4.参数列表和成员方法一样的规则,5.构造器由系统调用。构造器是初始化对象,而不是创建对象
案例1.创建对象时指定人的姓名与年龄
- public class Hello {
- public static void main(String[] args) {
- new Person("Smith", 19).show();
- }
- }
-
- class Person {
- String name;
- int age;
-
- public Person(String pName, int pAge) {
- name = pName;
- age = pAge;
- }
-
- public void show() {
- System.out.println("name " + name + " age " + age);
- }
- }
- public class Hello {
- public static void main(String[] args) {
- new Person().show();//使用无参构造器 null 18
- new Person("Smith", 19).show();//使用有参构造器 Smith 19
- }
- }
-
- class Person {
- String name;
- int age;
-
- public Person(String pName, int pAge) {
- name = pName;
- age = pAge;
- }
-
- public Person(){
- age=18;
- }
-
- public void show() {
- System.out.println("name " + name + " age " + age);
- }
- }

解决变量名的命名问题,一个笑话,赵本山问奥尼尔,你知道我爸爸的爸爸是谁吗,奥尼尔说不知道,赵本山说是我爷爷。然后奥尼尔回美国问科比你只我我爸爸的爸爸是谁吗?科比说我不知道啊,奥尼尔说是赵本山爷爷。
那个调用this就指向谁
- public class Hello {
- public static void main(String[] args) {
- new Person("Smith", 19).show();
- }
- }
-
- class Person {
- String name;
- int age;
-
- public Person(String pName, int pAge) {
- this.name = pName;
- this.age = pAge;
- }
- }
案例:比较类的大小:
- public class Hello {
- public static void main(String[] args) {
- System.out.println(new Person("Smith", 19).comparaTo(new Person("Smith",19))+"");
- }
- }
-
- class Person {
- String name;
- int age;
-
- public Person(String name,int age){
- this.name = name;
- this.age = age;
- }
-
- public boolean comparaTo(Person p){
- return this.name.equals(p.name)&&this.age == p.age;
- }
- }
1.定义类,方法max,实现求某个double数组的最大值,并返回
- public class Hello {
- public static void main(String[] args) {
- double[] arr = {1,2,3,3,1,2};
- System.out.println(new A01().max(arr));
- }
- }
-
- class A01 {
- public double max(double[] arr){
- double max = arr[0];
- for (int i = 1; i < arr.length; i++) {
- if (max
- }
- return max;
- }
- }
2.定义方法find,实现字符串数组的查找,并返回索引,没找到返回-1
- public class Hello {
- public static void main(String[] args) {
- String[] arr ={"apple","banana","pear"};
- String str = "apple";
- System.out.println(new A02().find(str,arr));
- }
- }
-
- class A02 {
- public int find(String str ,String[] arr){
- int index = -1;
- for (int i = 0; i < arr.length; i++) {
- if (arr[i].equals(str)) index = i;
- }
- return index;
- }
- }
3.定义书本方法,超过150就显示那个,没过150显示100
- public class Hello {
- public static void main(String[] args) {
- Book book = new Book();
- book.updatePrice(160);
- book.showInfo();
- }
- }
-
- class Book {
- int price;
-
- public void updatePrice(int price) {
- this.price = 100;
- if (price>150) this.price = price;
- }
-
- public void showInfo() {
- System.out.println("book's price is " + this.price);
- }
- }