【Java】this、static、代码块、包

发布于:2024-05-19 ⋅ 阅读:(28) ⋅ 点赞:(0)

this关键字 用法:

(1)this可以修饰属性:

总结:当属性名字和形参发生重名的时候,或者  属性名字 和局部变量重名的时候,都会发生就近原则,所以如果我要是直接使用变量名字的话就指的是离的近的那个形参或者局部变量,这时候如果我想要表示属性的话,在前面要加上:this.修饰

如果不发生重名问题的话,实际上你要是访问属性也可以省略this.

  1. package com.star4;
  2. /**
  3.  * @Auther: Starshine
  4.  */
  5. public class Person {
  6.     //属性
  7.     int age;
  8.     String name;
  9.     double height;
  10.     //空构造器
  11.     public Person(){
  12.     }
  13.     //有参构造器
  14.     public Person(int age,String name,double height){
  15.         this.age = age;
  16.         this.name = name;
  17.         this.height = height;
  18.     }
  19.     //方法:
  20.     public void eat(){
  21.         int age = 10;
  22.         System.out.println(age);//就近原则,age指的是离它近的age--》局部变量的age
  23.         System.out.println(this.age);//这里指代的就是属性的age
  24.         System.out.println("我喜欢吃饭");
  25.     }
  26. }

(2)this修饰方法:

总结:在同一个类中,方法可以互相调用,this.可以省略不写。

  1. package com.star4;
  2. /**
  3.  * @Auther: Starshine
  4.  */
  5. public class Person {
  6.     //属性
  7.     int age;
  8.     String name;
  9.     double height;
  10.     //空构造器
  11.     public Person(){
  12.     }
  13.     //有参构造器
  14.     public Person(int age,String name,double height){
  15.         this.age = age;
  16.         this.name = name;
  17.         this.height = height;
  18.     }
  19.     //方法:
  20.     /*public void eat(){
  21.         int age = 10;
  22.         System.out.println(age);//就近原则,age指的是离它近的age--》局部变量的age
  23.         System.out.println(this.age);//这里指代的就是属性的age
  24.         System.out.println("我喜欢吃饭");
  25.     }*/
  26.     public void play(){
  27.         /*this.*/eat();
  28.         System.out.println("上网");
  29.         System.out.println("洗澡");
  30.     }
  31.     public void eat(){
  32.         System.out.println(/*this.*/age);
  33.         System.out.println("吃饭");
  34.     }
  35. }

(3)this可以修饰构造器:

总结:同一个类中的构造器可以相互用this调用,注意:this修饰构造器必须放在第一行

  1. package com.star4;
  2. /**
  3.  * @Auther: Starshine
  4.  */
  5. public class Person {
  6.     //属性
  7.     int age;
  8.     String name;
  9.     double height;
  10.     //空构造器
  11.     public Person(){
  12.     }
  13.     //有参构造器
  14.     public Person(int age,String name,double height){
  15.         this(age,name);
  16.         this.height = height;
  17.     }
  18.     public Person(int age,String name){
  19.         this(age);
  20.         this.name = name;
  21.     }
  22.     public Person(int age){
  23.         this.age = age;
  24.     }
  25.     //方法:
  26.     /*public void eat(){
  27.         int age = 10;
  28.         System.out.println(age);//就近原则,age指的是离它近的age--》局部变量的age
  29.         System.out.println(this.age);//这里指代的就是属性的age
  30.         System.out.println("我喜欢吃饭");
  31.     }*/
  32.     public void play(){
  33.         /*this.*/eat();
  34.         System.out.println("上网");
  35.         System.out.println("洗澡");
  36.     }
  37.     public void eat(){
  38.         System.out.println(/*this.*/age);
  39.         System.out.println("吃饭");
  40.     }
  41. }
static

【1】static可以修饰:属性,方法,代码块,内部类。

【2】static修饰属性;

  1. package com.star5;
  2. /**
  3.  * @Auther: Starshine
  4.  */
  5. public class Test {
  6.     //属性:
  7.     int id;
  8.     static int sid;
  9.     //这是一个main方法,是程序的入口:
  10.     public static void main(String[] args) {
  11.         //创建一个Test类的具体的对象
  12.         Test t1 = new Test();
  13.         t1.id = 10;
  14.         t1.sid = 10;
  15.         Test t2 = new Test();
  16.         t2.id = 20;
  17.         t2.sid = 20;
  18.         Test t3 = new Test();
  19.         t3.id = 30;
  20.         t3.sid = 30;
  21.         //读取属性的值:
  22.         System.out.println(t1.id);
  23.         System.out.println(t2.id);
  24.         System.out.println(t3.id);
  25.         System.out.println(t1.sid);
  26.         System.out.println(t2.sid);
  27.         System.out.println(t3.sid);
  28.     }
  29. }

内存分析:

一般官方的推荐访问方式:可以通过类名.属性名的方式去访问:

static修饰属性总结:

(1)在类加载的时候一起加载入方法区中的静态域中

(2)先于对象存在

(3)访问方式: 对象名.属性名    类名.属性名(推荐)

static修饰属性的应用场景:某些特定的数据想要在内存中共享,只有一块 --》这个情况下,就可以用static修饰的属性

  1. package com.star5;
  2. /**
  3.  * @Auther: Starshine
  4.  */
  5. public class StarStudent {
  6.     //属性:
  7.     String name;
  8.     int age;
  9.     static String school;
  10.     //这是一个main方法,是程序的入口:
  11.     public static void main(String[] args) {
  12.         StarStudent.school = "马士兵教育";
  13.         //创建学生对象:
  14.         StarStudent s1 = new StarStudent();
  15.         s1.name = "张三";
  16.         s1.age = 19;
  17.         //s1.school = "马士兵教育";
  18.         StarStudent s2 = new StarStudent();
  19.         s2.name = "李四";
  20.         s2.age = 21;
  21.         //s2.school = "马士兵教育";
  22.         System.out.println(s2.school);
  23.     }
  24. }

属性:

静态属性 (类变量)

非静态属性(实例变量)

【3】static修饰方法;

  1. package com.star5;
  2. /**
  3.  * @Auther: Starshine
  4.  */
  5. public class Demo {
  6.     int id;
  7.     static int sid;
  8.     public void a(){
  9.         System.out.println(id);
  10.         System.out.println(sid);
  11.         System.out.println("------a");
  12.     }
  13.     //1.static和public都是修饰符,并列的没有先后顺序,先写谁后写谁都行
  14.     static public void b(){
  15.         //System.out.println(this.id);//4.在静态方法中不能使用this关键字
  16.         //a();//3.在静态方法中不能访问非静态的方法
  17.         //System.out.println(id);//2.在静态方法中不能访问非静态的属性
  18.         System.out.println(sid);
  19.         System.out.println("------b");
  20.     }
  21.     //这是一个main方法,是程序的入口:
  22.     public static void main(String[] args) {
  23.         //5.非静态的方法可以用对象名.方法名去调用
  24.         Demo d = new Demo();
  25.         d.a();
  26.         //6.静态的方法可以用   对象名.方法名去调用  也可以 用  类名.方法名 (推荐)
  27.         Demo.b();
  28.         d.b();
  29.       
  30.     }
  31. }
代码块

【1】类的组成:属性,方法,构造器,代码块,内部类

【2】代码块分类:普通块,构造块,静态块,同步块(多线程)

【3】代码:

  1. package com.star6;
  2. /**
  3.  * @Auther: Starshine
  4.  */
  5. public class Test {
  6.     //属性
  7.     int a;
  8.     static int sa;
  9.     //方法
  10.     public void a(){
  11.         System.out.println("-----a");
  12.         {
  13.             //普通块限制了局部变量的作用范围
  14.             System.out.println("这是普通块");
  15.             System.out.println("----000000");
  16.             int num = 10;
  17.             System.out.println(num);
  18.         }
  19.         //System.out.println(num);
  20.         //if(){}
  21.         //while(){}
  22.     }
  23.     public static void b(){
  24.         System.out.println("------b");
  25.     }
  26.     //构造块
  27.     {
  28.         System.out.println("------这是构造块");
  29.     }
  30.     //静态块
  31.     static{
  32.         System.out.println("-----这是静态块");
  33.         //在静态块中只能方法:静态属性,静态方法
  34.         System.out.println(sa);
  35.         b();
  36.     }
  37.     //构造器
  38.     public Test(){
  39.         System.out.println("这是空构造器");
  40.     }
  41.     public Test(int a){
  42.         this.a = a;
  43.     }
  44.     //这是一个main方法,是程序的入口:
  45.     public static void main(String[] args) {
  46.         Test t = new Test();
  47.         t.a();
  48.         Test t2 = new Test();
  49.         t2.a();
  50.     }
  51. }

总结:

(1)代码块执行顺序:

最先执行静态块,只在类加载的时候执行一次,所以一般以后实战写项目:创建工厂,数据库的初始化信息都放入静态块。

一般用于执行一些全局性的初始化操作。

再执行构造块,(不常用)

再执行构造器,

再执行方法中的普通块。

包,import

【1】生活案例:

邮寄快递:中国.北京.通州区.****小区.5号楼.3单元.101房.赵珊珊

历史:常山赵子龙

【2】包的作用:

为了解决重名问题(实际上包对应的就是盘符上的目录)

解决权限问题

【3】创建包:

包名定义:

(1)名字全部小写

(2)中间用.隔开

(3)一般都是公司域名倒着写 :  com.jd   com.star

(4)加上模块名字:

com.jd.login    com.jd.register

(5)不能使用系统中的关键字:nul,con,com1---com9.....

(6)包声明的位置一般都在非注释性代码的第一行:

【4】导包问题:

  1. //声明包:
  2. package com.star7;
  3. import com.star2.Person; //导包:就是为了进行定位
  4. import java.util.Date;
  5. /**
  6.  * @Auther: Starshine
  7. */
  8. public class Test {
  9.     //这是一个main方法,是程序的入口:
  10.     public static void main(String[] args) {
  11.         new Person();
  12.         new Date();
  13.         new java.sql.Date(1000L);//在导包以后,还想用其他包下同名的类,就必须要手动自己写所在的包。
  14.         new Demo();
  15.     }
  16. }

总结:

(1)使用不同包下的类要需要导包: import **.*.*;  例如:import java.util.Date;

(2)在导包以后,还想用其他包下同名的类,就必须要手动自己写所在的包。

(3)同一个包下的类想使用不需要导包,可以直接使用。

(4)在java.lang包下的类,可以直接使用无需导包:

(5)IDEA中导包快捷键:alt+enter  

        可以自己设置自动导包

(6)可以直接导入*:

【5】在Java中的导包没有包含和被包含的关系:

设置目录平级的格式(不是包含和被包含的显示):

【6】静态导入:

  1. package com.star11;
  2. //静态导入:
  3. import static java.lang.Math.*;
  4. //导入:java.lang下的Math类中的所有静态的内容
  5. /**
  6.  * @Auther: Starshine
  7.  */
  8. public class Test {
  9.     //这是一个main方法,是程序的入口:
  10.     public static void main(String[] args) {
  11.         System.out.println(random());
  12.         System.out.println(PI);
  13.         System.out.println(round(5.6));
  14.     }
  15.     //在静态导入后,同一个类中有相同的方法的时候,会优先走自己定义的方法。
  16.     public static int round(double a){
  17.         return 1000;
  18.     }
  19. }

网站公告

今日签到

点亮在社区的每一天
去签到