一、this关键字
1.this的作用:
·this表示的是当前对象本身,
·更准确地说,this代表当前对象的一个引用。
2.普通方法中使用this
·区分类成员属性和方法的形参
·调用当前对象的其他方法(可以省略)
·位置:任意
3.构造方法中使用this
·使用this来调用其它构造方法
·位置:必须是第一条语句
4.this不能用于static方法。(讲完static,大家就知道为什么了!)
5.this测试代码
- ublic class TestThis {
- int a,b,c;
- TestThis(){
- System.out.println("正要new一个Hello对象");
- }
- TestThis(int a,int b){
- //Hello();// //这样是无法调用构造方法的!
- this(); //调用无参的构造方法,并且必须位于第一行!
- a = a;//这里都是指的局部变量而不是成员变量
- this.a = a;//这样就区分了成员变量和局部变量. 这种情况占了this使用情况的大多数!
- this.b = b;
- }
- TestThis(int a,int b,int c){
- this(a,b);//调用无参的构造方法,并且必须位于第一行!
- this.c = c;
- }
- void sing(){
- }
- void chifan(){
- this.sing();//sing();
- System.out.println("你妈妈喊你回家吃饭!");
- }
- public static void main(String[] args){
- TestThis hi = new TestThis(2,3);
- hi.chifan();
- }
- }
6. 课堂demo【重点掌握】
- package netclass02;
-
- /**
- * @Auther: Yu Panpan
- * @Date: 2022/1/7 - 01 - 07 - 14:24
- * @Description: netclass02
- * @version: 1.0
- */
-
- /*
- * this:表示当前对象的指针
- * 指向当前对象,表示当前对象的引用
- * 用途:
- * 1、构造方法,当构造方法中的参数名称跟类的成员变量名称一样的时候,可以使用this代表当前对象
- * 注意:有了this之后,就可以将构造方法的参数跟成员变量保持一致(符合命名规范)
- * 当构造方法中需要调用其他的构造方法时,可以使用this(参数列表)调用其他构造方法,但是必须位于方法体的第一行(规则)
- * 2、普通方法中:
- * 当多个普通方法之间需要相互调用的时候,可以使用this来进行调用,指的是当前对象的其他方法
- * 3、调用成员变量的时候如何使用:
- * 当方法中的参数名称跟成员变量保持一致的时候,使用 this.变量名称 表示的是对象的值,而使用变量名称表示形参列表中的值
- */
- public class ThisDemo {
-
- //成员变量
- String name;
- int age;
-
- //构造器、构造方法
- public ThisDemo(){
-
- }
-
- public ThisDemo(String name){
- this.name = name;
- }
-
- public ThisDemo(String name,int age){
- // this.name = name;
- this(name);
- this.age = age;
- }
-
- public void test1(){
- System.out.println("test1被执行");
- // test2(); //也可以省略this来调用其他方法
- }
-
- public void test2(String name){
- System.out.println("test2被执行");
- this.test1();
- System.out.println(name);
- System.out.println(this.name);
- }
-
- public static void main(String[] args) {
- ThisDemo td = new ThisDemo("张飞",20);
- // System.out.println(td.name);
- // System.out.println(td.age);
- // td.test1();
- td.test2("赵云");
- }
- }
二、static关键字
1.在类中,用static声明的成员变量为静态成员变量 ,或者叫做: 类属性,类变量.
·它为该类的公用变量,属于类,被该类的所有实例共享,在类被载入时被显式初始化
·对于该类的所有对象来说,static成员变量只有一份。被该类的所有对象共享
·可以使用”对象.类属性”来调用。不过,一般都是用“类名.类属性”
·static变量置于方法区中
2.用static声明的方法为静态方法
·不需要对象,就可以调用(类名.方法名)
·在调用该方法时,不会将对象的引用传递给它,所以在static方法中不可访问非static的成员
·静态方法不能以任何方式引用this和super关键字
3.static示例代码
- public class TestStatic {
- int a;
- static int width;
- static void gg(){
- System.out.println("gg");
- }
- void tt(){
- System.out.println("tt");
- }
- public static void main(String[] args){
- TestStatic hi = new TestStatic();
- TestStatic.width = 2;
- TestStatic.gg(); //gg();
- //通过引用也可以访问static变量或static方法。不过,一般还是使用类名.static成员名来访问。
- hi.gg();
- gg();
- }
- }
- static关键字
- 使用static声明的成员变量称为静态变量,
- 使用static声明的方法称为静态方法
- 静态变量和静态方法又称为类变量和类方法
- static关键字用法-课堂demo【重点掌握】
- package netclass02;
-
- /**
- * @Auther: Yu Panpan
- * @Date: 2022/1/7 - 01 - 07 - 15:17
- * @Description: netclass02
- * @version: 1.0
- */
- /*
- * 使用static统计在类中一共产生多少个对象?
- */
- public class StaticDemo2 {
-
- static int count;
- // int count;
-
- public StaticDemo2(){
- count++;
- System.out.println("创建了" +count + "个对象");
- }
-
- public static void main(String[] args) {
- new StaticDemo2();//1
- new StaticDemo2();//2
- new StaticDemo2();//3
- }
- }
三、代码块
1.概念:使用”{}”括起来的一段代码
2.分类:根据位置可分类
·普通代码块—>直接在方法或语句中定义的代码块
·构造代码块—>直接写在类中的代码块
·静态代码块—>使用static声明的代码块
·同步代码块—>多线程的时候会学到
3.静态初始化块
·如果希望加载后,对整个类进行某些初始化操作,可以使用static初始化块。
·类第一次被载入时先执行static代码块;类多次载入时,static代码块只执行一次;static经常用来
·进行static变量的初始化。
·是在类初始化时执行,不是在创建对象时执行。
·静态初始化块中不能访问非static成员。
·demo
- public class TestStaticBlock {
- static {
- System.out.println("此处,可执行类的初始化工作!");
- }
- public static void main(String[] args) {
- System.out.println("main方法中的第一句");
- }
- }
4. 课堂demo【重点掌握】
- package netclass02;
-
- /**
- * @Auther: Yu Panpan
- * @Date: 2022/1/7 - 01 - 07 - 15:34
- * @Description: netclass02
- * @version: 1.0
- */
-
- /*
- * 代码块: 使用{}括起来的一段代码叫做代码块
- * 分类:
- * 普通代码块: 定义在方法中,使用{}括起来的代码叫做普通代码块
- * 构造代码块:定义在类中的使用{}括起来的代码叫做构造代码块
- * 注意:每次代码运行的时候会将构造代码块中的代码添加到构造方法的前面
- * 构造代码块中的代码会添加到每一个构造方法中,当使用this(参数)的时候不会添加
- * 静态代码块:使用static{}括起来的代码叫做静态代码块,在程序载入的时候优先执行
- * 数据库连接等其他提前需要准备好的代码会放在static代码块
- * 同步代码块:
- * 在使用多线程的时候会使用,用来给共享空间进行加锁操作(后面讲)
- * 执行顺序:静态代码块--》构造代码块(创建对象的时候才会用到)--》普通代码块
- */
- public class CodeBlockDemo {
-
- int x;
- int y;
-
- {
- System.out.println("构造代码块");
- }
-
- static{
- System.out.println("static代码块");
- // x = 100;
- }
-
- public CodeBlockDemo(){
-
- }
- public CodeBlockDemo(int x){
- System.out.println("CodeBlockDemo(int x)被执行了");
- this.x = x;
- }
- public CodeBlockDemo(int x,int y){
- this(x);
- System.out.println("CodeBlockDemo(int x,int y)被执行了");
- // this.x = x;
- this.y = y;
- }
- public void test1(){
- System.out.println("test1方法被执行");
- {
- System.out.println("test1方法中的普通代码块");
- }
- }
-
- public synchronized void test2(){
- //定义一个同步代码块(先见个面,讲到多线程的时候再详细讲。)
- // synchronized(this){
- //
- // }
- }
-
- public static void main(String[] args) {
- CodeBlockDemo cbd = new CodeBlockDemo();
- cbd.test1();
- {
- System.out.println("main方法中的普通代码块");
- }
- CodeBlockDemo cbd2 = new CodeBlockDemo(1,2);
- }
- }
-
四、package
1.为什么需要package?
·为了解决类之间的重名问题。
·为了便于管理类:合适的类位于合适的包
2.package怎么用?
·通常是类的第一句非注释性语句。
·包名:域名倒着写即可,再加上模块名,并不内部管理类。
3.注意事项:
·写项目时都要加包,不要使用默认包。
·com.tensent和com.tensent.oa,这两个包没有包含关系,是两个完全独立的包。只逻辑上看起来
·后者是前者的一部分。
4.JDK中的主要包
·java.lang
·包含一些Java语言的核心类,如String、Math、Integer、System和Thread,提供常用功能。
·java.awt
·包含了构成抽象窗口工具集(abstract window toolkits)的多个类,这些类被用来构建和管理应用·程序的图形用户界面(GUI)。
·java.net
·包含执行不网络相关的操作的类。
·java.io
·包含能提供多种输入/输出功能的类。
·java.util
·包含一些实用工具类,如定义系统特性、使用不日期日历相关的函数。
5. 课堂demo【重点掌握】
- package netclass02;
-
- import java.util.Date;
- import java.util.Scanner;
-
- /**
- * @Auther: Yu Panpan
- * @Date: 2022/1/7 - 01 - 07 - 16:24
- * @Description: netclass02
- * @version: 1.0
- */
-
- /*
- * package:包,对应到文件系统就是多级目录
- * 为了解决两个问题:
- * 1、文件同名问题
- * 2、为了方便管理类,将具体处理功能的代码放到同一个目录下
- * 使用:
- * 一般定义package会放置在java文件的第一行
- * package 域名的倒写
- * 比如:www.taobao.com
- * 包名:com.taobao.
- * package com.tensent.项目名称
- * 包名的命名规则:
- * 企业性质.企业名称.项目名称.模块名称. 子模块名称....
- * 企业性质:com org edu gov
- * 企业名称:alibaba baidu huawei tensent
- * 项目名称:oa erp hrms crm
- * 模块名称:ums--userManagementSystem 用户管理系统
-
原文:https://blog.csdn.net/weixin_64094228/article/details/122379426