Constructs in java, encapsulation

Today I will tell you about the construction and encapsulation in object-oriented;

Construction: The construction method has the following characteristics: 1. The method name is consistent with the class name. 2. No return type. The next few construction styles, go directly to the code:

//This is a pet class with one property: name (name)
public class Pet {
  String name;
//Construction with no parameters
public Pet(){
  this.name = "Husky";
}
//Construction of
public Pet with parameters (String name1){
  this.name = name1; //I don't use this.name = name; this is fine, I'm afraid you don't understand;
}


//Here I built a test method directly in this
public static void main(String[] args){
  //Here I tested the no-argument construction first, and the system will recognize it automatically. As far as he will automatically call the no-parameter construction
  Pet dog = new Pet();
  System.out.println(dog.name);//Output result: Husky
  //The following is a call to the parameterized construction
  Pet dog2 = new Pet("Labrador");
  System.out.println(dog2.name);//Output result: Labrador
}
}

The above is a simple structure, have you learned it; if you don’t understand, you can ask me, and then go to the next one:

 

Encapsulation: Encapsulation has the following characteristics: to use private, encapsulation and encapsulation, that is, it must be encapsulated, so to use private attributes, other classes must access and modify the encapsulated attributes through the interface to obtain and modify; next Please see the simple code for details:

//This is a car class with two attributes: wheels and weight

The first: no package

public class Vehicle {

  int wheels;//Number of wheels
  double weight;//Car weight
}

I build a test method to access properties in Vehicle:

public class TestVehicle{

  public static void main(String[] args){

    Vehicle c = new Vehicle();

    System.out.println("Number of wheels:"+c.wheels+"Car weight:"+c.weight);//Because the Vehicle class is not encapsulated, all I can directly access its properties;

}

}

There should be no need to say anything if it is not closed.

The second type: encapsulated, as long as it is encapsulated normally, you can see the code of get set;

Package shortcut keys: the first type: as shown below, right-click and see the picture

The second: move to the property to be encapsulated: Alt+? Then there will also be a row of letters with get set and properties that must be set to private

public class Vehicle {

  private int wheels;//Number of wheels

  private double weight;//车重

public int getWheels() {
  return wheels;
}
public void setWheels(int wheels) {
  this.wheels = wheels;
}
public double getWeight() {
  return weight;
}
public void setWeight(double weight) {
  this.weight = weight;
}


}

I build a test method to access properties in Vehicle:

public class TestVehicle{

  public static void main(String[] args){

    Vehicle c = new Vehicle();

    System.out.print(c.getWheels()+" "+c.setWeight());//When accessing his properties at this time, you must call his interface to access, otherwise you can't access it, you need to modify it Through the interface, remember: get means to get, it means to acquire, set means to modify, practice more, it will be easy to understand;

}

}

Finally, let me summarize for you. The construction is that the method name and the class name are the same. The encapsulation is to have: get and set.

I originally wanted to inherit, and also talked about overloading and polymorphism, but I didn't have time to write it;

I'll explain it to you later tonight or tomorrow;

I will work hard to learn and keep updating. Thank you for watching. If you don't understand, you can privately message me and give me a follow. Thank you. Bye, see you next time;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324973056&siteId=291194637