zoukankan      html  css  js  c++  java
  • java关键字 —— new、this、static

      java关键字,也叫保留字(50个),是java有特殊意义的标识符,不能用作参数名、变量名、方法名、类名、包名等。

      现在我们讲讲其中三个关键字的使用吧~~~

    一、new关键字

    1. 用途:新建类的对象

    2. 工作机制:

    • 为对象成员分配内存空间,并指定默认值
    • 对成员变量进行显式初始化
    • 执行构造方法
    • 计算并返回引用值

    二、this关键字

    1. 本质:指创建的对象的地址

    2. 分类:

    • 普通方法:this指向调用该方法的对象
    • 构造方法:this指向正要初始化的对象

    3. 用途:

    • 指向对象
    • 调用重载的构造方法

    【注】:this不能用于static方法中

    /*
     * @Author: bpf
     * @Description: 测试this关键字
     * @FilePath: Learn in the InternetCodejavaTestTestThis.java
     */
    
    public class TestThis {
        int index;
        int id;
        String name;
        String s = "Test <this> key word!";
    
        TestThis() {
            System.out.println(s);
        }
        TestThis(int index, int id) {
            // TestThis();      // 调用构造方法不成功
            this();             // 调用无参的构造方法
            // index = index;   // 此句无意义
            this.index = index;
            this.id = id;
        }
        TestThis(int index, int id, String name) {
            this(index, id);    // 使用this调用构造方法时必须是第一句
            this.name = name;
        }
    
        public void showInfo() {
            System.out.printf(  // 此句不加this效果一样
                "%d - %d: %s
    ", this.index, this.id, this.name);
        }
    
        public static void main(String args[]) {
            TestThis test1 = new TestThis(0, 10086, "中国移动");
            test1.showInfo();
            TestThis test2 = new TestThis(1, 10010, "中国联通");
            test2.showInfo();
        }
    }

    三、static关键字

    1. 用途:

    • 在类中修饰变量,成为成员变量,即类变量
    • 在类中修饰方法,成为静态方法,即类方法

    2. 特点:

    • 对同类的所有对象来说,成员变量只有一份,由类的所以对象共享。
    • static不能修饰实例方法中的局部变量
    • static修饰的方法不能调用非static成员
    /*
     * @Author: bpf
     * @Description: 测试static关键字
     * @FilePath: Learn in the InternetCodejavaTestTestStatic.java
     */
    
    public class TestStatic {
        int index;
        String name;
        static String school = "Underwater School"; // 类变量
    
        public TestStatic(int index, String name) {
            this.index = index;
            this.name = name;
        }
    
        public void welcome(int index, String name) {
            printSchool();
            System.out.printf("Welcome NO.%d %s to Underwater School!
    ", index, name);
        }
    
        public static void printSchool() {          // 类方法
            // welcome(this.index, this.name);  
                // 不能在静态方法中调用非静态方法、非静态变量
                // 不能在静态方法中使用this关键字
            System.out.println(school);
        }
    
        public static void main(String args[]) {
            TestStatic stu = new TestStatic(1, "Jerry");
            stu.printSchool();
            stu.school = "Sky School";
            stu.printSchool();
        }
    }

  • 相关阅读:
    python快速入门及进阶
    Git基础及进阶-系统总结
    go语言快速入门教程
    go学习笔记
    win10中,vscode安装go插件排雷指南
    centos7下安装pcre库(pcretest)
    c语言typedef
    虚拟机中安装centos7后无法上网,使用桥接网络+ssh
    强化学习-Q-learning学习笔记
    python基础教程系列1-基础语法
  • 原文地址:https://www.cnblogs.com/bpf-1024/p/12667873.html
Copyright © 2011-2022 走看看