this关键字 用法:
(1)this可以修饰属性:
总结:当属性名字和形参发生重名的时候,或者 属性名字 和局部变量重名的时候,都会发生就近原则,所以如果我要是直接使用变量名字的话就指的是离的近的那个形参或者局部变量,这时候如果我想要表示属性的话,在前面要加上:this.修饰
如果不发生重名问题的话,实际上你要是访问属性也可以省略this.
- package com.star4;
- /**
- * @Auther: Starshine
- */
- public class Person {
- //属性
- int age;
- String name;
- double height;
- //空构造器
- public Person(){
- }
- //有参构造器
- public Person(int age,String name,double height){
- this.age = age;
- this.name = name;
- this.height = height;
- }
- //方法:
- public void eat(){
- int age = 10;
- System.out.println(age);//就近原则,age指的是离它近的age--》局部变量的age
- System.out.println(this.age);//这里指代的就是属性的age
- System.out.println("我喜欢吃饭");
- }
- }
(2)this修饰方法:
总结:在同一个类中,方法可以互相调用,this.可以省略不写。
- package com.star4;
- /**
- * @Auther: Starshine
- */
- public class Person {
- //属性
- int age;
- String name;
- double height;
- //空构造器
- public Person(){
- }
- //有参构造器
- public Person(int age,String name,double height){
- this.age = age;
- this.name = name;
- this.height = height;
- }
- //方法:
- /*public void eat(){
- int age = 10;
- System.out.println(age);//就近原则,age指的是离它近的age--》局部变量的age
- System.out.println(this.age);//这里指代的就是属性的age
- System.out.println("我喜欢吃饭");
- }*/
- public void play(){
- /*this.*/eat();
- System.out.println("上网");
- System.out.println("洗澡");
- }
- public void eat(){
- System.out.println(/*this.*/age);
- System.out.println("吃饭");
- }
- }
(3)this可以修饰构造器:
总结:同一个类中的构造器可以相互用this调用,注意:this修饰构造器必须放在第一行
- package com.star4;
- /**
- * @Auther: Starshine
- */
- public class Person {
- //属性
- int age;
- String name;
- double height;
- //空构造器
- public Person(){
- }
- //有参构造器
- public Person(int age,String name,double height){
- this(age,name);
- this.height = height;
- }
- public Person(int age,String name){
- this(age);
- this.name = name;
- }
- public Person(int age){
- this.age = age;
- }
- //方法:
- /*public void eat(){
- int age = 10;
- System.out.println(age);//就近原则,age指的是离它近的age--》局部变量的age
- System.out.println(this.age);//这里指代的就是属性的age
- System.out.println("我喜欢吃饭");
- }*/
- public void play(){
- /*this.*/eat();
- System.out.println("上网");
- System.out.println("洗澡");
- }
- public void eat(){
- System.out.println(/*this.*/age);
- System.out.println("吃饭");
- }
- }
static
【1】static可以修饰:属性,方法,代码块,内部类。
【2】static修饰属性;
- package com.star5;
- /**
- * @Auther: Starshine
- */
- public class Test {
- //属性:
- int id;
- static int sid;
- //这是一个main方法,是程序的入口:
- public static void main(String[] args) {
- //创建一个Test类的具体的对象
- Test t1 = new Test();
- t1.id = 10;
- t1.sid = 10;
- Test t2 = new Test();
- t2.id = 20;
- t2.sid = 20;
- Test t3 = new Test();
- t3.id = 30;
- t3.sid = 30;
- //读取属性的值:
- System.out.println(t1.id);
- System.out.println(t2.id);
- System.out.println(t3.id);
- System.out.println(t1.sid);
- System.out.println(t2.sid);
- System.out.println(t3.sid);
- }
- }
内存分析:
一般官方的推荐访问方式:可以通过类名.属性名的方式去访问:
static修饰属性总结:
(1)在类加载的时候一起加载入方法区中的静态域中
(2)先于对象存在
(3)访问方式: 对象名.属性名 类名.属性名(推荐)
static修饰属性的应用场景:某些特定的数据想要在内存中共享,只有一块 --》这个情况下,就可以用static修饰的属性
- package com.star5;
- /**
- * @Auther: Starshine
- */
- public class StarStudent {
- //属性:
- String name;
- int age;
- static String school;
- //这是一个main方法,是程序的入口:
- public static void main(String[] args) {
- StarStudent.school = "马士兵教育";
- //创建学生对象:
- StarStudent s1 = new StarStudent();
- s1.name = "张三";
- s1.age = 19;
- //s1.school = "马士兵教育";
- StarStudent s2 = new StarStudent();
- s2.name = "李四";
- s2.age = 21;
- //s2.school = "马士兵教育";
- System.out.println(s2.school);
- }
- }
属性:
静态属性 (类变量)
非静态属性(实例变量)
【3】static修饰方法;
- package com.star5;
- /**
- * @Auther: Starshine
- */
- public class Demo {
- int id;
- static int sid;
- public void a(){
- System.out.println(id);
- System.out.println(sid);
- System.out.println("------a");
- }
- //1.static和public都是修饰符,并列的没有先后顺序,先写谁后写谁都行
- static public void b(){
- //System.out.println(this.id);//4.在静态方法中不能使用this关键字
- //a();//3.在静态方法中不能访问非静态的方法
- //System.out.println(id);//2.在静态方法中不能访问非静态的属性
- System.out.println(sid);
- System.out.println("------b");
- }
- //这是一个main方法,是程序的入口:
- public static void main(String[] args) {
- //5.非静态的方法可以用对象名.方法名去调用
- Demo d = new Demo();
- d.a();
- //6.静态的方法可以用 对象名.方法名去调用 也可以 用 类名.方法名 (推荐)
- Demo.b();
- d.b();
- }
- }
代码块
【1】类的组成:属性,方法,构造器,代码块,内部类
【2】代码块分类:普通块,构造块,静态块,同步块(多线程)
【3】代码:
- package com.star6;
- /**
- * @Auther: Starshine
- */
- public class Test {
- //属性
- int a;
- static int sa;
- //方法
- public void a(){
- System.out.println("-----a");
- {
- //普通块限制了局部变量的作用范围
- System.out.println("这是普通块");
- System.out.println("----000000");
- int num = 10;
- System.out.println(num);
- }
- //System.out.println(num);
- //if(){}
- //while(){}
- }
- public static void b(){
- System.out.println("------b");
- }
- //构造块
- {
- System.out.println("------这是构造块");
- }
- //静态块
- static{
- System.out.println("-----这是静态块");
- //在静态块中只能方法:静态属性,静态方法
- System.out.println(sa);
- b();
- }
- //构造器
- public Test(){
- System.out.println("这是空构造器");
- }
- public Test(int a){
- this.a = a;
- }
- //这是一个main方法,是程序的入口:
- public static void main(String[] args) {
- Test t = new Test();
- t.a();
- Test t2 = new Test();
- t2.a();
- }
- }
总结:
(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】导包问题:
- //声明包:
- package com.star7;
- import com.star2.Person; //导包:就是为了进行定位
- import java.util.Date;
- /**
- * @Auther: Starshine
- */
- public class Test {
- //这是一个main方法,是程序的入口:
- public static void main(String[] args) {
- new Person();
- new Date();
- new java.sql.Date(1000L);//在导包以后,还想用其他包下同名的类,就必须要手动自己写所在的包。
- new Demo();
- }
- }
总结:
(1)使用不同包下的类要需要导包: import **.*.*; 例如:import java.util.Date;
(2)在导包以后,还想用其他包下同名的类,就必须要手动自己写所在的包。
(3)同一个包下的类想使用不需要导包,可以直接使用。
(4)在java.lang包下的类,可以直接使用无需导包:
(5)IDEA中导包快捷键:alt+enter
可以自己设置自动导包
(6)可以直接导入*:
【5】在Java中的导包没有包含和被包含的关系:
设置目录平级的格式(不是包含和被包含的显示):
【6】静态导入:
- package com.star11;
- //静态导入:
- import static java.lang.Math.*;
- //导入:java.lang下的Math类中的所有静态的内容
- /**
- * @Auther: Starshine
- */
- public class Test {
- //这是一个main方法,是程序的入口:
- public static void main(String[] args) {
- System.out.println(random());
- System.out.println(PI);
- System.out.println(round(5.6));
- }
- //在静态导入后,同一个类中有相同的方法的时候,会优先走自己定义的方法。
- public static int round(double a){
- return 1000;
- }
- }