Javase's Object__java the road

Source: Internet
Author: User

Methods in Obiect: tostring Method

The return value type is a string How to rewrite ToString

The object created normally has a ToString () method
, return the information such as the class name address

Rewrite ToString

Public String toString () {
///This name and age is the property return name+ in this class
, +age;

}

And then call the ToString method again to return this.

That's it, all of that, with the StringBuilder definition, the direct output shows a string.
Gave her the output of ToString.

Rewrite ToString to describe this object. Equals Method

returns a Boolean that compares strings

The dual equals ratio is the same object, and equals is the content of this two object (like it's not like)

Person QQ = Person
is ww = person

if (QQ==WW) {returns false}
if (Qq.equals (WW)) {returns true}



overriding the Equals method
The modification method is used to determine that the current object is not as long as the given object "
/"
publc Boolean equals (Object obj) {
if (obj==null) {
//null). return
false;

}

if (obj==this) {return
true;
}
Fixed on the above.
//Use instanceof to see if obj is Teacher this class
if (obj instanceof Teacher) {
//Convert to Teacher First, then go to the content
//Compare these attributes
Teacher t = (Teacher) obj;
Return T.name.equals (this.name)
&&t.age==this.age
&&t.gender.equals (this.gender);
;

} return

false;
}

Base type with double equals
String strings are not basic types, so they are also compared with equals

The equals of object is compared with double equals, so rewrite

parameter is defined as object, it can be accepted, except for the basic type
Like int long Boolean, and so on, not in the form of objects.
Can not be introduced in polymorphic things.

That solves the basic type to have the corresponding basic type of class
Int-integer
Interger in = new Interger (1);
With this class, you can pass in the object argument.

It's called the packing class.
Allow basic types to participate in object-oriented development

The wrapper class is final and cannot define their subclasses

Number and its main methods

Abstract class number is the parent of Byte, Double, Float, Integer long and short classes
The subclass of number must provide a way to convert the represented numbers to Byte,double,float,int,long and short:
Doublevalue () returns the specified value as a double
Intvalue () returns the specified value as int
Floatvalue () returns the specified value in float form

Return base type:

    public static void Main (string[] args) {
        //TODO auto-generated method stub
        Double d = new double (123.1);
        Change d back to basic type
        d.doublevalue ();
    }

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.