I. Code and effect drawing

1. The encapsulation

Case: Requirement Description Penguin species using encapsulation to implement electronic pet system correctly input health value and affinity density to ensure the validity of health value (0-100), otherwise use the default value 60 to ensure the validity of intimacy (0-100), otherwise use the default value 60 code as follows (example) :

package work1; public class Dog { private String name; private String sex; private int love; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(int sex) {if(sex==1){this.sex = "Q "; }else{this.sex = "Q "; } } public int getLove() { return love; } public void setLove(int love) { if(love>=0&&love<=100){ this.love = love; }else {system.out.println (" Intimacy should be between 0 and 100, default is 60"); this.love = 60; }}}Copy the code
package work1; public class Penguin { private String name; private String sex; private int health; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(int sex) {if(sex==1){this.sex = "Q "; }else{this.sex = "Q "; } } public int getHealth() { return health; } public void setHealth(int health) { if(health>=0&&health<=100){ this.health = health; }else {system.out.println (" Health value should be between 0 and 100, default is 60"); this.health = 60; }}}Copy the code
package work1; import java.util.Scanner; public class Test1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(" Welcome to the pet shop! \n Please enter the name of the pet to adopt: "); String name = sc.next(); System.out.println(" please select the pet type to adopt :(1). Dog 2. Penguin) "); int i = sc.nextInt(); If (I ==1){// Dog Dog = new Dog(); dog.setName(name); System.out.println(" please select the gender of the dog :(1. dog.setSex(sc.nextInt()); System.out.println(" Please enter the closeness of the dog (between 1 and 100) : "); dog.setLove(sc.nextInt()); System. Out. Println (" pet confession: \ n my name is "+ name +", the value of health is "+ 0 +", and master the "+" intimacy "+ t getLove () +", my gender is "+ t getSex ()); }else {// Penguin p = new Penguin(); p.setName(name); System.out.println(" please select penguin gender :(1. p.setSex(sc.nextInt()); System.out.println(" Please input penguin health value (between 1 and 100) : "); p.setHealth(sc.nextInt()); System. Out. Println (" pet confession: \ n my name is "+ name +", the value of health is "+ p.g etHealth () +", and master the "+" intimacy "+ 0 +", my gender is "+ p.g etSex ()); }}}Copy the code

2. The inheritance

Example: Optimize the tamagotchi system using inheritance, extract the parent class, create a subclass, and call the parent constructor using super in the subclass. The code is as follows (example) :

package work3; Public class Dog extends Pet {@override public void show() {system.out.println (" \ n my name is "+ enclosing getName () +", health value is "+ 0 +", and master the "+" intimacy is "+ enclosing getLove () +", my gender is "+ enclosing getSex ()); } private int love; public Dog(String name, int sex, int love) { super(name, sex); this.love = love; } public int getLove() { return love; } public void setLove(int love) { if(love>=0&&love<=100){ this.love = love; }else {system.out.println (" Intimacy should be between 0 and 100, default is 60"); this.love = 60; }}}Copy the code
package work3; Public class Penguin extends Pet {@override public void show() {system.out.println (" \ n my name is "+ enclosing getName () +", health value is "+ enclosing getHealth () +", and the master of the "+" intimacy "+ 0 +", my gender is "+ enclosing getSex ()); } private int health; public Penguin(String name, int sex, int health) { super(name, sex); this.health = health; } public int getHealth() { return health; } public void setHealth(int health) { if(health>=0&&health<=100){ this.health = health; }else {system.out.println (" Health value should be between 0 and 100, default is 60"); this.health = 60; }}}Copy the code
package work3; public class Pet { private String name; private String sex; public void show(){ } public Pet(String name, int sex) { this.name = name; If (sex==1){this.sex = "Q "; }else{this.sex = "Q "; } } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(int sex) {if(sex==1){this.sex = "Q "; }else{this.sex = "Q "; }}}Copy the code
package work3; import java.util.Scanner; import static java.lang.System.out; public class Test2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Println (" Welcome to pet shop! \n Please enter the name of the pet to adopt: "); String name = sc.next(); Println (" please select the type of pet you want to adopt :(1. Penguin) "); int i = sc.nextInt(); If (I = = 1) {/ / the dog dog out. Println (" please select the sex of the dog: (1) Q wang 2 Q sister) "); int sex=sc.nextInt(); Out. Println (" Please enter the closeness of the dog (between 1 and 100) : "); Dog dog = new Dog(name, sex, sc.nextInt()); dog.show(); }else {// penguin out.println(" please select penguin gender :(1. int sex=sc.nextInt(); Println (" Please input penguin health value (between 1 and 100) : "); Penguin p = new Penguin(name, sex, sc.nextInt()); p.show(); }}}Copy the code

conclusion

The above is the content of the encapsulation and inheritance case, mainly using the encapsulation and inheritance method. Welcome to pay attention to the public number: the future has light, receive a line of large factory Java interview questions summary + the knowledge points learning thinking guide + a 300 page PDF document Java core knowledge points summary!