Java beginners must see! Java encapsulation and inheritance of the case, you learn Java is very helpful!

Time:2020-12-21

1、 Code and effect drawing

1. Package

Case: Requirement Description

Using encapsulation to realize penguin‘s correct input health value and intimacy of electronic pet system

Ensure the validity of health value (0-100), otherwise take the default value of 60

Otherwise, the value of intimacy is taken as 100-60

The code is as follows (example)

package work1;
package work1;
package work1;

Java beginners must see! Java encapsulation and inheritance of the case, you learn Java is very helpful!

2. Inheritance

Case: use inheritance to optimize the electronic pet system, extract the parent class, create the subclass, and use super to call the parent class construction method in the subclass.

The code is as follows (example)

package work3;
package work3;
package work3;
package work3;

Java beginners must see! Java encapsulation and inheritance of the case, you learn Java is very helpful!

summary

The above is the content of encapsulation and inheritance case, mainly using the method of encapsulation and inheritance.

Welcome to the official account: the bright future, the first line Java interview question summary, the knowledge point learning mind Guide + a 300 page PDF document’s Java core knowledge point summary!