this关键字,static关键字,代码块

this关键字

作用:

  • 当成员变量和局部变量重名时,优先使用局部变量,但是加了this以后就可以区分开局部和成员变量.
  •  this是一个指向当前对象的一个引用.

用法:

  • this.方法名的作用就是让类中一个方法,访问该类里的另一个方法或实例变量.
  • this.成员变量名作用是显示的访问当前对象的成员变量
public class Person {
    String name;
    int age;
    String gender;

    public Person(){
        //在类中的某个构造方法中,使用后this关键字调用另一个构造方法
        this("",21,"");
    }

    //this在类中表示当前正在访问的对象,this.成员变量名--显示的访问当前对象的成员变量
    public Person(String name,int age,String gender){
        this.name =name;
        this.age =age;
        this.gender = gender;
    }
    public void show(){
        System.out.println("姓名:"+name+" 年龄:"+age+" 性别:"+gender);
    }
    public void work(){
        System.out.println("gongzuo");
        this.show();
    }
}

 

若是没有this就会出现下面的情况. 

public Person(String name,int age,String gender){
    name =name;
    age =age;
    gender = gender;
}

 static关键字

1.静态变量(static修饰的变量)

静态变量属于类,存储在方法区中,随着类的加载而存在,使用时可以用类名访问(类名.变量名)也可以用对象访问(对象.变量名).

注;静态变量只能修饰成员变量,不能修饰局部变量!

public class Chinese {
    String name;//中国人每个人的名字不同,需要在每个对象中都有一个name属性
    static String country="中国";//中国人的国籍都是中国,此时应该用static修饰,静态属性在内存中只有一份

    //使用一个变量来记录此类被创建了多少个对象
    static int count =0;
    public Chinese() {
        count = count+1;
    }
}
public class TestChinese {
    public static void main(String[] args) {
        //静态成员随着类的加载而加载的,只要类加载了,静态成员就可以被使用
        System.out.println(Chinese.country);
        Chinese c1 = new Chinese();
        c1.name= "韩";
        //c1.country="中国";

        Chinese c2 = new Chinese();
        c2.name = "赵";
        //c2.country= "中国";
        System.out.println(Chinese.country);//建议使用类名直接访问静态的成员
        System.out.println(Chinese.count);
    }
}

2静态方法

可以在不创建对象的情况下调用某个方法,使方法和对象解绑.用类名.方法名() 来调用方法
注意: 静态方法中 只能访问用static 修饰的成员,因为非静态成员需要创建对象才能访问,但静态方法被调用时可以不创建对象,此时会矛盾.

public class Cal {
    public static int add(int a,int b){
        System.out.println(a+b);
        return a+b;
    }
    public static int sub(int a,int b){
        System.out.println(a-b);
        return a-b;
    }
    public static int mul(int a,int b){
        System.out.println(a*b);
        return a*b;
    }
    public static int div(int a,int b){
        System.out.println(a/b);
        return a/b;
    }

    public static void main(String[] args) {
        Cal.add(8,2);
        Cal.sub(8,2);
        Cal.mul(8,2);
        Cal.div(8,2);
    }
}

代码块

定义:

         代码块:在类中声明一个没有声明的代码块.

分类:

         实例代码块:在每次创建对象时就会执行

         静态代码块:

  • 只要类被加载时自动最先执行,只会执行一次
  • 通过类名访问类中静态成员时,类会被加载 
  • 在一个类中使用main方法,也会加载类
  • 创建对象也会加载类,类只会被加载一次

    如果有多个实例代码块或多个静态代码快,他们会按照先后顺序执行。
    实例代码块先于构造方法执行

public class CodeBlock {

    static int a=2;
    {
        System.out.println("我是实例代码块,在创建对象时,不需要显示调用,我会自动执行1");
    }
    {
        System.out.println("我是实例代码块,在创建对象时,不需要显示调用,我会自动执行2");
    }
    static {
        System.out.println("我是静态代码块,在创建对象时,不需要显示调用,我会自动执行");
    }
}
public class TestCodeBlock {
    public static void main(String[] args) {
        new CodeBlock();
        new CodeBlock();
        System.out.println(CodeBlock.a);
    }
}

  • 20
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值