Class person{
private String name;
Private String location;
Person (String name) {
THIS.name = name; Location = "Beijing";
}
Overloading of methods
Person (string name, string location) {
THIS.name = name; this.location = location;
}
Public String info () {
Return "Name:" + name + "Location:" + location;
}
}
Class Teacher extends Person {
Private String captial;
Teacher (string name, String captial) {
This (name, "Beijing", captial);
}
Teacher (string n, String l, String captial) {
Super (N,L);
This.captial = captial;
}
Rewrite
Public String info () {
return Super.info () + "captial:" + captial;
}
}
Class Student extends Person {
Private String School;
Student (string name, string school) {
This (name, "Beijing", school);
}
Student (string n, String l, string school) {
Super (N,L);
This.school = School;
}
Public String info () {
return Super.info () + "School:" + School;
}
}
public class Teststudentandteacher {
public static void Main (String []args) {
person P1 = new Person ("A");
person P2 = new Person ("B", "Shanghai");
Student S1 = new Student ("C", "S1");
Student s2 = new Student ("C", "Shanghai", "S2");
Teacher T1 = new Teacher ("C", "profession");
System.out.println (P1.info ());
System.out.println (P2.info ());
System.out.println (S1.info ());
System.out.println (S2.info ());
System.out.println (T1.info ());
}
}
Object-oriented 5 (overloading, overriding, inheriting of methods)