难点程序

DAY 05

1、

public classArrayDemo {


       public static void main(String[] args){


              // int[] arr = newint[]{5,1,7,0,8,1,3};

              // int[] arr = {3,1,7,3,8,0,4};

              // arr[3] = 15;

              // System.out.println(arr[3]);


              int[] arr;

              // arr = new int[5];

              // arr = new int[]{5,1,7,0,8,1,3};

              arr = {3,1,7,3,8,0,4};

2、

importjava.util.Arrays;

public classArrayCopyDemo {


       public static void main(String[] args){


              // int[] arr1 = {5,1,7,0,8,2,6};


              // int[] arr2 = new int[9];


              //表示从arr1下标为2的位置上复制4个元素放入arr2数组中,从arr2数组的下标为4的位置开始放

              // System.arraycopy(arr1, 2, arr2,4, 4);


              //System.out.println(Arrays.toString(arr2));


              //数组的扩容 --- 数组的复制 --- 产生一个新的数组,导致扩容之后的数组和原数组不是同一个

              int[] arr = {3,6,1,7,9};

              /*

              int[] newArr = new int[8];

              System.arraycopy(arr, 0, newArr,0, arr.length);

              arr = newArr;

              */

              System.out.println(arr);

              arr = Arrays.copyOf(arr, 8);

              System.out.println(arr);

              System.out.println(Arrays.toString(arr));





       }


}

3、

importjava.util.Arrays;

public classArrayApplicationDemo {


       public static void main(String[] args){


              int[] arr = newint[]{5,1,7,0,8,1,3,4,12,6};


              //获取最大值

              /*

              int max = arr[0];

              for(int i = 1; i < arr.length;i++){

                     if(max < arr[i])

                            max = arr[i];

              }

              System.out.println(max);

              */

              int max = 0;

              for(int i = 1; i < arr.length;i++){

                     if(arr[max] < arr[i])

                            max = i;

              }

              System.out.println(arr[max]);

              //遍历数组

              //方式一:下标是从0->length - 1

              //先依次获取下标,然后利用下标获取对应位置上的元素

              /*

              for(int i = 0; i < arr.length;i++){

                     //System.out.println(arr[i]);

                     arr[i] += 10;

              }

              */

              //方式二:直接获取每一个位置上的元素

              //每一个元素都是一个int类型元素,那么就可以定义一个int类型的变量来依次表示每一个位置上的值

              //此时i会依次表示数组中的每一个元素

              //增强for循环

              //只能遍历数组但是不能改变数组中的元素

              /*

              for(int i : arr){

                     // System.out.println(i);

                     i += 10;

              }

              */

              //方式三:将数组中的元素一个个的拿出来拼接成字符串

              // String str =Arrays.toString(arr);

              // System.out.println(str);

              //获取指定下标位置上的元素

              // arr[5] += 15;

              // System.out.println(arr[1]);


              //获取数组的长度

              // int len = arr.length;

              // System.out.println(len);

       4、

importjava.util.Scanner;

public classArrayExer {


       public static void main(String[] args){


              //获取行数

              Scanner s = newScanner(System.in);

              int row = s.nextInt();


              //定义二维数组来存储杨辉三角

              int[][] arr = new int[row][];


              //遍历数组,向里填充元素

              for(int i = 0; i < row; i++){

                     //先给每一个一维数组定义大小

                     arr[i] = new int[i + 1];

                     //遍历这个一维数组,向里填充元素

                     for(int j = 0; j <= i;j++){

                            //判断头尾元素

                            if(j == 0 || j == i)

                                   arr[i][j] =1;

                            else

                                   arr[i][j] =arr[i - 1][j] + arr[i - 1][j - 1];

                            //填充完成之后打印这个填充的元素

                            System.out.print(arr[i][j]+ "\t");


                     }

                     System.out.println();

5、

importjava.util.Scanner;

public classArraySearchDemo {


       public static void main(String[] args){


              /*

              int[] arr = {5,1,7,0,8,2,6};


              Scanner s = newScanner(System.in);

              int n = s.nextInt();


              //定义一个变量记录位置

              //规定如果找不到这个元素,将下标记录-1

              int index = -1;


              for(int i = 0; i < arr.length;i++){

                     if(arr[i] == n){

                            index = i;

                            break;

                     }

              }

              System.out.println(index);

              */


              //二分查找/折半查找

              //空间复杂度:o(1)

              int[] arr ={5,9,15,16,28,37,45,48,56,59,60};


              Scanner s = newScanner(System.in);

              int n = s.nextInt();


              //记录最小值的下标

              int min = 0;

              //记录最大值的下标

              int max = arr.length - 1;

              int mid = (max + min) / 2;

              while(arr[mid]!= n){


                     if(arr[mid] > n)

                            max = mid - 1;

                     else

                            min = mid + 1;


                     if(min > max){

                            mid = -1;

                            break;

                     }

                     mid = (min + max) / 2;             

              }     

              System.out.println(mid);


6、

importjava.util.Arrays;

public class ArraySortDemo{


       public static void main(String[] args){


              int[] arr = {5,1,7,0,8,2,6};


              //冒泡排序

              //定义一个循环控制轮数

              /*

              for(int i = 1; i < arr.length;i++){

                     //定义一个循环控制每一个轮的次数

                     for(int j = 1; j <=arr.length - i; j++){

                            if(arr[j - 1] > arr[j]){

                                   int temp =arr[j - 1];

                                   arr[j - 1] =arr[j];

                                   arr[j] =temp;

                            }

                     }

              }


              */

              //选择排序

              //控制轮数

              /*

              for(int i = 1; i < arr.length;i++){

                     //控制每一轮要比较的下标

                     for(int j = i; j

                            //拿选定的下标上的元素和比较的下标上的元素进行比较

                            if(arr[i - 1] >arr[j]){

                                   int temp =arr[i - 1];

                                   arr[i - 1] =arr[j];

                                   arr[j] =temp;

                            }


                     }

              }

              */

              //只能进行升序排序

              //扩展:底层用的是快速排序+归并排序

              //时间复杂度:O(nlogn)

              Arrays.sort(arr);

              //反转数组

              //方式一:

              //时间复杂度O(n),空间复杂度o(n)

              /*

              int[] newArr = newint[arr.length];

              for(int i = arr.length - 1, j = 0;i >= 0; i--, j++){

                     newArr[j] = arr[i];

              }

              */

              //方式二:头尾交换

              //时间复杂度O(n),空间复杂度o(1)

              for(int i = 0, j = arr.length - 1;i <= j; i++, j--){

                     int temp = arr[i];

                     arr[i] = arr[j];

                     arr[j] = temp;

              }

              System.out.println(Arrays.toString(arr));      

       }

}

7、

public classLoopExer {


       public static void main(String[] args){


              /*

                     *

                     **

                     ***

                     ****

                     *****

              */

              for(int i = 1, j = 1; i <= 9;j++){

                     //无论哪一行,上来都是先打印*

                     System.out.print("*");

                     //判断是否是打印完成本行最后一个*

                     if(j == i){

                            //换行

                            System.out.println();

                            //行数+1

                            i++;

                            // *从头重新开始计数

                            j = 0;

                     }

              }

       }

}

DAY 06

1、

packagecn.tedu.test;


public classMethodDemo {


       // Alt + /提示键

       // Ctrl+F11运行

       // Ctrl + Shift + O导包

       // Ctrl+D删除一行

       // Ctrl+/单行注释/取消单行注释

       // Ctrl+Shift+/多行注释

       // Ctrl+Shift+\取消多行注释

       // Alt+↑/↓ 向上/下移动

       public static void main(String[] args) {


              System.out.println(add(100000));


       }


       public static int add(int n) {


              if (n == 1)

                     return 1;


              return n + add(n - 1);


       }


}

2、

packagecn.tedu.object;


public classPersonDemo {


       public static void main(String[] args) {


              //利用new关键字创建了一个Person对象

              Person p = new Person();


              //表示给对象p的属性name进行赋值

              p.name = "秋香";

              //表示给对象p的属性age进行赋值

              p.age = 66;

              // 0表示女生,1表示男生

              p.gender = 0;


              //打印p的属性height的值

              System.out.println(p.weight);


              p.sleep();

              //表示让p对象去执行eat方法

              p.eat();


       }


}


// 定义一个代表人的类

class Person {


       //将人的特征用属性/成员变量来表示

       String name; //姓名

       int age; //年龄

       byte gender; //性别

       double height; //身高

       double weight; //体重


       //将人的行为用方法表示

       //表示吃的行为

       public void eat() {

              System.out.println(name + "在吃东西~~~");

       }


       //表示睡的行为

       public void sleep() {

              //局部变量

              String tool = "床";

              System.out.println(name + "在" + tool + "上睡觉~~~");

       }


}

DAY 07

1、

packagecn.tedu.object;


public classThisDemo {


       public static void main(String[] args) {


              Student s1 = newStudent("Amy");

              System.out.println("s1:"+ s1);

              Student s2 = new Student("Sam");

              System.out.println("s2:"+ s2);


              s1.name = "Lily";


       }


}


class Student {


       String name;

       int age;

       String no; //学号


       //用this代替一个具体的对象

       // this代表当前在活动的对象

       // this在本类中调用本类中的属性和方法

       public Student(String name) {

              this.name = name;

              // System.out.println("this:"+ this);

       }


       public Student(String name, int age) {

              this.name = name;

              this.age = age;

       }


       public Student(String name, int age,String no) {


              // this.name = name;

              // this.age = age;

              // this调用的时候调用的普通的方法而不是构造方法

              // this.Student(name,age);

              // this(参数列表)--- this语句

              //表示调用本类中对应形式的构造方法

              // this(name, age) ->Student(String, int);

              //必须放在构造方法的首行

              this(name, age);

              this.no = no;


       }


}

2、

packagecn.tedu.object;

public classLocalCodeDemo {

       public static void main(String[] args) {


              int i = 8;

              //定义在方法中代码块 --- 局部代码块

              {

                     // j的作用范围变小,生命周期变短,释放内存 -> 提高栈内存的利用率

                     int j = 10;

                     System.out.println(i + j);

              }     

              System.out.println(i);

       }

}

3、

packagecn.tedu.object;


public classConstructorDemo {


       public static void main(String[] args) {


              //调用构造方法创建Person对象

              // Person p = new Person();

              Person p = new Person("翠花");

              p.name = "如花";

              p.eat();


       }


}


class Person {


       String name;

       int age;

       char gender;

       String no; //身份证号


       //构造方法 --- 创建对象

       //在类中没有手动指定构造方法的时候,那么在编译的时候会自动添加一个无参的构造方法

       //特点:没有返回值类型,与类同名

       //构造方法可以构成重载

       public Person() {

       }


       //在代码中手动指定了含参构造,那么就不会再默认添加无参构造

       public Person(String n) {

              //遵循就近原则

              name = n;

       }


       // Person p = new Person("珠子", -18);

       //构造方法中可以写return,用于规避一些不合常理的数据

       public Person(String n, int a) {

              name = n;

              if (a < 0)

                     return;

              age = a;

       }


       public void eat() {

              System.out.println(name + "在吃东西~~~");

       }


}

4、

packagecn.tedu.object;


public classConstructorCodeDemo {


       public static void main(String[] args) {


              // Baby b1 = new Baby();

              // Baby b2 = new Baby("铁蛋");


       }


}


// 定义一个代表婴儿的类

class Baby {


       String name;


       //构造代码块/初始化代码块

       //无论利用哪个构造方法创建对象,构造代码块都会先于构造方法执行

       //如果每一个构造方法中都有一些要初始化的操作,可以将它们提取到构造代码块中执行

       {

              this.cry();

              this.eat();

       }


       public Baby() {

              System.out.println("running~~~");

       }


       public Baby(String name) {

              // this();

              this.name = name;

              // this.cry();

              // this.eat();

       }


       public void cry() {

              System.out.println("这个婴儿在哇哇哇的哭~~~");

       }


       public void eat() {

              System.out.println("这个婴儿在吃奶~~~");

       }


}

5、

packagecn.tedu.extendsx;


public classExtendsDemo {


       public static void main(String[] args) {


              Cat c = new Cat();

              //通过继承,子类可以使用父类中定义的一部分属性和方法

              c.eat();


       }


}


// 父类

class Pet {


       String name;

       String color;


       public Pet(String name) {

       }


       public Pet(String name, String color) {

       }


       public void eat() {

              System.out.println("在吃东西~~~");

       }


}


// 利用extends关键字让原来的类与提取出来的新的类产生了联系 --- 继承

// 子类

class Cat extendsPet {


       public Cat() {

              //在子类构造方法中,如果没有手动指定,那么默认添加super()

              //super语句 --- 表示调用父类中对应形式的构造方法

              // super() --- Pet()

              //如果父类中只提供了含参构造

              //那么子类中就必须手动提供对应形式的super语句

              // super语句必须放在构造方法的第一行

              super("波斯猫");

       }


       public void drink() {

              //通过super代表父类对象

              //通过父类对象调用父类中的方法和属性

              super.eat();

              System.out.println("吃完东西喝点水~~~");

       }


       public void catches() {

              System.out.println("这只猫在挠沙发玩~~~");

       }


}


class Dog extendsPet {


       public Dog() {

              // super("金毛", "绿色");

              super("二哈");

              super.color = "黑色";

       }


       public void bark() {

              System.out.println("这只狗在叫~~~");

       }


}

6、

packagecn.tedu.extendsx;


public classOverrideDemo {


       public static void main(String[] args) {


              Teacher t = new Teacher();

              //子类对象是调用重写之后的方法

              t.work();


       }


}


// 表示职业的类

class Profession {


       public void work() {

              System.out.println("在工作~~~");

       }


}


class Teacherextends Profession {


       //父类中的方法写的比较简单,子类需要对父类中的方法进行扩展

       @Override // @注解

       // @Override这个注解是用于校验当前方法是否构成了重写

       public void work() {

              System.out.println("这个老师在诲人不倦");

       }


}

7、

packagecn.tedu.fengzhuang;


public classFengzhuangExer {


       public static void main(String[] args) {


              Rectangle r = new Rectangle(5.45,3.87);

              System.out.println(r.getGirth());

              System.out.println(r.getArea());


       }


}


// 定义一个代表矩形的类

class Rectangle {


       private double width; //宽

       private double height; //高


       //当矩形定义好之后,宽和高就固定下了

       public Rectangle(double width, doubleheight) {

              //判断宽和高是否合法

              if (width <= 0 || height <=0)

                     return;

              this.width = width;

              this.height = height;

       }


       //矩形画好之后宽和高不能产生变动了,所以不需要再提供set方法

       public double getWidth() {

              return width;

       }


       public double getHeight() {

              return height;

       }


       //计算周长

       public double getGirth() {

              return 2 * (width + height);

       }


       //计算面积

       public double getArea() {

              return width * height;

       }


}

8、

packagecn.tedu.fengzhuang;


public classDriverDemo {


       public static void main(String[] args) {


              Driver d = new Driver();

              // d.name = "Bob";

              // d.age = -28;

              d.setAge(-24);

              System.out.println(d.getAge());


       }


}


class Driver {


       private String name;

       private /*私有的*/int age;

       private String no;

       private char gender;


       public String getName() {

              return name;

       }


       public void setName(String name) {

              this.name = name;

       }


       public int getAge() {

              return age;

       }


       public void setAge(int age) {

              if (age >= 18 && age<= 70)

                     this.age = age;

       }


       public String getNo() {

              return no;

       }


       public void setNo(String no) {

              this.no = no;

       }


       public char getGender() {

              return gender;

       }


       public void setGender(char gender) {

              this.gender = gender;

       }


}

9、

packagecn.tedu.duotai;


public class DuotaiDemo{


       public static void main(String[] args) {


              //父类声明对象,用子类创建对象 --- 向上造型

              //利用向上造型来创建的这个对象

              //对象在编译过程中并不会检查到底使用的是哪个子类

              //在编译期间只会检查声明类和实现类之间是否有继承关系

              //直到运行的时候才会检查具体的子类然后根据子类来分配空间

              Pet p = new Cat();

              //当使用向上造型来创建对象的时候,只能使用父类中声明的方法

              //而不能使用子类中单独定义的方法

              //但是方法的执行看的是具体的子类

              p.eat();


       }


}


class Pet {


       public void eat() {

              System.out.println("在吃东西~~~");

       }


}


class Cat extendsPet {


       @Override

       public void eat() {

              System.out.println("这只猫在吃草~~~");

       }


       public void catches() {

              System.out.println("这只猫在挠狗玩~~~");

       }


}


class Dog extendsPet {


       @Override

       public void eat() {

              System.out.println("这只狗在吃猫~~~");

       }

       public void bark() {

              System.out.println("这只狗在喵喵的叫~~~");

       }

}


DAY  08

1、

packagecn.tedu.staticx;


public class StaticDemo1{


       @SuppressWarnings("static-access")

       public static void main(String[] args) {


              //System.out.println(Student.classroom);


              Student s1 = new Student();

              s1.name = "段誉";

              s1.classroom = "大理";


              Student s2 = new Student();

              s2.name = "王语嫣";

              s2.classroom = "茶花山庄";


              s1.study();

              s2.study();


       }


}


// 定义代表学生的类

class Student {


       String name;

       int age;

       static String classroom;


       @SuppressWarnings("static-access")

       public void study() {

              System.out.println(this.name +"在" + this.classroom + "教室中学习~~~");

       }


}

2、

packagecn.tedu.staticx;


public classStaticDemo2 {


       @SuppressWarnings("static-access")

       public static void main(String[] args) {

              // B.m();

              //当构成静态方法的隐藏的时候,方法的执行看的是声明类

              A a = new B();

              a.m();

              StaticDemo2 p =new StaticDemo2();

              System.out.println(p.qiuhe(6));

       }


       public static int qiuhe(int i){

              int j=1;

                            for(intk=10;(i/10!=0);){

                                   i=i/10;

                                   j++;

                            }

                            return j;

              }


}


class A {


       public static void m() {

              System.out.println("A~~~");

       }


}


class B extends A{


       public static void m() {

              System.out.println("B~~~");

       }

}

3、

packagecn.tedu.staticx;


public classStaticDemo3 {


       public static void main(String[] args) {


              new C();

              // new C();

              // C.m();

              // C.m();


       }


}


class C {


       //静态代码块

       //只在类加载(第一次使用)的时候执行一次

       static {

              System.out.println("C");

       }


       {

              System.out.println("C2");

       }


       public C() {

              System.out.println("C3");

       }


       public static void m() {

              System.out.println("running~~~");

       }


}

4、

packagecn.tedu.staticx;


public classStaticDemo4 {


       public static void main(String[] args) {


              //加载父类 --- 父类静态

              //加载子类 --- 子类静态

              //创建父类对象 --- 父类构造

              //创建子类对象 --- 子类构造

              new SB();

              new SB();


       }


}


class SA {


       SD d;


       static {

              System.out.println("A1");

       }


       {

              System.out.println("A2");

              d = new SD();

       }


       public SA() {

              System.out.println("A3");

       }


}


class SB extendsSA {


       static SC c = new SC();


       static {

              System.out.println("B1");

       }


       {

              System.out.println("B2");

       }


       public SB() {

              System.out.println("B3");

       }


}


class SC {

       public SC() {

              System.out.println("C");

       }

}


class SD extendsSC {

       public SD() {

              System.out.println("D");

       }

}

5、

packagecn.tedu.staticx;


public classStaticDemo5 {


       public static void main(String[] args) {


              System.out.println(D.i);


       }


}


class D {


       static {

              i = 7;

       }

       static int i = 5;


}

6、

packagecn.tedu.staticx;


public classStaticDemo5 {


       public static void main(String[] args) {


              System.out.println(D.i);


       }


}


class D {


       static {

              i = 7;

       }

       static int i = 5;


}

DAY 09

1、

package cn.tedu.abstractx;


publicclass AbstractDemo {


  publicstaticvoid main(String[] args) {


    // 不允许被实例化

    // Pet p = new Pet();

    // p.eat();


    // 创建了一个Pet所对应的匿名内部类的对象

    // Pet p = new Pet() {

    //

    // @Override

    // public void eat() {

    //

    // }

    // }; // 匿名内部类


  }


}


abstractclass Animal {

}


// 抽象类

abstractclass Pet {


  public Pet() {

  }


  // 抽象类中可以定义一切方法

  // 抽象方法

  // Pet.eat();

  publicabstractvoid eat();


  publicvoid drink() {

    System.out.println("在喝水中~~~");

  }


}


// 子类继承抽象类之后必须重写其中的抽象方法,除非子类也是抽象类

abstractclass Cat extends Pet {

}

class Dog extends Pet {

  @Override

  publicvoid eat() {

    System.out.println("这只狗在吃猫~~~");

  }

}


2、

package cn.tedu.abstractx;


publicclass AbstractExer {


  publicstaticvoid main(String[] args) {


    // Shape s = new Rectangle(3.5,

2.4);

    Shapes = new Square(5);

    System.out.println(s.getArea());

    System.out.println(s.getGirth());


  }


}


abstractclass Shape {


  publicabstractdouble getArea();


  publicabstractdouble getGirth();


}


// 代表矩形

class Rectangle extends Shape {


  privatedoublewidth;

  privatedoubleheight;


  public Rectangle(doublewidth, doubleheight) {

    this.width = width;

    this.height = height;

  }

  publicdouble getArea() {

    returnwidth * height;

  }

  publicdouble getGirth() {

    return 2 * (width + height);

  }

}


// 正方形

class Square extends Rectangle {


  public Square(doublewidth) {

    super(width, width);

  }

}

// 椭圆形

class Oval extends Shape {


  privatedoublea;

  privatedoubleb;


  public Oval(doublea, doubleb) {

    this.a = a;

    this.b = b;

  }

  @Override

  publicdouble getArea() {

    return 3.14 * a * b;

  }

  @Override

  publicdouble getGirth() {

    return 3.14 * (a + b);

  }

}

class Circle extends Oval {


  public Circle(doubleradis) {

    super(radis, radis);

  }


}

3、

package cn.tedu.innerclass;

publicclass InnerDemo1 {

  publicstaticvoid main(String[] args) {

    new Outer1().m();

  }

}

class Outer1 {

  inti = 10;

  publicvoid m() {

    System.out.println("Outer~~~");


    // 当方法内部类使用所在的方法中的数据的时候

    // 要求这个属性得是一个常量

    // 从JDK1.8开始,方法内部类使用到当前方法中的数据的时候

    // 将该数据默认为常量

    // 常量的隐式声明

    intj = 10;


    // 方法内部类

    // 只能在定义它的方法中使用

    // 可以使用外部类中的属性和方法

    // 如果内部类和外部类存在了同名属性或者方法,则使用内部类中定义

    // 只能用abstract/final

    // 方法内部类中可以定义非静态的属性和非静态方法

    // 但是不能定义静态变量和静态方法

    // 然而定义静态常量

    class Inner1 extends Object implements Cloneable {

      intk = 8;

      staticfinalintx = 7;

      publicvoid m() {

       System.out.println("Inner~~~");

       i += 5;

       m2();

       // 外部类.this.外部类的方法或者属性

       Outer1.this.m2();

       System.out.println(j);

       // j += 6;

       System.out.println(k);

       System.out.println(x);

      }

      publicvoid m2() {

       System.out.println("Inner m2~~~");

      }

    }

    Inner1i1 = new Inner1();

    i1.m();

  }

  publicvoid m2() {

    System.out.println("Outer m2~~~");

  }


}

2、

package cn.tedu.innerclass;

publicclass InnerDemo1 {

  publicstaticvoid main(String[] args) {

    new Outer1().m();

  }

}

class Outer1 {

  inti = 10;

  publicvoid m() {

    System.out.println("Outer~~~");


    // 当方法内部类使用所在的方法中的数据的时候

    // 要求这个属性得是一个常量

    // 从JDK1.8开始,方法内部类使用到当前方法中的数据的时候

    // 将该数据默认为常量

    // 常量的隐式声明

    intj = 10;


    // 方法内部类

    // 只能在定义它的方法中使用

    // 可以使用外部类中的属性和方法

    // 如果内部类和外部类存在了同名属性或者方法,则使用内部类中定义

    // 只能用abstract/final

    // 方法内部类中可以定义非静态的属性和非静态方法

    // 但是不能定义静态变量和静态方法

    // 然而定义静态常量

    class Inner1 extends Object implements Cloneable {

      intk = 8;

      staticfinalintx = 7;

      publicvoid m() {

       System.out.println("Inner~~~");

       i += 5;

       m2();

       // 外部类.this.外部类的方法或者属性

       Outer1.this.m2();

       System.out.println(j);

       // j += 6;

       System.out.println(k);

       System.out.println(x);

      }

      publicvoid m2() {

       System.out.println("Inner m2~~~");

      }

    }

    Inner1i1 = new Inner1();

    i1.m();

  }

  publicvoid m2() {

    System.out.println("Outer m2~~~");

  }


}

3、

package cn.tedu.innerclass;


publicclass InnerDemo3 {


  publicstaticvoid main(String[] args) {


    Outer3.Inner3oi3 = new Outer3.Inner3();

    oi3.m();

  }


}


class Outer3 {


  inti = 0;


  // 静态内部类

  // 只能使用外部类中的静态属性和静态方法

  // 静态内部类中可以定义一切的方法和属性

  staticclass Inner3 {


    intj = 8;

    staticintk = 4;


    publicvoid m() {

      // System.out.println(i);

    }


  }


}


4、

package cn.tedu.innerclass;


publicclass InnerDemo4 {


  publicstaticvoid main(String[] args) {


    // 匿名内部类

    // a是匿名内部类产生的对象

    // 匿名内部类实际上是实现了对应的接口

    Aa = new A() {


      @Override

      publicvoid m() {

       System.out.println("running~~~");

      }


      @Override

      publicvoid m2() {


      }

    };

    a.m();

    // 匿名内部类实际上是继承了对应的类

    Bb = new B() {


      @Override

      publicvoid m() {

       System.out.println("running~~~");

      }

    };

    b.m();


    // C c = new C() {

    // };

  }


}


interface A {


  void m();


  void m2();

}


abstractclass B {


  publicabstractvoid m();

}


finalclass C {

}

5、

package cn.tedu.interfacex;


import java.io.Serializable;


publicclass InterfaceDemo {


  publicstaticvoid main(String[] args) {


    // 接口不允许实例化

    // Shape s = new Shape();

    System.out.println(Shape.girth);


  }


}


interface Shape extends Cloneable, Serializable {


  // 默认用 public static final 修饰

  staticpublicfinaldoublegirth = 0;


  // public Shape(){}


  // 接口中的方法默认public abstract修饰

  double getGirth();


  publicdouble getArea();


}


// 利用implements关键字让类和接口产生了联系 --- 实现

// Rectangle实现了Shape接口

@SuppressWarnings("serial")

class Rectangle implements Shape, Cloneable {


  @Override

  publicdouble getGirth() {

    return 0;

  }


  @Override

  publicdouble getArea() {

    return 0;

  }

}

6.

package cn.tedu.interfacex;


publicclass InterfaceDemo2 {


  publicstaticvoid main(String[] args) {


    // A a = new B1();


    // 在Java中支持的是类和类之间的单继承

    // 所以此时会形成一棵继承结构树

    // 所以比较容易的就能确定两个类之间是否有继承关系

    // 因此在进行强制转换的时候

    // 会检查要转换的对象的声明类和转换的类型是否有继承关系

    // a对象的声明类型是A类,要转换的类型是B1

    // B1继承了A,所以在编译时期就不报错

    // 到了运行的时候才会检查对象的实际类型和要转换的类型是否一致

    // 运行的时候,发现a的实际类型是B1,要转换的类型是B1

    // 类型一致,允许转换

    // B1 b1 = (B1) a;


    // a对象的声明类型是A类,要转换的类型是B2

    // B2继承了A,所以在编译时期就不报错

    // 到了运行的时候,a的实际类型是B1,要转换的类型是B2

    // 类型不一致,所以报错 --- ClassCastException

    // B2 b2 = (B2) a;


    // b1对象的声明类型B1类,要转换的是B2

    // B2没有继承B1,编译就不通过

    // B2 b2 = (B2) b1;


    // C c = (C) a;


    // 在Java中,类和接口之间是多实现,接口和接口之间是多继承

    // 所以构成了一张图状结构 --- 网状结构

    // 不容易确定两个节点之间的关系

    // 因此在编译时期为了提高效率放弃检查

    // 直到运行的时候再确定类型是否相同

    // D d = (D) a;


    // C c = new C();

    // D d = (D) c;


  }


}


class A {

}


class B1 extends A {

}


class B2 extends A {

}


class C {

}


interface D {

}

7、

package cn.tedu.interfacex;


publicclass InterfaceDemo3 {


  publicstaticvoid main(String[] args) {

   Ee=new E();

  Calcc=(i,j)->i+j;

  Calcd=(doublei,doublej)-> {

    returni+j;

    };

   System.out.println(e.max(3, 7));

  }

}

// 如果一个接口中只定义了一个抽象方法,那么把这个接口声明为函数式接口,接口中的默认方法 --- 默认是public

// 函数式接口用@FunctionalInteface

@FunctionalInterface

interface Calc {

  publicdouble add(doublei, doublej);//抽象类中的方法定义方式

  publicdefaultdouble max(doublei, doublej) {

    returni>j ? i : j;

  }

  publicstaticdouble min(doublei, doublej) {

    returni

  }

}

class E implementsCalc  {

  @Override

  publicdouble add(doublei, doublej) {

    returni + j;

  }


}

8、

package cn.tedu.lambda;


publicclass LambdaDemo {


  publicstaticvoid main(String[] args) {


    // 接口中只定义了1个抽象方法

    // 可以利用Lambda表达式来重写这唯一的一个抽象方法

    //Calcc = newCalc(){

    //

    // @Override

    // public double add(double i,

double j) {

    // return i + j;

    // }

    //

    // };


    // 表示重写Calc中的唯一的一个抽象方法add

    // Lambda表达式只能作用在函数式接口上

    //Calcc = (double a,double b) -> {

    // return a + b;

    // };

    // 方法体只有一句,可以省略{}和return不写

    // 唯一的一句方法体的计算结果默认为当前方法的返回值

    //Calcc = (double i,double j) -> i + j;


    // 重写的是Calc接口中的方法add

    // add方法的参数列表的类型是已知的

    // 可以省略参数类型不写

    // 函数式编程

    Calcc = (x, y) ->x + y;


    System.out.println(c.add(5.8, 9.47));


  }


}


interface Calc {


  double add(doublei, doublej);


}

9、

package cn.tedu.lambda;


publicclass LambdaDemo2 {


  publicstaticvoid main(String[] args) {


    // 方法体只有1句

    // 这一句方法体是直接操作参数

    // 这一句方法体是调用了已有类Math中的静态方法sqrt

    // 参数列表可以省略

    // Calculator c = d ->

Math.sqrt(d);

    Calculatorc = Math::sqrt;

    System.out.println(c.sqrt(9));


  }


}


interface Calculator {


  double sqrt(doubled);


}package cn.tedu.lambda;


publicclass LambdaDemo2 {


  publicstaticvoid main(String[] args) {


    // 方法体只有1句

    // 这一句方法体是直接操作参数

    // 这一句方法体是调用了已有类Math中的静态方法sqrt

    // 参数列表可以省略

    // Calculator c = d ->

Math.sqrt(d);

    Calculatorc = Math::sqrt;

    System.out.println(c.sqrt(9));


  }


}


interface Calculator {


  double sqrt(doubled);


}

10、

packagecn.tedu.packagex;


// * 通配符

// * 表示导入当前包下的所有的类但是不包括子包下的类

importjava.util.*;


public classPackageDemo {


  @SuppressWarnings("resource")

  public static void main(String[] args) {


    Scanner s = new Scanner(System.in);

    int n = s.nextInt();


    int[] arr = new int[n];

    for (int i = 0; i < n; i++) {

      arr[i] = s.nextInt();

    }


    Arrays.sort(arr);


  }


}


11、

package cn.tedu.reiew;


publicclass StaticDemo {


  publicstaticvoid main(String[] args) {


    System.out.println(A.i);

    System.out.println(A.j);


  }


}


class A {


  // 先加载a,i,j

  // 其中a先标记为null,i和j标记为0

  // 进行初始化操作

  // 先执行 static A a = new A();

  // 这个时候a的具体地址来覆盖掉标记的null

  // a的实际对象是存在堆内存中,然后方法区中存储a的地址

  // 创建a对象那么就得执行a的构造方法

  // 意味着要执行i++,j++,在创建对象将标记值先认为是默认值

  // i->1 j->1

  // a的构造方法执行完成,然后初始化i和j

  // i = 5;利用实际数据5覆盖掉标记值1

  // 注意:在类加载过程中不允许计算标记值/

  // 但是在创建对象或者其他方式使用的时候会将标记值认为是默认值使用

  static A a = new A();

  staticinti = 5;

  staticintj;


  public A() {

    i++;

    j++;

    System.out.println(i);

    System.out.println(j);

  }


}


DAY 10

1.

package cn.tedu.reiew;


publicclass StaticDemo {


  publicstaticvoid main(String[] args) {


    System.out.println(A.i);

    System.out.println(A.j);


  }


}


class A {


  // 先加载a,i,j

  // 其中a先标记为null,i和j标记为0

  // 进行初始化操作

  // 先执行 static A a = new A();

  // 这个时候a的具体地址来覆盖掉标记的null

  // a的实际对象是存在堆内存中,然后方法区中存储a的地址

  // 创建a对象那么就得执行a的构造方法

  // 意味着要执行i++,j++,在创建对象将标记值先认为是默认值

  // i->1 j->1

  // a的构造方法执行完成,然后初始化i和j

  // i = 5;利用实际数据5覆盖掉标记值1

  // 注意:在类加载过程中不允许计算标记值/

  // 但是在创建对象或者其他方式使用的时候会将标记值认为是默认值使用

  static A a = new A();

  staticinti = 5;

  staticintj;


  public A() {

    i++;

    j++;

    System.out.println(i);

    System.out.println(j);

  }


}

2.

package cn.tedu.object;


publicclass ObjectDemo2 {


  publicstaticvoid main(String[] args) {


    // hashCode --- 哈希码

    // 哈希码是根据哈希散列算法计算出来的

    // 哈希散列算法保证同一个类产生的对象的哈希码能够是散列且比较均匀的分布在43亿值上

    // 产生重合的概率是相对较小的,因此人为的认为对象的哈希码是唯一的

    // 在存储对象的时候会将对象的哈希码作为元素的内存地址表示

    // System.out.println(new

Object().hashCode());

    // System.out.println(new

Object().hashCode());

    // System.out.println(new

Object().hashCode());

    // System.out.println(new

Object().hashCode());


    Objecto = newObject();

    // 默认是打印对象的 类型@地址 的形式

    System.out.println(o.toString());

    // 直接打印对象是在底层调用了对象的toString

    System.out.println(o);


    Students = new Student();

    System.out.println(s);


  }


}


class Student {


  private String name;

  privateintage;


  public String getName() {

    returnname;

  }


  publicvoid setName(String name) {

    this.name = name;

  }


  publicint getAge() {

    returnage;

  }


  publicvoid setAge(intage) {

    this.age = age;

  }


  @Override

  public String toString() {

    return"Student [name=" + name + ", age=" + age + "]";

  }


  // @Override

  // public String toString() {

  // return "name:" +name + "\r\nage:" + age;

  // }


}

3.

package cn.tedu.object;


publicclass ObjectDemo3 {


  publicstaticvoid main(String[] args) {


    Personp1 = newPerson();

    // p1.name = "Amy";

    p1.name = null;

    p1.age = 15;

    p1.gender = '女';


    Personp2 = newPerson();

    p2.name = null;

    p2.age = 15;

    p2.gender = '女';


    // 底层默认使用==进行比较 --- 比较两个对象的地址是否一样

    System.out.println(p1.equals(p2));


  }


}


classPerson {


  Stringname;

  intage;

  chargender;


  @Override

  publicboolean equals(Object obj) {


    // 判断地址是否一样

    if (this == obj)

      returntrue;


    // 判断参数是否为空

    if (obj == null)

      returnfalse;


    // 判断类型是否一致

    if (this.getClass() != obj.getClass())

      returnfalse;


    // 判断属性值

    Personp = (Person) obj;


    // 判断属性age是否一致

    if (this.age != p.age)

      returnfalse;


    // 判断属性gender是否一致

    if (this.gender != p.gender)

      returnfalse;


    // 判断属性name是否一致

    if (this.name == null) {

      if (p.name != null)

       returnfalse;

    }elseif (!this.name.equals(p.name))

      returnfalse;


    returntrue;

  }


}

4.

package cn.tedu.object;


publicclass ObjectDemo4 {


  publicstaticvoid main(String[] args) {


    // 不是内部类,不会产生class文件

    //Calcc = (i, j) -> i+ j;

    Objectc = (Calc) (i, j) ->i + j;


    // Object c = newCalc() {

    //

    // @Override

    // public double add(double i,

double j) {

    // return i + j;

    // }

    // };

    System.out.println(c.getClass());


  }


}


interface Calc {


  publicdouble add(doublei, doublej);


}

5.

package cn.tedu.object;


publicclass ObjectExer {


}


class User {


  private String username;

  private String password;


  public String getUsername() {

    returnusername;

  }


  publicvoid setUsername(String username) {

    this.username = username;

  }


  public String getPassword() {

    returnpassword;

  }


  publicvoid setPassword(String password) {

    this.password = password;

  }


  // hashCode的重写:

  // 如果两个对象equals为true,那么哈希码一致

  // 不同对象的哈希码要不同并且要做到散列分布

  @Override

  publicint hashCode() {

    finalintprime = 31;

    intresult = 1;

    result = prime * result + ((password == null) ? 0 : password.hashCode());

    result = prime * result + ((username == null) ? 0 : username.hashCode());

    returnresult;

  }


  @Override

  publicboolean equals(Object obj) {


    if (this == obj)

      returntrue;


    if (obj == null)

      returnfalse;


    if (getClass() != obj.getClass())

      returnfalse;


    Userother = (User) obj;


    if (password == null) {

      if (other.password != null)

       returnfalse;

    }elseif (!password.equals(other.password))

      returnfalse;


    if (username == null) {

      if (other.username != null)

       returnfalse;

    }elseif (!username.equals(other.username))

      returnfalse;


    returntrue;

  }


  // @Override

  // public boolean equals(Objectobj){

  //

  // // 判断地址是否一致

  // if (this ==obj)

  // return true;

  //

  // // 判断参数是否为空

  // if (obj== null)

  // return false;

  //

  // // 判断类型是否一致

  // if (this.getClass() !=

obj.getClass())

  // return false;

  //

  // // 判断属性是否一致

  // User user = (User)obj;

  //

  // if (this.username == null) {

  // if (user.username != null)

  // return false;

  // } else if

(!this.username.equals(user.username))

  // return false;

  //

  // if (this.password == null) {

  // if (user.password != null)

  // return false;

  // } else if

(!this.password.equals(user.password))

  // return false;

  //

  // return true;

  //

  // }


}

6.package cn.tedu.string;


publicclass StringDemo {


  publicstaticvoid main(String[] args) {


    // Stringstr= "abc";

    //str= "def";

    // System.out.println(str);


    // s1指向方法区

    Strings1 = "ab";

    // s2指向堆内存,堆内存指向方法区

    Strings2 = new String("ab");

    // 字符串在方法区中只存放一份

    // 后续使用到值相等的字符串的时候使用的是同一个

    Strings3 = "ab";

    // "a"和"b"是两个字面量

    // 字面量在参与运算的时候为了提高效率在编译时期就会运算

    // String s4 = "a" +"b"; -> String s4 = "ab";

    Strings4 = "a" + "b";

    // s5 = s5 + "b";不会优化

    // 实际上底层是利用了StringBuilder中的append方法来完成

    Strings5 = "a";

    // s5 = new

StringBuilder(s5).append("b").toString();

    // s5最后指向堆内存,堆内存再指向方法区

    s5 = s5 + "b";


    System.out.println(s1 == s2);

    System.out.println(s1 == s3);

    System.out.println(s1 == s4);

    System.out.println(s1 == s5);

    System.out.println(s2 == s5);


  }


}

7.

package cn.tedu.string;


publicclass StringDemo2 {


  publicstaticvoid main(String[] args) {


    // 表示获取从1970-01-01 00:00:00到现在的毫秒值

    longbegin= System.currentTimeMillis();


    // Stringstr="";

    // for (inti = 0; i <100000; i++) {

    //str+= "a";

    // }


    StringBuildersb = newStringBuilder();

    for (inti = 0; i< 100000000; i++) {

      sb.append("a");

    }

    // Stringstr=sb.toString();


    longend= System.currentTimeMillis();

    System.out.println(end - begin);

  }


}

8.

package cn.tedu.string;


publicclass StringDemo3 {


  publicstaticvoid main(String[] args) {


    // Stringstr= "vhaovldasnoasbo";


    // 获取字符串的长度

    //

System.out.println(str.length());


    // 通过指定的下标获取字符串对应位置上的字符

    // char c = str.charAt(4);

    // System.out.println(c);


    // 将字符串转化为字符数组形式

    // char[]cs=str.toCharArray();

    // for (char c :cs) {

    // System.out.println(c);

    // }


    char[] cs = { 'd', 'e', 'g', 'y', 'c', 'r', 'n', 'u', 'a', 'u' };


    // 将字符数组转化为字符串

    // Stringstr= newString(cs);


    Stringstr = new String(cs, 3, 5);

    System.out.println(str);

  }


}

9.

package cn.tedu.string;


import java.util.Scanner;


publicclass StringExer {


  @SuppressWarnings("resource")

  publicstaticvoid main(String[] args) {


    Scanners = new Scanner(System.in);

    Stringstr = s.next();

    intbegin = s.nextInt();

    intend = s.nextInt();


    Stringsub = subString(str, begin, end);

    System.out.println(sub);

  }


publicstatic String subString(String str, intbegin, intend) {


    // 判断参数是否为空

    if (str == null)

      returnnull;


    // 判断起始下标

    if (begin< 0 || begin>= str.length() || begin>end)

      returnnull;


    // 判断结束下标

    if(end< 0 || end>= str.length())

      returnnull;


    char[] cs = str.toCharArray();

    Stringsub = new String(cs, begin, end - begin);


    returnsub;


  }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270