Override equals and toString examples

{class TestTeacher public 
	public static void main (String [] args) { 
		the Person new new the Person P1 = ( "A"); 
		the Person new new the Person X = ( "F"); ///////////// //////// 
		the Person new new the Person P2 = ( "B", "Shanghai"); 
		the Person new new the Person P3 = ( "A"); 
		
		
		Student Student new new S1 = ( "C", "Sl"); 
		Student Student = new new S2 ( "C", "Shanghai", "S2"); 
		
		System.out.println (p1.equals (P3)); 
		System.out.println (== P1 P3); // verification class equals rewriting implementation 
		System.out.println ( "toString person verification result is" + x); // verify toString rewriting 
		System.out.println (p1.info ()); 
		System.out.println (P2 .info ()); 
		System.out.println (s1.info ());
		System.out.println(s2.info());
		
		T1 = new new Teacher Teacher ( "D", "Professor"); 
		System.out.println (t1.info ()); 
	} 
} 

// create a Person class 
class Person { 
	
	// definition of member variables 
	Private String name; 
	Private String LOCATION ; 
	
	// Constructors the Person 
	the Person (String name) { 
		this.name = name; 
		LOCATION = "Beijing";	 
	} 
	
	// constructor overloads the Person 
	the Person (String name, String LOCATION) { 
		this.name = name; 
		this.location LOCATION =; 
	} 
	
	// the Person conventional method 
	public String info () { 
		return "name:" + name + "LOCATION" + LOCATION; 
	} 
	
	// methods override equals the Person  
	public boolean equals (Object obj) {
		IF (obj == null) return to false; 
		the else { 
			IF (the instanceof the Person obj) { 
				the Person P = (the Person) obj; 
				IF (this.name == == name && this.location LOCATION) { 
					return to true; 
				} 
			} 
		} 
		to false return; 
	} 
	
	// the Person toString method override 
	public String toString () { 
		return "the I'm Cool the Person A"; 
	} 
} 


// class subclasses inherit the parent class definition 
class the extends the Person Teacher { 
	Private String Capital; // define member variable 
	
	// Teacher constructor 
	Teacher (String name, String Capital) { 
		the this (name, "Beijing", Capital); 
	} 
	
	// Teacher constructor overloads 
	Teacher (String n, String l, String capital) {
		Super (n-, L); 
		this.capital = Capital; 
		
	} 
	 
	// common method Teacher
	public String info () { 
		return super.info () + "Capital:" + Capital; 
	} 
} 











// class inherits a base class 
class Student the extends the Person { 
	Private School String ; 
	
	// Constructors Student 
	Student (String name, String School) { 
		the this (name, School, "Beijing"); 
	} 
	
	// constructor overloads Student 
	Student (n-String, String L, String School) { 
		Super (n-, L); 
		this.school = School; 
	} 
	
	// conventional method 
	public String info () { 
		return super.info () + "School:" School +; 
	} 
}

 

Guess you like

Origin www.cnblogs.com/lsswudi/p/11265270.html