Java basics to improve the in-depth understanding of the three major features of Java

Encapsulation

Encapsulation in Java is to privatize the properties of an object, while providing some interfaces (methods) for the outside world to call for interacting with the outside world.
To put it simply, the attributes in the class have their access permissions set to private, and then provide methods with public access permissions (get methods or set methods or other methods) for external calls according to business needs.
Of course, this principle is not a dogma. In some business requirements, it is easier to set the attribute access permissions to public. However, I still support the above principle.

Not much to say, give a chestnut

//我们使用两种不同的方式来实现一个Person类

//1.将类中的属性设置为private,暴露set方法和get方法给外界
public class Person {
    
    
        /*
         * 对属性的封装
         * 一个人的姓名、性别、年龄都是这个人的私有属性
         */
        private String name ;
        private String sex ;
        private int age ;

        /*
         * setter()、getter()是该对象对外开发的接口
         */
        public String getName() {
    
    
           return name;
       }

        public void setName(String name) {
    
    
           this.name = name;
       }

        public String getSex() {
    
    
            return sex;
        }

        public void setSex(String sex) {
    
    
            this.sex = sex;
        }

        public int getAge() {
    
    
            return age;
        } 
    }

//2.将类中的属性设置为public,外界直接调用属性来操作该类
 public class Person {
    
    
        public String name ;
        public String sex ;
        public int age ;
    }

Which of the above is better or worse? It's actually hard to tell. If you consider the amount of code and whether it is easy to read, the second method is the proper king. But if you consider the degree of easy expansion, the first method is the optimal solution. The reason is that it has a method, and since there is a method, it can make logical judgments. For example, we can make a logical judgment for the assignment of the outside world in the setAga method. Have you ever seen someone whose age is 200? If you use the first method, just assign it directly. When you maintain or pull data from the database in the future, this data will be very strange. Is this right or wrong? Is there a problem with the database, or is it caused by an abnormal network? Wait for a bunch of questions.

public void setAge(int age) {
    
    
            if(age > 200){
    
    
                System.out.println("ERROR:error age input....");    //提示錯誤信息
            }else{
    
    
                this.age = age;
            }
        }

Therefore, this is also the main reason why I prefer to use the second way to implement a class.

inherit

Inheritance describes the "is-a" relationship. If there are two objects A and B, if it can be described as "A is B", then it can mean that A inherits B, where B is called the parent class by the inheritor , A is called a subclass by the successor.

In fact, the inheritor is a specialization of the inherited. In addition to the characteristics of the inherited, it also has its own unique characteristics. At the same time, in the inheritance relationship, the inheritor can completely replace the inherited, but not vice versa. For example, we can say that cats are animals, but we cannot say that animals are cats. For this we call it an upward transformation .

Three inherited characteristics:
1. The child class has non-private properties and methods of the parent class.
2. The subclass can have its own attributes and methods, that is, the subclass can extend the parent class.
3. The subclass can implement the method of the parent class in its own way.

Of course, when it comes to inheritance, these three things must be indispensable: the constructor, the protected keyword, and the up-casting.

Constructor

Subclasses can inherit the properties and methods of the parent class. In addition to those private, there is another thing that the subclass cannot inherit—the constructor. For the constructor, it can only be called, not inherited. To call the construction method of the parent class, we can use super().
For subclasses only, the correct initialization of the constructor is very important, and if and only if there is only one method to ensure this: the parent class constructor is called in the constructor to complete the initialization, and the parent class constructor has the execution parent All the capabilities required for class initialization.
Insert picture description here

protected keyword

The private access modifier is the best choice for encapsulation, but this is only based on the ideal world. Sometimes we need such a requirement: we need to hide certain things from this world as much as possible, but still allow children Members of the class to access them. At this time, you need to use protected.

For protected, it indicates that as far as the class user is concerned, he is private, but as far as any subclass inherits from this class or any other class located in the same package, he can access it.

Upward transformation

Converting a child class to a parent class is generally referred to as up-casting. Since up-casting is a conversion from a special type to a more general type, it is always safe, and the only possibility of change is the loss of attributes and methods. The reference of the parent class type can call all the properties and methods defined in the parent class, and it is out of reach for the methods and properties that only exist in the subclass.

Polymorphism

The so-called polymorphism refers to the specific type pointed to by the reference variable defined in the program and the method call issued through the reference variable is not determined during programming, but is determined during the running of the program, that is, which reference variable will point to. The instance object of the class, the method in which the method call issued by the reference variable is the method implemented in the class, can only be determined during the running of the program. Because the specific class is determined when the program is running, the reference variable can be bound to a variety of different class implementations without modifying the source code, so that the specific method called by the reference will change accordingly, that is, it will not be modified. The program code can change the specific code bound when the program is running, so that the program can select multiple running states, which is polymorphism.

Insert picture description here
Insert picture description here
The realization of polymorphism
Insert picture description here
Insert picture description here
Insert picture description here
Classic case: http://blog.csdn.net/thinkGhoster/archive/2008/04/19/2307001.aspx.

Guess you like

Origin blog.csdn.net/qq_36828822/article/details/109515034