JAVA Learning Notes

1. Encapsulation Three main object-oriented f...
1. Encapsulation
2. private keywords
3. this keyword
4. Construction methods

1. Encapsulation

Three main object-oriented features: encapsulation, inheritance, polymorphism

Encapsulation in Java:
1. The method is an encapsulation
2. Keyword private is also an encapsulation

Encapsulation means hiding details from the outside world

public class Method { public static void main(String[] args) { int[] array = {12, 32, 34, 234, 33}; int max = getMax(array); System.out.println(max); } public static int getMax(int[] array) { int max = array[0]; for (int i = 1; i < array.length; i++) { if(array[i] > max) { max = array[i]; } } return max; } }

Encapsulate the method getMax:

2. private keywords

Encapsulate with the private keyword to privateize attributes in the class.

Once modified with private s, this class is still freely accessible.But!No more direct access beyond this class

Indirect access to private member variables defines a pair of Getter/Setter methods, which must be called setXXX or getXXX
Naming rules:
For setter, there can be no return value, the parameter type corresponds to the member variable
For getter s, there can be no parameters, and the return value type corresponds to the member variable

/* Description of the problem: When defining Person's age, there is no way to prevent unreasonable values from being set Solution: Modify member variables that need to be protected with the private keyword */ public class Person { String name; private int age; public void show(){ System.out.println("My name is" + name + "This year" + age + "year"); } //This member method is designed to set data to age public void setAge(int num) { if(num < 100 && num >= 0) { age = num; } else { System.out.println("Data is not reasonable!"); } } //This member method is designed to obtain age data public int getAge() { return age; } }

For the basic type of boolean, the getter method must be written as isXXX, with the setXXX rule unchanged

3. this keyword

There are three applications of this: calling this class property, calling this class method, representing the current object.

If you need to access member variables in this class, you need to use: this.member variable name.
When local variables of a method and member variables of a class are renamed, local variables are preferred according to the proximity principle.

package Class; /* "this is who calls the method through Person02" */ class Person02 { String name; //The parameter who is the name of the other party (Tom) //The member variable name is your own name (Jerry) public void sayHello(String name) { //System.out.println(name + ", Hello I am" + name);//Tom, Hello I am Tom System.out.println(name + ",Hello I am" + this.name);//Hello, Tom, this is Jerry System.out.println(this);//Class.Person02@4554617c } } public class Person02Use { public static void main(String[] args) { Person02 person = new Person02(); person.name = "Jerry"; person.sayHello("Tom"); System.out.println(person);//Class.Person02@4554617c } }

4. Construction methods

Constructing methods are methods designed to create objects. When we create objects with the new keyword, we are actually calling the construction method, which is called only once when we instantiate a new object.

public class name (parameter type parameter name){ Method Body }

Notice:
1.The name of the constructor must be exactly the same as the class name it is in
2.Construct method does not write return value type, connect void Do not write
3.Construction method cannot return A specific return value
4.If you do not write any construction methods, the compiler will default to giving you a construction method that does nothing with no parameters or method body.
public Student02() {}
5.Once you have written at least one construction method, the compiler will not give it away.
6.Construction methods can be overloaded.

public class Student02 { private String name; private int age; public Student02() { System.out.println("Execute parameterless construction method"); } public Student02(String name,int age) { System.out.println("Execute the full parameter construction method"); 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; }

The main function of a construction method is to initialize objects, so if you want to assign attributes when an object is instantiated, you can use the construction method to do so.

qq_45806995 10 original articles published. 2. Visits 186 Private letter follow

7 February 2020, 23:35 | Views: 4352

Add new comment

For adding a comment, please log in
or create account

0 comments