Java-static、final

static

1)static:静态的(类有关的)
2)可以用来修饰属性:被static修饰的属性,称为静态属性,该属性跟类有关,跟对象无关
3)访问静态属性:类名.属性名
或 对象名.属性名(使用较少)
4)实例变量:就是没有用static修饰的属性,跟对象有关

public class Test1 {
    public static void main(String[] args) {
        User user=new User();
        user.name="刘";
        user.age=12;
        User.num=100;
    }
}
class User{
    String name;//实例变量
    int age;//实例变量
    static int num;//人数  静态属性
}
public class Test1 {
    public static void main(String[] args) {
        Student student=new Student("公孙瓒","男",45);
        System.out.println(Student.num);//1
        Student student2=new Student("刘备","男",41);
        System.out.println(Student.num);//2
        Student student3=new Student("左慈","男",39);
        System.out.println(student3.num);//3
    }
}
//设计一个学生类,属性:姓名,性别,年龄,人数(静态)
//               构造方法(姓名,性别,年龄)
//               统计出来总的人数(对象的个数,每次调用都会创造一个新的对象)
class Student{
    String name;
    String sex;
    int age;
    static int num=0;
    String address;
//在构造方法当中,添加num++,这样每次创建新的对象的时候,num的值都会加1
    Student(String name,String sex,int age){
        this.name=name;
        this.sex=sex;
        this.age=age;
        num++;
    }    
}

5)静态方法:用static修饰的方法,叫做静态方法,静态方法跟类有关,跟对象无关
6)访问静态方法:类名.静态方法名
或 对象名.静态方法名(不推荐)

public class Test1 {
    //用static修饰任何变量或方法:在内存都有单独的存储空间,外界可以通过类名直接去访问
    public static void main(String[] args) {
        Coo.show();
    }
}
class Coo{
    static void show(){
        System.out.println("今天是个好天气");
    }
}

总结:用static修饰变量或者方法,不用创建也可以直接访问。static修饰的变量或方法跟类有关,而对象无关。
7)静态方法不可以访问实例变量:实例变量跟对象有关,只有当创建对象,才有该实例对象。静态方法跟类有关,不需要创建对象,就可以直接访问静态的方法
8)静态方法可以访问静态变量

public class Test1 {
    public static void main(String[] args) {
        Emp.salary();
        Emp emp1=new Emp("公孙瓒","男",45);
        Emp emp2=new Emp("刘备","男",41);
        Emp emp3=new Emp("左慈","男",39);
        Emp.salary();
    }
}
//设计一个雇员类,属性:姓名,性别,年龄,人数(静态)
//               方法:求总的员工的薪水(每人工资月薪100000)
class Emp{
    String name;
    String sex;
    int age;
    static int num;
    String address;
    Emp(String name,String sex,int age){
        this.name=name;
        this.sex=sex;
        this.age=age;
        num++;
    }
    static void salary(){
        int sum=num*100000;
        System.out.println(sum);
    }
}

final

final:最终的,最后的
1)final可以修饰变量(属性),方法,类
2)用final修饰变量,初始化以后,其值不能再被修改

public class Test1 {
    public static void main(String[] args) {
        Zoo zoo=new Zoo();
//        zoo.num1=1200;//报错
        final int a=12;
//用final修饰变量:初始化以后,也不能修改了
//        a=35;报错
    }
}
class Zoo{
//final修饰属性:被final修饰的属性,初始化以后不能再被修改
    final int num1=1000;
}

3)java中常量:其值永远都不会变的量,称为常量
标准:static final修饰属性-----常量

public class Test1 {
    //常量的标准格式:static+final属性
    //常量名字一定大写
    public static void main(String[] args) {
        System.out.println(Yoo.NAME);
    }
}
class Yoo{
    //final static/static final
    final static String NAME="刘英谦";//常量的名字,字母全部大写
    //final+属性:从语法上是常量,企业开发中,一般都是加上static以后,才称为常量
}

4)final修饰方法,不可以被子类重写(覆盖)

public class Test1 {
    public static void main(String[] args) {
        Too2 too=new Too2();
        too.show();
    }
}
class Too1{
    final void show(){
        System.out.println("今天是个好天气");
    }
}
class Too2 extends Too1{
//    void show(){--报错
//        System.out.println("天气好好");
//    }
}

5)final修饰类,不能再给继承,该类不能再有子类了

public class Test1 {
    public static void main(String[] args) {
    }
}
final class Voo1{
}
//class Voo2 extends Voo1{}//--报错

猜你喜欢

转载自www.cnblogs.com/lilyo/p/12792713.html