java package, this keyword, setter and getter methods 23

Package

Why package

  • At this stage we define the properties of objects can easily access, arbitrary changes of control, the idea is to package it hidden member variables.
    What is the package
  • Encapsulation is "reasonable hidden, reasonable exposure."
  • Basic Implementation Specification: package is generally the member variables (attributes) to hide, to provide reasonable exposure method accessed.
    Step package
  1. You need to use the private key attribute modification.
  • private: private, hidden meaning. Modified members can only be accessed directly in the current class. Otherwise error!
  1. Need to provide a set of public getter and setter methods expose the values ​​of member variables and assignment.
  • getter methods: get + member variables capitalized Returns the value of the member variable.
  • setter method: set + member variables capitalized assignment method member variable.
    The role of the package
    can improve safety, and component reusability of code.
    Encapsulation is an object-oriented features and specifications
    even though the code pointless after we still have to write the code style (private member variables, methods exposed!)
public class PackageDemo {
    public static void main(String[] args) {
        Student boNiu = new Student();
        boNiu.setAge(21); // 赋值,这里的数据必须满足0 - 200之间,否则赋值会失败!
        boNiu.setName("赵丽颖");
        System.out.println(boNiu.getAge()); // 取值
        System.out.println(boNiu.getName()); // 取值
        boNiu.study();
    }
}
class Student{
    private String name ; // 只能在本类中访问
    private int age ; // 只能在本类中访问

    public void setAge(int a){
        if(a > 0 && a < 200){
            age = a;
        }else{
            System.out.println("您的年龄输入有误,请确认!");
        }
    }

    public int getAge(){
        return age;
    }

    public void setName(String n){
        name = n ;
    }

    public String getName(){
        return name;
    }

    public void study(){
        System.out.println("好好学习,认真听课~~~~");
    }
}

this keyword

The name of the parameter set method we defined not do "see to know the name meaning" parameter name no real meaning. But the parameter names when write meaningful and member variable name conflict, resulting in the assignment of data failure.
For example: n this definition is not intended to see the name known, but if the changed name n then repeated
public void the setName (String n) {
name = n;
}
method to solve this keyword

  • this represents the address of the current object.
  • In this method, who calls this method, this represents whom.
  • used in this method can distinguish variables are local variables or access member variables: see the name in order to achieve local variables known meaning, it will not conflict!
public class PackageDemo {
    public static void main(String[] args) {
        // 1.创建对象
        Student wangLe = new Student();
        wangLe.setName("王乐"); // 赋值
        wangLe.setAge(19);  // 赋值
        System.out.println(wangLe.getName()); // 取值
        System.out.println(wangLe.getAge());  // 取值
        wangLe.study();
    }
}
class Student{
    private String name ; // 只能在本类中访问
    private int age ; // 只能在本类中访问
    // public 是公开的含义,可以直接暴露出去被访问的。
    // this在Java中代表了当前对象的地址引用。
    // this在方法中,谁调用这个方法,this就代表了谁。
    // this用在方法中可以区分变量是访问局部变量还是访问成员变量。
    public void setAge(int age){
        this.age = age; // this.age = 19   wangLe.age = 19
    }

    public int getAge(){
        return age;
    }

    public void setName(String name){
        this.name = name ; // this.name = 王乐  wangLe.name = 王乐
    }

    public String getName(){
        return name;
    }

    public void study(){
        System.out.println("好好学习,认真听课~~~~");
    }
}

Note:
setter and getter methods can be directly generated compiler
right click -> select Generate-> select Setter and Gertter-> Hold down the Shift key and click on the last one directly select them all.

Published 34 original articles · won praise 16 · views 281

Guess you like

Origin blog.csdn.net/qq_41005604/article/details/105249978