java 修饰符 this static super final 权限修饰符

java 修饰符

this

this修饰属性

属性名字形参发生重名的时候,或者 属性名字局部变量重名的时候,就会发生就近原则,所以如果我要是直接使用变量名字的话就指的是离的近的那个形参或者局部变量,这时候如果我想要表示属性的话,在前面要加上:this.修饰。如果不发生重名问题的话,实际上你要是访问属性也可以省略this。

public class Person {
    String name;
    int age;
    double height;
	//全参构造器
    public Person(String name, int age, double height) {
        this.name = name;
        this.age = age;
        this.height = height;
    }
}

this修饰方法

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

this修饰构造器

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

    //有参构造器
    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;
    }

static

static修饰属性

  • 在类加载的时候一起加载入方法区中的静态域中,先于对象存在,被所有该类的对象共享
  • 访问方式: 对象名.属性名 类名.属性名(推荐)
  • 应用场景:某些特定的数据想要在内存中共享,只有一块 。这个情况下,就可以用static修饰的属性
  • 静态属性 (类变量),非静态属性(实例变量)

static修饰方法

  • static和public都是修饰符,并列的没有先后顺序,先写谁后写谁都行

  • 在静态方法中不能访问非静态的属性和方法,因为static先于对象存在

  • 在静态方法中不能使用this关键字

  • 非静态的方法可以用对象名.方法名去调用;静态的方法可以用对象名.方法名去调用,也可以用类名.方法名(推荐)

  • 同一个类中可以直接调用

static修饰代码块

  • 最先执行static静态块,且只在类加载的时候执行一次
public class Student {
    static String school;
    String name;
    static int score;

    Student(String name) {
        this.name = name;
    }

    int sum(int score) {
        score += score;
        return score;
    }

    void print() {
        System.out.println(school + "," + name + "," + score);
    }

    //1.static和public都是修饰符,并列的没有先后顺序,先写谁后写谁都行
    static public void sprint() {
//        System.out.println(name);//2.在静态方法中不能访问非静态的属性
//        System.out.println(this.sid);//4.在静态方法中不能使用this关键字
//        print();//3.在静态方法中不能访问非静态的方法
        System.out.println("static:"+school + "," + score);
    }
    //静态块
    static{
        System.out.print("静态块:");
        //在静态块中只能方法:静态属性,静态方法
        sprint();
    }
    public static void main(String[] args) {
//两句在堆中分配了两个 Student 类内存空间,stu1 的 name 为”小明”, stu2 的 name 为”小王”。
        Student stu1 = new Student("小明");
        Student stu2 = new Student("小王");
//因为在 Student 类中 school 的是类成员,所有对象共享,使得 stu1、stu2 的 school 均是“**大学”。
        Student.school = "**大学";
/*Student 类中 static int score 是一个成员变量,而 int sum(int score)中的 score 仅是一个函数参数变量,由于sum 函数中没有使用this进行全局,导致sum 函数内的所有操作仅在函数内有效,不会改变函数外的 score 变量,而 int 类型的类初始值是 0,故这两句代码执行结束之后,stu1 和 stu2 的成员变量 score 依旧是 0。*/
        stu1.sum(20);
        stu2.sum(30);
//打印出 school + "," + name + "," + score
        //5.非静态的方法可以用对象名.方法名去调用
        stu1.print();
        stu2.print();
        //6.静态的方法可以用 对象名.方法名 去调用  也可以用 类名.方法名 (推荐)
        stu1.sprint();
        Student.sprint();
        sprint();//同一个类中可以直接调用
    }
}
静态块:static:null,0
**大学,小明,0
**大学,小王,0
static:**大学,0
static:**大学,0
static:**大学,0

super

super指的是父类的,可以修饰属性、方法、构造器;

super修饰属性、方法

在子类的方法中,可以通过 super.属性super.方法 的方式,显示的去调用父类提供的属性,方法。在通常情况下,super.可以省略不写:

在特殊情况下,当子类和父类的属性、方法重名时,你要想使用父类的属性、方法,必须加上修饰符super.,只能通过super.属性super.方法来调用,在这种情况下,super.就不可以省略不写。

super修饰构造器

我们平时写的构造器的第一行都有:super()

作用:调用父类的空构造器,只是我们一般都省略不写

所有构造器的第一行默认情况下都有super(),但是一旦你的构造器中显式的使用super调用了父类构造器,那么这个super()就不会给你默认分配了。

如果构造器中没有显示的调用父类构造器的话,那么第一行都有super(),可以省略不写

在构造器中,super调用父类构造器和this调用子类构造器只能存在一个,两者不能共存:因为super修饰构造器要放在第一行,this修饰构造器也要放在第一行;

Person父类

public class Person {
    int age;
    String name;
    int suth = 30;

    public Person() {
        /*super();*/
    }

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

    public void eat(){
        System.out.println("------eat------Person");
    }
}

Student子类

public class Student extends Person {
    double score;
    int suth = 20;

    public Student() {
    }

    public Student(String name, int age, double score) {
        /*this.name = name;
        this.age = age;//*/
        /*super.name = name;
        super.age = age;*/
        super(age, name); //等效以上两种方法
        this.score = score;
    }

    public void print() {
        System.out.println(age+"的"+name + "分数为" + score);
    }

    @Override
    public void eat() {
        System.out.println("------eat------Student");
    }

    public void a() {
        System.out.println("suth:" + suth); //20,student
        System.out.println("this.suth:" + this.suth); //20,student
        System.out.println("super.suth:" + super.suth); //30,peson
        eat();
        this.eat();
        super.eat();
    }
}

Test测试类

public class Test {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.a();
        Student s2 = new Student("fyz",18,99);
        s2.print();
    }
}
suth:20
this.suth:20
super.suth:30
------eat------Student
------eat------Student
------eat------Person
18的fyz分数为99.0

final

  • final修饰一个变量,变量的值不可以改变,这个变量也变成了一个字符常量,约定俗称的规定:名字大写

  • final修饰引用数据类型,那么地址值就不可以改变

  • final修饰方法,那么这个方法不可以被该类的子类重写:

  • final修饰类,代表没有子类,该类不可以被继承:一旦一个类被final修饰,那么里面的方法也没有必要用final修饰了(final可以省略不写)

例如Math类:看源码发现:
Math类没有子类,不能被其他类继承了,里面的属性全部被final修饰,方法也是被final修饰的,只是省略不写了,子类也没有必要进行重写。

public final class Math {
    private Math() {}
    
    public static final double PI = 3.14159265358979323846;
    
    public static double sin(double a) {
        return StrictMath.sin(a); // default impl. delegates to StrictMath
    }
}

Math类中的所有的属性,方法都被static修饰,那么不用创建对象去调用,外界不可以创建对象,只能通过类名.属性名 类名.方法名 去调用

public class Test {
    public static void main(String[] args) {
        System.out.println("PI:" + Math.PI + "\nsin(20):" + Math.sin(20));
    }
}
PI:3.141592653589793
sin(20):0.9129452507276277

权限修饰符

同类class同包 package子类 extends所有类all
private✔️
default *✔️✔️
protected✔️✔️✔️
public✔️✔️✔️✔️
  • private:权限:在当前类中可以访问
  • default缺省修饰符:权限:到同一个包下的其他类都可以访问
  • protected:权限:最大到不同包下的子类
  • public:在整个项目中都可以访问

属性,方法修饰符(4种):private,default *,protected,public
类修饰符(2种):default *,public
一般属性用private修饰 ,方法用public修饰

posted @ 2021-08-06 18:57  SKPrimin  阅读(30)  评论(0编辑  收藏  举报