Java_basic_3: static, this 关键字

2017-10-07  本文已影响0人  DiscoSOS

static

image.png

static 修饰方法

public static void main(String args[]){
//
}
package com.chee3e;

public class HelloWorld {
    public static void print() {
        System.out.println("Hello!");
    }
    
    public static void main(String args[]) {
        //1.
        HelloWorld.print();
        //2.
        HelloWorld demo = new HelloWorld();
        demo.print();
    }

}

public class HelloWorld {
    String name = "Jamie";
    static String hobby = "football";
    
    // 
    public static void print() {
        System.out.println("Welcome" + Name); //error!!
        System.out.println("Hobby is :" + hobby );
    }
}


public class HelloWorld {
    String name = "Jamie";
    static String hobby = "football";
    // 
    public static void print() {

        HelloWorld demo = new HelloWorld();
        System.out.println("Welcome to EU, " + demo.name);
        
        System.out.println("Hobby is :" + hobby );
    }
    
    public static void main(String args[]) {
        print();
    }

}

public class HelloWorld {
    String name = "Jamie";
    static String hobby = "football";
    // 
    public  void show() {
        System.out.println("WELCOME TO EU, " + name);
        System.out.println("His hobby is " + hobby);
    }
}   
public class HelloWorld {
    String name = "Jamie";
    static String hobby = "football";
    // 
    public  void show() {
        System.out.println("WELCOME TO EU, " + name);
        System.out.println("His hobby is " + hobby);
    }
    
    public static void main(String args[]) {
        // create a new object
        HelloWorld demo = new HelloWorld();
        demo.show();
        
    }
}

初始化块/static初始化块 initializer block/static initializer block

public class HelloWorld {
    int num1;
    int num2;
    static int num3;
    
    // constructor
    public HelloWorld(){
        num1 = 91;
        System.out.println("use constructor to assign the variable num1" );
        
    }
    
    // initializer block
    {
        num2 =74;
        System.out.println("use an initializer block to assign the variables num2");
    }
    // a static initializer block
    static {
        num3 = 99;
        System.out.println("use a static initializer block to assign the variables num3");
    }
    
    
    public static void main (String args[]) {
        HelloWorld demo = new HelloWorld();
        System.out.println(demo.num1);
        System.out.println(demo.num2);
        System.out.println(num3);
        //
        HelloWorld demo2 = new HelloWorld();    
    }

}

*运行代码结果:

use a static initializer block to assign the variables num3
use an initializer block to assign the variables num2
use a constructor to assign the variable num1
91
74
99
use an initializer block to assign the variables num2
use a constructor to assign the variable num1

this 关键字

public class Telephone {
    //
    private double screen;
    private double cpu;
    private double mem;
    
    public void sendMessage() {
        System.out.println("sendMessage");
    }
    
    public double getCpu() {
        this.sendMessage();
        return cpu;
    }
    public void setCpu(double cpu) {
        this.cpu = cpu;
    }
    public double getMem() {
        return mem;
    }
    public void setMem(double mem) {
        this.mem = mem;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读