java面向对象编程(3)

16 阅读3分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第23天,点击查看活动详情

static关键字

我们之前学过static修饰的方法!

  • 修饰属性
  • 修饰方法
  • 修饰代码块
  • 修饰类

修饰属性

static修饰属性,该属性属于类,不属于对象,通过类可以调用该属性!该属性只要一分,就是一个类的所有实例对象共用该static属性!

class People1{
	//static修饰属性,该属性属于类
    public static String name;
    public static int age;
}
public class Test {
    public static void main(String[] args) {
        //通过类名调用类属性,并且赋值
        People1.name = "bug郭";
        People1.age =18;
        //实例化一个对象
        People1 people1 = new People1();
        //通过对象调用(static修饰的)类属性,
        // 不建议这样使用,类属性属于类,应该通过类名调用
        System.out.println("name:"+people1.name);
        System.out.println("age:"+people1.age);
    }
}

在这里插入图片描述

  • 类的实例化对象共用类属性

当我们创建多个对象分别调用他们的类方法时,运行结果如下: 在这里插入图片描述可以看到static修饰的类属性,空间只有一块,属于所有的对象,当我们改变了类属性,所有的对象的属性也跟着改变!就是说public static int age; 赋值18后,所有的People1对象都是18岁! 在这里插入图片描述可以看到static修饰的类属性,只有一份,在方法区中!

修饰方法 static修饰方法和修饰属性相同!属于类方法,只有一份!

class People1{
    public static String name;
    public static int age;
    public static void eat(){
        System.out.println("eat");
    }
}
public class Test {
    public static void main(String[] args) {
        People1.eat(); //通过类名调用
        People1 people1 = new People1();
        people1.eat(); //实例对象调用
    }                         
 }

在这里插入图片描述通过static修饰的方法,进行调用时可以直接通过类名.方法调用访问!并且和类变量一样不建议使用对象调用!

  • static修饰的方法只能调用static修饰的成员
class Animal{
    String name;
    int age1; //未被static修饰,实例化变量属于对象
    static int age2; //static修饰,类对象,属于类
    public static void eat(){
        this.age1 = 12;  //error 实例化成员不能在static修饰的类方法中使用
        age2 = 12;  //staic方法只允许使用该方法外类中中的static修饰的成员
    }
}
  • thissuper不能对类成员进行访问 在这里插入图片描述
class Animal{
    String name;
    static int age2; //static修饰,类对象,属于类
    public static void eat(){
     this.age2;  //error
    // 类变量(static修饰)不能用this和super访问,他们都代表对象的引用
    }
}

在这里插入图片描述 注意事项

静态方法和实例无关,而是和类相关。 因此这导致了两个情况:

  • 静态方法不能直接使用非静态数据成员或调用非静态方法(非静态数据成员和方法都是和实例相关的)。
  • thissuper两个关键字不能在静态上下文中使用(this 是当前实例的引用, super是当前实例父类实例的引用, 也是和当前实例相关)。
  • main 方法为static方法。

总结

class Person {
    public int age;//实例变量,存放在对象内
    public String name;//实例变量
    public String sex;//实例变量
    public static int count;//类变量也叫静态变量,编译时已经产生,属于类本身,且只有一份。存放在方法区
    public final int SIZE = 10;//被final修饰的叫常量,也属于对象。 被final修饰,后续不可更改
    public static final int  COUNT = 99;//静态的常量,属于类本身,只有一份  被final修饰,后续不可更
    //实例成员函数
    public void eat() {
            int a = 10;//局部变量 
            System.out.println("eat()!");
            }
    //实例成员函数 
    public void sleep() {
            System.out.println("sleep()!");
            }
    
    //静态成员函数
    public static void staticTest(){
            //不能访问非静态成员
            //sex = "man"; error
            System.out.println("StaticTest()");
            }
}
public class Test{
    public static void main(String[] args) {
        //产生对象 实例化对象
        Person person = new Person();//person为对象的引用
        System.out.println(person.age);//默认值为0
        System.out.println(person.name);//默认值为null
        //System.out.println(person.count);//会有警告!
        //正确访问方式:
        System.out.println(Person.count);
        System.out.println(Person.COUNT);
        Person.staticTest();
        //总结:所有被static所修饰的方法或者属性,全部不依赖于对象。
        person.eat();
        person.sleep();
    }
}

在这里插入图片描述