Instance Member calls for instantiation and member Methods
Public class OOp_02 {// This is a class modified with public, and only one class name must be the same as the file name, or the class entry
Public static void main (String [] args) {// This is the main method. It can be said that all programs are executed in the main method.
// Create a Customer object;
Customer c = new Customer (); // create a Customer Class Object
// An error is returned if you directly obtain the object.
// System. out. println (c. age );
// Obtain by Method
System. out. println (c. getAge (); // call the member method in the output (when calling the member method: Object Name. method name)
// Assign a value. An error is returned if invalid data is assigned.
// An error is returned when values are directly assigned.
// C. age = 10;
// Method assignment. If the data is invalid, an error is returned. Call the method directly.
// C. setAge (-5 );
// If the method value is valid, an error is returned.
C. setAge (50); // call the member variable with the object name, and the real parameter of the uploaded object is 50
System. out. println (c. getAge (); // call the member method in the output,
}
}
Class Customer {// create a class
Private int age; // use the int data type to declare a member variable age and declare it as private. It is not accessible to the outside. The member variables are declared in the class body, and class are a level, so the call should use the object name, but the Declaration here is private, so external access cannot be called
Public void setAge (int _ age) {// create a member method, and declare local variables using the int data type in the form parameter list.
If (_ age <0 | _ age> 100) {// This is the judgment. if the input value is smaller than 0 or greater than 100, the following Java statement is executed.
System. out. println ("invalid age"); // output
Return; // return value. Because the method setting does not return the void value, but the return function has the function of terminating the function, the entire function will be terminated as long as it is run here.
}
Age = _ age; // assign a value to age,
}
Public int getAge () {// create a member method and return an int Data Type
Return age; // the return value is age and all functions are terminated.
}
}
This is a simple example and related comments. I have explained it in detail as much as possible. I wrote it once to deepen my memory of my knowledge and explained it only according to my own understanding, let's take a look.