3.3 this关键字与权限修饰符

1. this关键字

this关键字表示对象本身,用它可引用当前对象的成员变量、成员方法和构造方法,可用于以下三种情况:

用来调用该类的另一个构造方法,格式为this( [paramList])。

调用类中的成员变量,格式为this.variableName,可以解决局部变量与成员变量同名的问题,解决方法参数与成员变量同名的问题。

调用类中的成员方法,格式为this.methodName([paramList])。

  • Student类

public class Student{

    public Student() {
        System.out.println("Student无参构造方法...");
    }
    public Student(String name){
        this();     //调用无参构造方法
        this.name = name;
    }

    public String name;

    public void hello(){
        System.out.print("你好,");
    }

    public void showName(){
        this.hello();       //调用成员方法
        System.out.println("我的名字是" + this.name);
    }
}
  • 测试类

public class TestStudent {

    @Test
    public void test(){
        Student stu = new Student("张三");
        stu.showName();
    }

}

2. 权限修饰符

2.1. 类访问权限修饰符

类(也包括接口和枚举等)的访问权限通过访问修饰符实现,类的访问修饰符可以是public或缺省。

如果类用public修饰,则该类称为公共类,公共类可以被任何包中的类使用,如果类用缺省修饰符,该类只能被同一包中其它类使用。

  • Person类,com.wfit.test5.Person.java
public class Person {  //public修饰符

    public void show(){
        System.out.println("person类");
    }

}
  • Student类,com.wfit.test5.Student.java
class Student{  //缺省修饰符

    public void show(){
        System.out.println("student类");
    }

}
  • 同一个包的测试类,com.wfit.test5.Test1.java
public class Test1 {
    @Test
    public void test(){
        Person per = new Person();
        per.show();
        Student stu = new Student();
        stu.show();
    }
}
  • 不同包的测试类,com.wfit.test6.Test2.java
public class Test2 {
    @Test
    public void test(){
        Person per = new Person();
        per.show();
        Student stu = new Student();    //异常,Student类为缺省修饰符,其他包无法访问该类
        stu.show();
    }
}

2.2. 类成员访问权限修饰符

类成员的访问权限包括成员变量和成员方法的访问权限,共有4个修饰符,分别是public、protected、缺省的和private。

成员访问权限如下:

修饰符

同一个类

同一个包的类

不同包的子类

不同包的其它类

public

protected

缺省的/默认的

private

  • Person类,com.wfit.test7.Person.java

public class Person {

    public void showPublic(){
        System.out.println("show public");
    }

    protected void showProtected(){
        System.out.println("show protected");
    }

    void show(){
        System.out.println("show 缺省的");
    }

    private void showPrivate(){
        System.out.println("show private");
    }

    public void test(){
        this.showPublic();
        this.showProtected();
        this.show();
        this.showPrivate();
    }
}
  • 同一个包的类,com.wfit.test7.TestPerson.java

public class TestPerson {

    @Test
    public void test(){
        Person per= new Person();
        per.showPublic();
per.showProtected();
        per.show();
        //per.showPrivate();
    }

}
  • 不同包的子类,com.wfit.test8.Student.java

public class Student extends Person {
    
    public void test(){
        super.showPublic();
        super.showProtected();
        //super.show();
        //super.showPrivate();
    }

}
  • 不同包的其它类,com.wfit.test8.TestPerson.java

public class TestPerson {

    @Test
    public void test(){
        Person per= new Person();
        per.showPublic();
        //per.showProtected();
        //per.show();
        //per.showPrivate();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WFIT~SKY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值