Getting started with JavaSE 24: Java object-oriented supplement

Source: Internet
Author: User

Getting started with JavaSE 24: Java object-oriented supplement
I. Object Class in Java

The Object class is the parent class of all Java classes. If a class does not use the extends keyword to explicitly identify the inheritance of another class, this class defaults

Inherits the Object class.

Public class Person {//} // equivalent to public class Person extends Object {//}

Methods In the Object class, suitable for all subclasses.

1) toString () method

The public String toString () method is defined in the Object class. Its return value is of the String type. It describes the information about the current Object.

When a String is connected to its data type (for example, System. ouy. println ("info" + person), The toString () method of the object class is automatically called. Or

To rewrite the toString () method in the User-Defined type as needed

Instance:

public class Test{public static void main(String[] arge){Dog d = new Dog();System.out.println(d);System.out.println("d:"+d);System.out.println("d:"+d.toString());}}class Dog{public String toString(){return "I am a dog";}}

Running result:

2) equals () method

Compare whether the object reference points to the same memory address. Generally, when comparing two objects, check whether their values are consistent.

Rewrite.

The Object class is defined as public boolean equals (Object obj) to define the Object type.

Instance:

Public class Test {public static void main (String [] arge) {String s1 = new String ("hello"); String s2 = new String ("hello"); System. out. println (s1 = s2); // compare the object address System. out. println (s1.equals (s2); // compare object content }}

Running result:

Casting)

A base class's reference type variable can point to its subclass object;

The reference of a base class cannot access the new members (attributes and methods) of its subclass object );

You can use the instanceof class name to determine whether the object pointed to by the referenced variable belongs to this class or its subclass.

The subclass objects can be used as the base class objects, which are called upcasting, and vice versa ).

The upward transformation refers to the reference of the base class or parent class pointing to the subclass object.

We have encountered a lot in the previous example. I will not talk about it here. For details, refer to: JavaSE getting started 18: Java object-oriented polymorphism.

Three dynamic binding (pool binding) and Polymorphism

Dynamic binding is used to determine the actual type of the referenced object during the execution period (rather than the compilation period) and call the corresponding method based on the actual type.

In the following example, the enjoy () method is called based on the actual type referenced by pet, the member variable of the Lady object. That is, your new is

That type calls the enjoy () method of this type.

Instance code:

Class Animal {private String name; // constructor Animal (String name) {this. name = name;} public void enjoy () {System. out. println ("cry ...... ") ;}} class Cat extends Animal {private String eyesColor; // constructor Cat (String name, String eyesColor) {// call the constructor super (name) of the parent class ); this. eyesColor = eyesColor;} // rewrite the enjoy () method of the parent class Animal public void enjoy () {System. out. println ("cat call ...... ") ;}} class Dog extends Animal {private String furColor; // constructor Dog (String name, String furColor) {// call the constructor super (name) of the parent class ); this. furColor = furColor;} // rewrite the enjoy () method public void enjoy () {System. out. println ("dog call ...... ") ;}} class Lady {private String name; // reference type variable member private Animal pet; // constructor Lady (String name, Animal pet) {this. name = name; this. pet = pet;} public void myPetEnjoy () {pet. enjoy () ;}} public class Test {public static void main (String [] arge) {Cat c = new Cat ("catname", "blue "); dog d = new Dog ("dogname", "black"); Lady l1 = new Lady ("l1", c); Lady l2 = new Lady ("l2 ", d); l1.myPetEnjoy (); l2.myPetEnjoy ();}}

Running result:

Extensibility:

Rewrite the preceding example:

Add a Bird class:

Class Bird extends Animal {// constructor Bird () {// call the constructor super ("bird") of the parent class;} // rewrite enjoy () of the parent class Animal () method public void enjoy () {System. out. println ("Birds cry ...... ");}}

Rewrite the main method:

public class Test{public static void main(String[] arge){Cat c = new Cat("catname","blue");Dog d = new Dog("dogname","black");Bird b = new Bird();Lady l1 = new Lady("l1",c);Lady l2 = new Lady("l2",d);Lady l3 = new Lady("l3",b);l1.myPetEnjoy();l2.myPetEnjoy();l3.myPetEnjoy();}}

Running result:

The importance of polymorphism for system scalability

Rewrite birds:

Class Bird extends Animal {private String featherColor; // constructor Bird (String name, String featherColor) {// call the constructor super (name) of the parent class; this. featherColor = featherColor;} // rewrite the enjoy () method public void enjoy () {System. out. println ("Birds cry ...... ");}}

Rewrite the mian method:

 

public class Test{public static void main(String[] arge){Lady l4 = new Lady("l4",new Bird("birdname","green"));l4.myPetEnjoy();}}

Running result:

Polymorphism conditions:

1) There must be inheritance

2) rewrite

3) parent class references to subclass objects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.