[Java] Basics Summary

Local variables and member variables

  1. Define the position of the different
    local variables: the method of internal
    member variables: external process, which is directly written in the class
  2. Different scope
    of local variables: The only way which can only be used, the method can not be reused
    member variables: the entire class can all be common
  3. The default values for different
    local variables: no default, if you want to use, you must manually assign
    member variables: If there is no assignment, there will be defaults, rules, and the same array
  4. Different memory location
    local variables: Located in the stack memory
    member variables: Located heap memory
  5. Life cycle is not the same as
    local variables: With the push method and the birth, along with a stack method disappear
    member variables: With the object creation and birth, as the object is garbage disappear

private keyword

You can only be accessed with a modification of the key variables in this class

When the class member variables and local variables of the same name of the method

According to the principle of proximity preferentially use local variables, if access to members of the present class among the variable, required formatthis.成员变量名

public class test2 {
    String name;
    public void sayhello(String name){
        System.out.println(name+"你好"+this.name);
    }
}

Construction method

When an object is created when the method is used to initialize the object constructor, the object member variable is assigned to the initial value, the configuration method of writing, a method in which it names the same as the class name. It does not return a value, so no return type, not even need to void

Precautions

  1. If you do not provide a constructor, the system will give no constructor parameters.
  2. If you provide a constructor, the system will no longer provide no-argument constructor.
  3. Constructor is overloaded, either defined parameters, the parameters may not be defined.

Standard Code --JavaBean

JavaBean is a Java standard specification written in a language class. JavaBean meet class requirements and specific class must be public, and constructor with no arguments, to provide a method for operating a set and get member variable.

public class ClassName{ 
 //成员变量
 //构造方法
  //无参构造方法【必须】 
  //有参构造方法【建议】 
  //成员方法 
  //getXxx() 
  //setXxx() 
} 
//示例代码
public class Student {
    //成员变量
    private String name;
    private int age;

    //构造方法
    public Student() {
    }

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

    //成员方法
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }
}

Anonymous objects


/*
创建对象的标准格式:
类名称 对象名 = new 类名称();

匿名对象就是只有右边的对象,没有左边的名字和赋值运算符。
new 类名称();

注意事项:匿名对象只能使用唯一的一次,下次再用不得不再创建一个新对象。
使用建议:如果确定有一个对象只需要使用唯一的一次,就可以用匿名对象。
 */
public class Demo01Anonymous {

    public static void main(String[] args) {
        // 左边的one就是对象的名字
        Person one = new Person();
        one.name = "高圆圆";
        one.showName(); // 我叫高圆圆
        System.out.println("===============");

        // 匿名对象
        new Person().name = "赵又廷";
        new Person().showName(); // 我叫:null
    }
}
Published 218 original articles · won praise 6 · views 20000 +

Guess you like

Origin blog.csdn.net/u011035397/article/details/104945968