this关键字 static关键字 代码块 心已赠人 2021-11-05 07:52 360阅读 0赞 #### this关键字 #### 为什么需要学习this? 1.this 代表 现实世界 代词 “我的” 的概念 2.当成员变量和局部变量重名的时候使用 this this的概述: this代指当前对象,本质就是一个地址,变量,和当前对象指向同一个堆区的空间 this的特点: 1.每次new一个对象,都会创建一个变量 this指向同一堆区的空间 2.this代指当前对象,谁new或者shei调用方法,this就代指谁 3.this只能够出现类的内部类 4.static上下文中不能够出现this或者super(后面讲解 static和继承 的时候详细讲解) 5.默认在类的内部访问成员默认可以省略this 应用场景: 1.当局部变量和成员变量重名 2.当需要在类的内部访问类的成员 3.访问本类构造方法的时候可以使用this (后面讲解构造方法的时候详细讲解) #### 为什么需要学习static关键字? #### 多个对象共享同一分数据,可以使用static修饰 static的特点: 1.静态变量属于类的,不属于具体的对象 2.访问静态成员的方式: The static field Student.nationality should be accessed in a static way 1.通过对象访问静态成员 (不建议) 2.通过类访问 (推荐) 3.静态变量又称为 类变量,不使用static修饰的变量成为非静态变量 4.静态即可以修饰成员变量,还可以修饰方法 static 修饰方法的好处 可以方便外界在不创建对象的情况下直接调用方法,执行功能 static 修饰方法常用来编写工具类 Utils Objects Arrays 工具类的编写步骤: a. 构造方法私有 b. 所有成员方法添加 static 关键字 5.静态环境下不能够访问非静态变量,非静态环境下可以访问静态变量 Cannot make a static reference to the non-static field name 6.静态修饰的变量在类加载的时候加载到方法区的静态区 7.静态还可以修饰 代码块 和 类(内部类) public class StaticDemo01 { public static void main(String[] args) { // Student s1 = new Student("张三", 18, "中国"); // Student s2 = new Student("李四", 18, "中国"); // Student s3 = new Student("王五", 19, "中国"); Student.nationality = "中国"; Student s1 = new Student("张三", 18); Student s2 = new Student("李四", 18); Student s3 = new Student("王五", 19); Student.nationality = "美国"; s1.show(); s2.show(); s3.show(); Student.staticMethod(); } } class Student { String name; int age; static String nationality; // 国籍 public Student() { super(); } public Student(String name, int age) { super(); this.name = name; this.age = age; } public Student(String name, int age, String nationality) { super(); this.name = name; this.age = age; this.nationality = nationality; } public void show() { System.out.println("Student [name=" + name + ", age=" + age + ", nationality=" + nationality + "]"); } public static void staticMethod() { System.out.println("Student.staticMethod()"); // System.out.println(name); } static class InnerClass { } } #### 代码块: 使用 \{\} 包裹起来的代码就称为代码块 #### 代码块的分类: 1.构造代码块: 定义在成员位置,用于抽取多个构造方法中的共性代码,从而达到简化代码的目的 执行时机: 每次访问构造方法之前执行,可以执行多次 2.局部代码块: 定义在局部位置,方法体内,限定局部变量的作用域 3.静态代码块: 使用 static 修饰的代码块 静态代码块无论创建多少个对象,都是优于构造方法和构造代码块之前执行,并且只执行一次 执行时机: 在类加载的时候执行 类加载时机: 类中的成员第一使用的时候 执行顺序: 静态代码块 > 构造代码块 > 构造方法 4.同步代码块: 多线程后面讲解 public class CodeBlockDemo01 { public static void main(String[] args) { CodeBlock.num3 = 200; CodeBlock cb = new CodeBlock(); CodeBlock cb2 = new CodeBlock(1); CodeBlock cb3 = new CodeBlock(1,2); } static { System.out.println("我是 CodeBlockDemo01 类的静态代码块"); } } class CodeBlock { int num; int num2; static int num3; // 1.构造代码块 { System.out.println("创建对象之前做的准备工作1: " + getSum(1, 100)); } { System.out.println("创建对象之前做的准备工作2: " + getSum(1, 100)); } { System.out.println("创建对象之前做的准备工作3: " + getSum(1, 100)); } public CodeBlock() { System.out.println("无参"); } public CodeBlock(int num) { System.out.println("带1参"); this.num = num; } public CodeBlock(int num, int num2) { System.out.println("带2参"); this.num = num; this.num2 = num2; } static { System.out.println("我是 CodeBlock 类的静态代码块1"); } static { System.out.println("我是 CodeBlock 类的静态代码块2"); } static { System.out.println("我是 CodeBlock 类的静态代码块3"); } public void show() { { int num =30; } int num = 10; System.out.println(num); synchronized (new Object()) { } } public int getSum(int m, int n) { int sum = 0; for (int i = m; i <= n; i++) { sum += i; } return sum; } }
还没有评论,来说两句吧...