catalogue
2. How to realize encapsulation
1. Solve the conflict between member variable and local variable names
2. Call the member method through this
3. Call the construction method through this
Definition of construction method
Overloading of construction methods
In Java, there are four access levels for classes, member methods and attributes: private, default, protected and public
- Private (current class access level). If a member of a class is modified by private, the member can only be accessed by other members of the class, but other classes cannot directly access it
- Default (package access level). If a class or class member is not decorated with any control character, it is called the default access level. This class or class member can only be accessed by other classes in the package
- Protected (subclass access level): if a member of a class is modified by protected, the member can be accessed by other classes in the same package or subclasses of the class in other packages
- Public (public access level): if a class or class member is decorated with public, the class or class member can be accessed by all classes, whether the accessed class and the accessed class are in the same package or not
Class encapsulation
1. Why package
Encapsulation: attributes and methods are placed inside the class, and the attributes or methods are accessed through the object to hide the implementation details of the function Of course, you can also set access permissions
The meaning of encapsulation is to avoid exposing implementation details to the outside as much as possible, and only provide individual interfaces for users to call. In this way, when its own logic changes, it will not destroy the logic of the user or force the user to modify its own logic, but only need to modify its own code
The essence of encapsulation is to encapsulate the attributes and methods related to things in a class. When we call a class to create an instance, we don't need to care about the code details inside the class. It's equivalent to a black box. We just need that the instance (black box) can give the results we want.
2. How to realize encapsulation
When defining a class, privatize the properties in the class. The private properties can only be accessed in the class in which they are located. If the external wants to access the property, you need to provide some public methods
class Print{ private int age; private String name; public int fun() { age=9; return age; //private decoration. Age can only be used in the class that defines age. Other classes need to borrow public methods } } public class texe { public static void main(String[] args) { Print p=new Print(); //System.out.println(p.age); //private decoration. Age can only be used in the class that defines age. Other classes need to borrow public methods System.out.println(p.fun()); } }
this keyword
1. Solve the conflict between member variable and local variable names
class Sout{ int age=8;//Member variable public void print(int age) {//age is a local variable, parameter 9 age = age;//When a local variable has the same name as a member variable, the local variable takes precedence over the global variable //9 = 9 member variable is still 8 this. age = age;//this refers to a member variable // Change the member variable to 9 } } public class texe { public static void main(String[] args) { Sout p=new Sout(); p.print(9); System.out.println(p.age); }
2. Call the member method through this
class Person { int age=0; public void print(int age){ this.age=age; System.out.println(this.age); } public void speak(int age){ this.print(2); } } class text { public static void main(String[] args) { Person p1 = new Person(); p1.speak(3); } }
The speak method is called in the main method, and the print () member method is invoked in the speak method, this. in the print () method. Age calls the member variable, which is changed to the value of formal parameter 2
3. Call the construction method through this
The construction method is automatically invoked when instantiating an object. In a program, it is not possible to call a construction method just like calling common methods. However, this can be used to call other construction methods in a construction method.
class Person { public Person(){ this(2); System.out.println("This is a construction method without parameters"); } public Person(int age){ System.out.println("This is a construction method with parameters"+"Incoming parameter="+age); } } class text { public static void main(String[] args) { Person p1 = new Person(); } }
When creating an object, the construction method without parameters is automatically called. First execute this (2) and call the construction method with parameters
Construction method
Definition of construction method
If you need to assign values to the attributes of an object while instantiating it, you can use the construction method.
A constructor is a special member of a class. It will be called automatically when the class instantiates an object.
Definition of construction method
[modifier] method name (method parameter){
Method body
}
requirement:
- The method name is the same as the class name
- There is no return type before the method name
- You cannot use return to return a value, but you can write a return statement to end the method
class Person{ int age; public Person (){ System.out.println("This is a construction method"); } } class text { public static void main(String[] args) { Person p=new Person(); } }
In this code, it prints: This is a constructor
In the Person class, a parameterless constructor, {Person, is defined to execute Person p=new Person(); the constructor will be called automatically when the Person object is created
Overloading of construction methods
Overloads can also be formed between construction methods
class Person{ int age; public Person (){ System.out.println("This is a construction method"); } public Person (int s){ System.out.println("This is a parametric construction method"+s); } } class text { public static void main(String[] args) { Person p = new Person(); Person p1 = new Person(3); } }
Every class in java has at least one constructor: if a constructor is not defined in a class, the system will automatically create a default constructor for the class. The default constructor has no parameters and no code in the method body. Once a constructor is defined for the class, the system will not provide a default parameterless constructor
class Person{ int age; public Person (int s){ System.out.println("This is a parametric construction method"+s); } } class text { public static void main(String[] args) { Person p1 = new Person(3); Person p = new Person(); } }
Reason: a construction method with parameters has been created in the Person class. At this time, the system will not provide a default nonparametric construction method. Therefore, if a construction method with parameters has been defined in a class, it is best to define a construction method without parameters
class Person{ int age; public Person (int s){ System.out.println("This is a parametric construction method"+s); } public Person (){ System.out.println("This is a parametric construction method"); } } class text { public static void main(String[] args) { Person p1 = new Person(3); Person p = new Person(); } }
Code block
In a java class, several lines of code enclosed by {} are collectively referred to as code blocks
Static code block
Code blocks modified by static are called static code blocks
Instance code block
class Person { public Person(){ System.out.println("This is a construction method without parameters"); } { System.out.println("This is an example code block"); } static{ System.out.println("This is a static block of code"); } } class text { public static void main(String[] args) { Person p1 = new Person(); System.out.println("=============="); Person p = new Person(); } }
class Person { static void print(){ System.out.println("This is a construction method without parameters"); } static { System.out.println("This is a static block of code"); } { System.out.println("This is an example code block"); } } class text { public static void main(String[] args) { Person.print(); } }
Conclusion: static code blocks will be loaded even if they do not instantiate objects, and will only be loaded once.
When loading a class, the static code block in the class will be loaded first, followed by the instance code block, followed by other methods (independent of relative position)
However, if they are all static variables / global variables, it is related to the relative position
class Person { public static int age = 20; static { age = 10; } } class text { public static void main(String[] args) { Person p=new Person(); System.out.println(p.age); } }
Results: 10, that is, execute public static int age = 20 first;
class Person { static { age = 10; } public static int age = 20; } class text { public static void main(String[] args) { Person p=new Person(); System.out.println(p.age); } }
Result: 20, i.e. public static int age = 20 after execution;
However, there is a special case, when not initialized, the result is independent of the relative order
class Person { static { age =5; } public static int age ; }
class Person { public static int age ; static { age =5; } }
The results are all 5