static关键字+静态代码块+继承及Arrays

比眉伴天荒 2024-04-09 02:01 224阅读 0赞

static关键字+静态代码块+继承及Arrays

一、static关键字

1.含义:静态的

2.作用:

1.静态属性
2.静态方法
3.静态代码

3.static属性

1.含义:该属性属于类属性,不属于任何的对象
2.静态变量的生命周期:
创建:类加载到方法区时,系统会扫描该类的静态变量,并把静态变量加载到静态区中
销毁:程序结束时销毁
3.应用场景:该类所有的对象都共享的变量,就使用静态属性
4.注意:static修饰属性,该属性属于类的属性,直接用类名调用
  1. public class Test01 {
  2. public static void main(String[] args) {
  3. A a1 = new A();
  4. A a2 = new A();
  5. a1.str1 = "aaa";
  6. a2.str1 = "bbb";
  7. System.out.println(a1.str1);//aaa
  8. System.out.println(a2.str1);//bbb
  9. //a1.str2 = "xxx";
  10. //a2.str2 = "yyy";
  11. //System.out.println(a1.str2);//yyy
  12. //System.out.println(a2.str2);//yyy
  13. A.str2 = "xxx";
  14. A.str2 = "yyy";
  15. System.out.println(A.str2);//yyy
  16. System.out.println(A.str2);//yyy
  17. }
  18. }
  19. public class A {
  20. //属性/全局变量:成员变量 和 静态变量
  21. //成员变量
  22. String str1;
  23. //静态变量
  24. static String str2;
  25. }

二、Arrays工具类包

  1. import com.qf.arrays.MyArrays;
  2. public class Test01 {
  3. public static void main(String[] args) {
  4. int[] is = {
  5. 35,81,18,25,9,10,11,12,13};
  6. //排序 - 9,10,11,12,13,18,25,35,81
  7. MyArrays.sort(is);
  8. //搜索 - (注意:必须先排序,再查找,因为底层使用的是二分法查找)
  9. //返回元素在数组中的下标;如果元素不在数组中则返回 -插入点-1
  10. int index = MyArrays.binarySearch(is,35);
  11. System.out.println("搜索元素的下标为:" + index);
  12. //拷贝数组/扩容数组
  13. int[] newIs1 = MyArrays.copyOf(is,is.length*2);
  14. //拷贝数组(开始下标-包含,结束下标-不包含)
  15. int[] newIs2 = MyArrays.copyOfRange(newIs1,3,6);
  16. //替换
  17. MyArrays.fill(newIs2,888);
  18. //将数组转换为字符串
  19. String str = MyArrays.toString(newIs2);
  20. System.out.println(str);
  21. }
  22. }
  23. /**
  24. * 史上最牛逼的数组工具类
  25. * @author 高雷
  26. * @version 1.0
  27. */
  28. public class MyArrays {
  29. /**
  30. * 按照数字顺序排序指定的数组
  31. * @param a 按照数字顺序排列指定数组
  32. */
  33. public static void sort(int[] a) {
  34. for (int i = 0; i < a.length-1; i++) {
  35. for (int j = 0; j < a.length-1-i; j++) {
  36. if (a[j] > a[j+1]) {
  37. int temp = a[j];
  38. a[j] = a[j+1];
  39. a[j+1] = temp;
  40. }
  41. }
  42. }
  43. }
  44. /**
  45. * 使用二叉搜索算法搜索指定的int数组的指定值。
  46. * @param a 要搜索的数组
  47. * @param key 要搜索的值
  48. * @return 搜索键的索引,如果它包含在数组中; 否则, (-(insertion point) - 1)
  49. */
  50. public static int binarySearch(int[] a,int key) {
  51. int start = 0;
  52. int end = a.length-1;
  53. while (start <= end) {
  54. int mid = (start + end)/2;
  55. if (key > a[mid]) {
  56. start = mid +1;
  57. }else if(key < a[mid]){
  58. end = mid -1;
  59. }else{
  60. return mid;
  61. }
  62. }
  63. return - start-1;
  64. }
  65. /**
  66. * 将指定的int值分配给指定的int数组的每个元素。
  67. * @param a 要填充的数组
  68. * @param val 要存储在数组的所有元素中的值
  69. */
  70. public static void fill(int[] a,int val){
  71. for (int i = 0; i < a.length; i++) {
  72. a[i] = val;
  73. }
  74. }
  75. /**
  76. * 将指定的int值分配给指定的int数组的指定范围的每个元素。
  77. * @param a 要填充的数组
  78. * @param fromIndex 要用指定值填充的第一个元素(包括)的索引
  79. * @param toIndex 要用指定值填充的最后一个元素(排除)的索引
  80. * @param val 要存储在数组的所有元素中的值
  81. */
  82. public static void fill(int[] a,int fromIndex,int toIndex,int val){
  83. for (int i = fromIndex; i < toIndex; i++) {
  84. a[i] = val;
  85. }
  86. }
  87. /**
  88. * 复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。
  89. * @param original 要复制的数组
  90. * @param newLength 要返回的副本的长度
  91. * @return 原始数组的副本,被截断或用零填充以获得指定的长度
  92. */
  93. public static int[] copyOf(int[] original,int newLength){
  94. int[] is = new int[newLength];
  95. for (int i = 0; i < original.length; i++) {
  96. is[i] = original[i];
  97. }
  98. return is;
  99. }
  100. /**
  101. * 将指定数组的指定范围复制到新数组中。
  102. * @param original 要复制的数组
  103. * @param from 要复制的范围的初始索引(包括)
  104. * @param to 要复制的范围的最终索引,排他。 (该索引可能位于数组之外)
  105. * @return 一个包含原始数组的指定范围的新数组,用零截取或填充以获得所需的长度
  106. */
  107. public static int[] copyOfRange(int[] original, int from, int to){
  108. int[] is = new int[to-from];
  109. int index = 0;
  110. for (int i = from; i < to; i++) {
  111. is[index] = original[i];
  112. index++;
  113. }
  114. return is;
  115. }
  116. /**
  117. * 返回指定数组的内容的字符串表示形式。
  118. * @param a 要返回其字符串表示形式的数组
  119. * @return 一个字符串表示 a
  120. */
  121. public static String toString(int[] a){
  122. String str = "[";
  123. for (int i = 0; i < a.length; i++) {
  124. str += a[i];
  125. if (i != a.length-1) {
  126. str += ",";
  127. }
  128. }
  129. str += "]";
  130. return str;
  131. }
  132. }

三、静态代码块

1.构造方法的应用场景:初始化成员变量和静态变量

2.代码块:很少用

3.静态代码块:初始化静态变量

4.静态代码块为什么不能操作成员变量:

因为静态代码块是类加载到方法区时调用的,这时对象还没有创建
  1. public class Test01 {
  2. public static void main(String[] args) {
  3. A a1 = new A();
  4. A a2 = new A();
  5. }
  6. }
  7. public class A {
  8. String str1;//成员属性
  9. static String str2;//静态属性
  10. //静态代码块:类加载到方法区时调用
  11. //只能初始化静态属性
  12. static{
  13. str2 = "用真心待人";
  14. System.out.println("A类的静态代码块--" + str2);
  15. }
  16. //代码块:创建对象时优先于构造方法调用
  17. //可以初始化成员属性和静态属性
  18. {
  19. str1 = "xxx";
  20. str2 = "yyy";
  21. System.out.println("A类的代码块" + str1 + "--" + str2);
  22. }
  23. //构造方法:创建对象时调用
  24. //可以初始化成员属性和静态属性
  25. public A(){
  26. str1 = "aaa";
  27. str2 = "bbb";
  28. System.out.println("A类的构造方法" + str1 + "--" + str2);
  29. }
  30. }

四、继承

1.含义:子类继承父类的属性和方法

2.好处:解决代码的冗余

3.应用场景:分析多个类时发现,多个类有相同的属性和方法,就把相同的属性和方法抽取到父类中

需求:编写中国人和日本人的类

人类:

  • 属性:姓名、性别、年龄
  • 方法:吃饭饭、睡觉觉

中国人类 继承 人类:

  • 属性:身份证
  • 方法:打太极

日本人类 继承 人类:

  • 属性:年号
  • 方法:拍电影
  1. public class Test01 {
  2. public static void main(String[] args) {
  3. Chinese c = new Chinese();
  4. //设置父类的属性
  5. c.name = "张三丰";
  6. c.sex = '男';
  7. c.age = 120;
  8. //设置子类的属性
  9. c.id = "123456789";
  10. //获取父类的属性
  11. System.out.println(c.name);
  12. System.out.println(c.sex);
  13. System.out.println(c.age);
  14. //获取子类的属性
  15. System.out.println(c.id);
  16. //调用父类的方法
  17. c.eat();
  18. c.sleep();
  19. //调用子类的方法
  20. c.playTaiji();
  21. System.out.println("----------------------------");
  22. Japanese j = new Japanese();
  23. //设置父类的属性
  24. j.name = "波多";
  25. j.sex = '女';
  26. j.age = 18;
  27. //设置子类的属性
  28. j.yearNum = "昭和";
  29. //获取子类的属性
  30. j.yearNum = "昭和";
  31. //获取父类的属性
  32. System.out.println(j.name);
  33. System.out.println(j.sex);
  34. System.out.println(j.age);
  35. //获取子类的属性
  36. System.out.println(j.yearNum);
  37. //调用父类的方法
  38. j.eat();
  39. j.sleep();
  40. //调用子类的方法
  41. j.playVideo();
  42. }
  43. }
  44. //父类
  45. public class Person {
  46. String name;
  47. char sex;
  48. int age;
  49. public void eat(){
  50. System.out.println("吃饭饭");
  51. }
  52. public void sleep(){
  53. System.out.println("睡觉觉");
  54. }
  55. }
  56. // 子类 继承 父类
  57. public class Chinese extends Person {
  58. String id;
  59. public void playTaiji(){
  60. System.out.println("打太极");
  61. }
  62. }
  63. public class Japanese extends Person {
  64. String yearNum;
  65. public void playVideo(){
  66. System.out.println("拍视频");
  67. }
  68. }

发表评论

表情:
评论列表 (有 0 条评论,224人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java基础:static静态代码

    在Java中,静态代码块(static block)是在类加载时执行的,而不是在每次创建对象时执行的。当类被加载时,静态代码块会按照在类中出现的顺序被执行一次。这意味着无论创建