If that:
Using inheritance writing Person, Student category
- Write the Person class:
- Properties include: date name, age, birth
- Methods: showInfo ();
- Student writing class:
- Properties include: date name, age, birth, school
- Methods: showInfo ();
- study();
- Student class using inheritance optimization.
Ready Go!
first step
Person class
. 1 Package A; 2 // parent class . 3 public class the Person { . 4 // To make hidden variables member (package), to facilitate the rules; otherwise variables are directly assigned. 5 Private String name; 6 Private int Age; 7 Private String Birthday; 8 // ages must be passed in the constructor, because there is no set method does not allow access private variables 9 // If the age is less than 0, the age is 0; otherwise, assigned only to age 10 public the Person ( int age) { . 11 IF (age <0 ) { 12 is the this .age = 0 ; 13 is } The else { 14 the this .age = Age; 15 } 16 } . 17 // To other users to access variables, we assign the variables get and set methods with 18 @ provides a method to get name . 19 public void the setName (String name) { 20 is the this .name = name; 21 is } 22 is public String getName (String name) { 23 is return the this .name; 24 } 25 // generated automatically get, set method 26 is // the Source → 27 publicGetBirthday String () { 28 return Birthday; 29 } 30 public void setBirthday (String Birthday) { 31 is the this .birthday = Birthday; 32 } 33 is // subject method of claim showInfo, print information 34 // may be placed in this code front, easy to read, here for ease of understanding, according to the code writing order list. 35 public void the showInfo () { 36 System.out.println ( "name =" + name + "; Age =" + Age + "; Birthday =" + Birthday); 37 [ } 38 is }
Test category
. 1 Package A; 2 // test class . 3 public class Demo { . 4 public static void main (String [] args) { . 5 // create objects p1, call the constructor 6 // Here, if the Person class, age-free arg constructor, here it is easy to forget the assignment; there must be a method parameter assignment. . 7 the Person P1 = new new the Person (18 is ); . 8 p1.setName ( "Jack" ); . 9 p1.showInfo (); 10 } . 11 }
Output
name=Jack;age=18;birthday=null
The second step
Student writing class
/ *
* After the sub-class inheritance, you need to create a test class for the Student class object constructor
* Meanwhile, the subclass in the member variables inherit the parent class, will create a default constructor,
* This constructor by default ; default constructor calls the parent class, but this time the parent class only the Person (int Age)
* because we add a constructor, so the system will not provide a default constructor for us,
after * so we created the Student class , you need to fill a Person class default constructor.
* /
. 1 Package A; 2 // subclass . 3 public class Student the extends the Person { . 4 // default constructor may be deleted, the system automatically provides . 5 public Student () { . 6 . 7 } . 8 // add attributes meaning of the questions . 9 Private String schoolName; 10 // automatic generation . 11 public String getSchoolName () { 12 is return schoolName; 13 is } 14 public void setSchoolName (String schoolName) { 15 the this .schoolName = schoolName; 16 } . 17 @ The method of adding the meaning of the questions 18 // now need to test the class constructor methods below Student class inherits from the Person class over 19 // parent class name is private variables, can not be used directly, so using the following method call getName 20 // getName () method automatically generates parameters need to be deleted in the parent class and save! ! ! 21 is public void the showInfo () { 22 is // System.out.println ( "name =" + getName () + "; Age =" getAge + () + "; Birthday =" getBirthday + () + "; schooleName =" schoolName +); 23 is System.out.println ( "name =" + getName () + "; Age =" getAge + () + "; Birthday =" getBirthday + () + "; schooleName =" } 25 }
Test category
. 1 Package A; 2 // test class . 3 public class Demo { . 4 public static void main (String [] args) { . 5 // create objects p1, call the constructor 6 // Here, if the Person class, age-free arg constructor, here it is easy to forget the assignment; there must be a method parameter assignment. . 7 the Person P1 = new new the Person (18 is ); . 8 p1.setName ( "Jack" ); . 9 p1.showInfo (); 10 . 11 // distinguish print result 12 is System.out.println ( "******** ********************************************************** " ); 13 14 Student S1 = new new Student (); 15 s1.setName ( "Tom" ); 16 s1.setSchoolName ( "on Dalian" ); . 17 s1.showInfo (); 18 is // Everything in the parent class are subclasses inherited, limited only partially stuff control characters are not allowed to use the direct 19 } 20 }
Person class
Package Penalty for A; // parent class public class the Person { // To make member variables hidden (package) to facilitate the development of the rule of value; otherwise the variable will be assigned directly. Private String name; Private int Age; Private String Birthday; / * * After a subclass inheritance, we need to create an object for the Student category in the test class constructor * Meanwhile, the subclass in the inherited member variable parent class, will create a default constructor, * this constructor will default default constructor calls the parent class, but this time the parent class only the Person (int Age); * because we add a constructor, so the system will not provide us with the default constructor, * so we created the Student class, we need to fill a Person class default constructor. * / Public the Person () { // the TODO Auto-Generated Stub constructor } // Age must be passed in the constructor, because there is no set method does not allow access private variables // If the age is less than 0, the age is 0; otherwise, only to age assigned public the Person ( int age) { IF (age <0 ) { the this = 0 .age ; } the else { the this .age = Age; } } // to other users to access variables, we assign the variables with the get and set methods // provide a method to get name public void the setName (String name) { the this = .name name; } public String getName () { return the this .name; } // generated automatically get, set methods // the Source → public String getBirthday () { return Birthday; } public void setBirthday (String Birthday) { the this .birthday = Birthday; } public int getAge () { return Age; } public void the setAge ( int Age) { the this .age = Age; } // subject the showInfo method claim, the print information // this code may be placed in front, easy to read, here for ease of understanding, according to the code writing order list. public void showInfo() { System.out.println("name="+name+";age="+age+";birthday="+birthday); } }
This, this problem ends.
the third part
According to discuss this question
- Add a variable name and the parent class's name has anything to do with the child class?
- Who's calling the test class name in the end is?
(To be continued)
Today abused dizzy, cynicism (* T_T *)