Home > Back-end >  The static method of how to do
The static method of how to do

Time:01-07

Static methods showBehavior (Object obj), the body of the implementation: if obj is Student class instance is called study () method, if the call instance obj is the Teacher class is called the () method


How do I call this I won't, obj I don't know what ah, everything else wrote, is a static method that is unlikely to write,


Attach the original topic: (a) write the superclass Personl, contains the following member variables and methods:
1. The representation of a type String name of the member variable name
2. The integer on behalf of the member variables of age, the age
3. Specify the name and the name of a constructor
4. Specify the name and the name and age, the age of constructor
5. The member variable name and age of get and set methods
6. Accommodation method of live (), the method to realize the console output "each have lived"

(2) by the Person derived subclasses the Teacher, the Teacher the class is as follows:
1. The increase of type String representing teachers working member variable teaId,
2. Specify gonghaowu teaId name and the name of a constructor that calls the superclass constructor, whose name is specified to the staff name initialization,
3. The staff working teaId get and set methods
4. Rewrite the accommodation methods live (), output "teacher living in their own home or staff dormitory"
5. Write teaching called the () method, the output "teacher prepares a lesson every day, class and batch jobs"

(3) by a Perosn derived subclasses Student students, the class is as follows:
1. The increase of type String representing a member of the student id variable stuId,
2. Specify the student name and student id stuId name constructor, calls the superclass constructor, whose name is specified to the student's name initialization,
3. Specify the student id stuId, name the name and age, the age structure method, call the superclass of the specified name and age structure method,
4. Student id stuId get and set methods,
5. Rewrite the accommodation methods live (), output "students live in the dormitory of many"
6. Write learning study () method, the output "every day in class, students learn and do your homework"

(4) write a test class TestPerson, this class contains:
1. A static method display Person (p), the method body called the Person class instance p live () method
2. Static methods showBehavior (Object obj), method of implementation: if obj is Student class instance is called study () method, if the call instance obj is the Teacher class is called the () method

(5) write a test program:
1. TestPerson class to write the main method is the main, the main method to implement the following functions:
2. Create a Person, the Person class object name is "Sophia", age of 30.
3. Create a Student class object Student, Student Student id as "S001", the name is "Peter", the age of 20.
4. Create a class object the Teacher, the Teacher, the Teacher job number is "19111", name for "Tom," age of 40.
5. With object, the student and the teacher to the actual parameters to call a static method dispaly,
6. With object, the student and the teacher as the actual parameter to invoke static methods showBehavior,

CodePudding user response:

The static method is the simplest violence
Add the static keyword
Call, direct:
The name of the class. The method name
You can call

CodePudding user response:

Static method is to use static modification method, which can be called directly using the class, don't have to initialize the object
Common method to initialize an object, using the class by object method again
  • Related