Home > database >  inheritance
inheritance

Time:10-18

Package the Class;

Public class Father {
String name;
String sex;
int age;
Public void print () {
This. Name="Ren Minghua";
This. Sex="male";
Enclosing the age=46;
System. The out. Print (enclosing name);
}
Public static void main (String [] args) {
Father Father=new Father ();
String name=father. Name;
Father. Print ();
}
}



Package the Class;

Public class Son extends Father {
Public String getName () {
Return this. The name;
}
Public void elegantly-named setName (String name) {
If (" "equals (name)) {
} else {
this.name=name;
}
}
Public int getAge () {
Return the age;
}
Public void setAge (int age) {
this.age=age;
}
Public static void main (String [] args) {
Son Son=new Son ();
Son. SetAge (20);
Son. GetAge ();
Son. Elegantly-named setName (" ");
Son. The getName ();
System. The out. Print (son. The getName ());
Son. The print ();
}
}

CodePudding user response: