Java的关键字

this关键字 用法:

(1)this可以修饰属性: 

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

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

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

(2)this修饰方法: 

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

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

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

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

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

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

【2】static修饰属性;

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

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

static修饰属性总结:

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

(2)先于对象存在

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

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

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

属性:

静态属性 (类变量)

非静态属性(实例变量)

【3】static修饰方法;

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值