Java object-oriented optimization package 1_this (self Python in)

1. Class

package cn.itcast.day06.demo03;

/*
Problem Description: When defining the age of Person, can not prevent unreasonable value is set to come in.
Solution: The private key will need to be protected member variables can be modified.

Once the private use of modified, then this class which can still be freely accessible.
but! Outside of this range beyond the category no longer directly accessible.

Indirect access private member variables, is the definition of a pair of children Getter / Setter methods

It must be called setXxx or getXxx naming rules.
For Getter, the parameters have not, return type and the corresponding member variable;
For Setter, you can not return a value, parameter types, and the corresponding member variable.
 */
public class Person {

    String name; // Name
    private int age; // Age

    public void show() {
        System.out.println ( "My name:" + name + ", Age:" + age);
    }

    // This method members, set up specifically for the age data
    public void setAge(int num) {
        if (num <100 && num> = 9) {// if it is reasonably
            age = num;
        } else {
            System.out.println ( "Data unreasonable!");
        }
    }

    // This method members, specifically whisper obtain age data
    public int getAge() {
        return age;
    }

}

2. Object

package cn.itcast.day06.demo04;

public class Demo01Person {

    public static void main(String[] args) {
        Person person = new Person();
        // set my own name
        person.name = "Wangjianlin";
        person.sayHello ( "Sicong");

        System.out.println (person); // address value
    }

}
package cn.itcast.day06.demo04;

public class Demo02Student {

    public static void main(String[] args) {
        Student stu1 = new Student (); // constructor with no arguments
        System.out.println("============");

        Student stu2 = new Student ( "Zhao Liying", 20); // whole configuration parameters
        System.out.println("姓名:" + stu2.getName() + ",年龄:" + stu2.getAge());
        // If you need to change the contents of data among member variable objects, still need to use the method setXxx
        stu2.setAge (21); // change the age
        System.out.println("姓名:" + stu2.getName() + ",年龄:" + stu2.getAge());

    }

}
package cn.itcast.day06.demo04;

/*
When the class member variables and local variables of the same name of the method, in accordance with "the principle of proximity" preferentially use local variables.
If you need to access the member variables of this class which requires using the format:
this. member variable name

"By the method who called, who is this."
 */
public class Person {

    String name; // my own name

    // parameter name is the name of the other party
    // member variable name is his name
    public void sayHello(String name) {
        System.out.println (name + "., Hello I am a" + this.name);
        System.out.println(this);
    }

}

  

 

Guess you like

Origin www.cnblogs.com/yzg-14/p/12190070.html