【JavaSE学习笔记】面向对象

xiaoxiao2021-02-27  193

面向对象

A.static关键字

1)static关键字(静态的,共享的

定义一个“人”类:针对以下这个代码,提供个三个参数的构造方法

发现每次创建对象的时候,都要给country赋值

这样的话在堆内存消耗内存就比较大!

针对这种情况:当多个对象需要公用一个成员变量或者成员方法的时候

给他们前面用static关键字修饰

//Person基本类 class Person { // 成员变量 String name; // 姓名 int age; // 年龄 static String country; // 国籍 // 提供又残的构造方法:两个参数的构造方法 public Person(String name, int age) { this.name = name; this.age = age; } // 提供三个参数的构造方法 public Person(String name, int age, String country) { this.name = name; this.age = age; this.country = country; } // 成员方法:show():将成员输出出来 public void show() { System.out.println(name + "--->" + age + "--->" + country); } } // 测试类 public class Demo01 { public static void main(String[] args) { // 创建对象 Person p1 = new Person("张三", 20, "中国"); p1.show(); // 创建第二个对象 Person p2 = new Person("李四", 30); p2.show(); // 创建第三个对象 Person p3 = new Person("王五", 25); p3.show(); System.out.println("------------------"); p3.country = "美国"; p1.show(); p2.show(); p3.show(); } }自行测试

2)static关键字的特点: a.static随着类的加载而加载 b.优先于对象的存在,共享的\共用的 c.使用对象名可以去访问成员变量,如果这个成员变量被static修饰,可以被类名直接调用 class Demo{ //非静态成员变量 int num = 10; //静态成员变量 static int num2 = 20; //成员方法 public static void show() { System.out.println("show"); } } // 测试类 public class Demo01 { public static void main(String[] args) { //创建对象 Demo d = new Demo(); System.out.println(d.num); System.out.println(d.num2); //使用对象名调用成员变量 System.out.println(Demo.num2); //使用类名直接调用成员变量 System.out.println("------------------"); //show方法被static修饰 //第一种:使用对象名调用成员方法 d.show(); //第二种:直接使用类名 Demo.show(); } }

3)static关键字的注意事项

a.在静态方法中是没有this关键字

         静态随着类的加载而加载,this代表当前累的对象,它随着对象的创建而存在

b.静态的成员

         静态成员变量:在静态成员方法中,只能访问静态的成员变量

         静态成员方法:只能访问静态(成员方法/成员变量)

简单记:静态只能访问静态

非静态的成员方法:

          里面可以访问静态的成员(成员变量/成员方法)

B.代码块的概述与应用

1)java中,用{}括起来的代码,叫代码块

局部代码块:用来限定变量的生命周期,出现在main方法中

构造代码块:类中的成员位置,{代码块}用来给对象的数据进行初始化

                      每次在执行构造方法之前,要执行构造代码块

静态代码块:类中的成员位置,也是用{代码块},在代码的前面,用static代码

         作用:静态是和类有关系的,随着类的加载而加载,给类进行初始化

         在类中:只加载一次

class Demo{ //静态代码块 static{ int a = 1000; System.out.println(a); System.out.println("静态代码块"); } //构造代码块 { int x = 100; System.out.println(x); System.out.println("构造代码块"); } //构造方法 public Demo() { System.out.println("无参构造方法"); } public Demo(int a) { System.out.println("有参构造方法"); } //构造代码块 { int y = 200; System.out.println(y); System.out.println("构造代码块"); } //静态代码块 static{ int b = 2000; System.out.println(b); System.out.println("静态代码块"); } } // 测试类 public class Demo01 { public static void main(String[] args) { //局部代码块 { int x = 100; System.out.println(x); System.out.println("局部代码块"); } System.out.println("---------"); //创建对象 Demo d1 = new Demo(); System.out.println("----------"); Demo d2 = new Demo(); System.out.println("----------"); Demo d3 = new Demo(1); } }

2)面试题:

静态代码块,构造代码块,构造方法的执行顺勋?

       静态代码块--->构造代码块--->构造方法

C.API文档的使用与制作

1)如何使用

        打开API---->显示---->索引---->搜索你要学习的类

     Math类

              简单看该类的一个介绍:看版本号

                  构造方法------>构造方法摘要

                  成员变量------>字段摘要

                  成员方法------>方法摘要

     这个类属于哪一个包下的类,在java中除了java.lang包不用代码中导包,其他类都需要导包

      举例:Math

这个类是java.lang包下的

版本从1.0开始

字段摘要,方法摘要

Math类中没有构造方法,但所有方法都被static修饰,可以直接被类名调用

2)如何制作API

a.定义一个数组工具类:ArrayTool

数组工具类中的注释:使用文档注释,解释每一个方法的参数是什么意思

工具类中提供的一些方法:

遍历数组/求最大值/查找数组中的某个元素第一次在数组中出现的索引

/** * 这是数组的一个工具类 * * @author 张三 * @version V1.0 */ public class ArrayTool { /** * 这是数组工具类的无参构造方法 */ // 将无参构造方法私有化,为了不让外界创建对象,所以该类中的方法用static修饰 private ArrayTool() { } /** * 这是遍历数组的方法:遍历后,结果是:{元素1,元素2.....} * * @param arr:需要被遍历的数组 */ public static void printArray(int[] arr) { System.out.print("{"); for (int i = 0; i < arr.length; i++) { if (i == arr.length - 1) { System.out.println(arr[i] + "}"); } else { System.out.print(arr[i] + ","); } } } /** * 这是求数组中最大值的方法 * * @param arr:需要被查询的数组 * @return 返回的就是我们需要查找的最大值 */ public static int getMax(int[] arr) { int max = arr[0]; // 遍历其他元素 for (int i = 1; i < arr.length; i++) { max = Math.max(max, arr[i]); } return max; } /** * 查询数组中的元素在数组中第一次出现的索引,如果查不到,返回-1 * * @param arr:需要查询的数组 * @param value:需要查询的元素 * @return 返回该元素在数组中第一次出现的索引值 */ public static int getIndex(int[] arr, int value) { int index = -1; for (int i = 0; i < arr.length; i++) { if (arr[i] == value) { index = i; break; } } return index; } }

b.在ArrayDemo这个类里面测试一下

//测试类 public class ArrayDemo { public static void main(String[] args) { // 定义一个数组 int[] arr = { 11, 22, 33, 44, 55 }; // 遍历数组 ArrayTool.printArray(arr); // 求数组中的最大值 int max = ArrayTool.getMax(arr); System.out.println("max:" + max); // 求33在数组中第一次出现的索引 int index = ArrayTool.getIndex(arr, 33); System.out.println("index:" + index); } }

c.在编辑完代码后:在工程目录里找到这两个类,复制(不要复制package)到别的文件夹里

d.打开dos控制台--->切换到当前盘符--->javadoc -d 目录名 -author -version ArrayTool

注意 -d是标准格式,并不是指所在盘符

加载完毕(若没有错误,警告无视)

若提示权限不够,则把class前的public去掉

进入目录

打开ArrayTool.html

转载请注明原文地址: https://www.6miu.com/read-16366.html

最新回复(0)