Java overrides the use of ToString and generics

Source: Internet
Author: User
Tags modifier

First, overriding the ToString method in a class

public class Person {private string name;private int age;public person () {}public person (String name, int age) {THIS.name = Name;this.age = age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} @Overridepublic String toString () {//{name=name, age= Age}return "{" + "name=" + getName () + "age=" + getage () + "}";} Override Equals @Overridepublic Boolean equals (Object obj) {//To determine if the memory address is the same if (this = = obj) {  return true;} Determines whether the object passed in obj is null if the type of obj is the same as the      current type if (obj = = NULL | | This.getclass ()! = Obj.getclass ()) {  return false;} Strong-turn type person p  = (person) obj//determines whether the value of the passed Obj object is empty and the value is the same, return  this.age = = P.age  && Objects. Equa LS (this.name,p.name)//judgment value is the same}

Ii. definition and use of generics

  The flexibility to apply data types to different  classes, methods, and interfaces to    pass data types as parameters
  1. Defining and using classes that contain generics  
  define the format:
  modifier class  class name < variable >{} that represents the generic type  
  
  Example:
  //generics use defined positions in the class  after the class name
Public  class Person <e>{
  //generics as parameters in parentheses
Public  Boolean Add (E e) {
  }
  //  with generic method  in alternate method type
Public  E Get (int index) {
  
  }
  }
  2. Distance definition generic class
Public  class Mygenericclass<mvp> {
  //There is no MVP type, here represents an unknown type of data to pass what is what type in the future
  Private MVP MVP;
  
Public  void SETMVP (MVP MVP) {
  THIS.MVP = MVP;
  }
  
Public  MVP GETMVP () {
  return MVP;
  }
  }
  
  3.  the format of a method definition that contains a generic type
  format:
  modifier < variable representing generic type > Return value type method name (parameter) {}
  
Public  class MyGenericMethod {
Public  <MVP> void Show (MVP MVP) {
  System.out.println (Mvp.getclass ());
  }
  
Public  <MVP> MVP Show2 (MVP MVP) {
  return MVP;
  }
  }
  
  4. Interface with generic type
  define the format:
  modifier Interface Interface name < variable >{} that represents a generic type
  
public interface  mygenericinterface<e>{
public abstract void Add (e e);
  
  
}
      
          For example:
                  public class MyImp1 implements mygenericinterface<string> {
                    @Override
                    public void Add (String e) {
                    Omitted...
                    }
?
                    @Override
                    Public String Gete () {
                    return null;
                    }
                }
  
2. The type of the generic is always indeterminate until the type of the generic is  determined when the object is created
Public  class Myimp2<e> implements mygenericinterface<e> {
  @Override
Public  void Add (E e) {
Omitted...
  }
  
  @Override
Public  E Gete () {
  return null;
  }
  }
  /**
  Determining generics
  */
  
Public  class Genericinterface {
Public  static void Main (string[] args) {
  myimp2<string>  my = new myimp2<string> ();  
  my.add ("AA");
  }
  }
The wildcard character of a generic type
  when a generic class or interface is used, the type of the generic is not deterministic and can be represented by a wildcard character <?>. But once a generic wildcard is used,
Only the generic method of the object class can be used, and its own methods cannot be used in the collection.
  
  1. Wildcard characters Basic use
  Generic wildcard: Do not know when to use what type to accept, at this time can be used? Indicates an unknown wildcard character
  
  Example:
Public  static void Main (string[] args) {
  collection<intger> list1 = new arraylist<integer> ();
  getelement (List1);
  collection<string> list2 = new arraylist<string> ();
  getelement (LIST2);
  }
Public  static void GetElement (Collection<?> coll) {}
  //? Delegates can receive any type
 notes:tips: generic no inheritance relationship collection<object> list = NE W arraylist<string> (); this is wrong; 2. Wildcard advanced when you set generics before using----restricted generics, you can actually set them arbitrarily, as long as the class is set. In Java generics, however, you can develop a generic upper bound for the on-line and lower-bound generics: Format: type name <?                           Extends class > object name meaning: You can only receive the lower bound of the type and its subclass generics: Format: Name of type <? Super Class > object name meaning: You can only receive the type and its parent class type 
                  Example: public static void Main (string[] args) {Collection<i                          Ntger> List1 = new arraylist<integer> ();                          collection<string> list2 = new arraylist<string> ();                          collection<number> list3 = new arraylist<number> ();                            collection<object> list4 = new arraylist<object> ();                          GetElement (List1);                          GetElement (LIST2);//Error GetElement (LIST3);                           GetElement (LIST4);//Error GetElement2 (LIST1);//Error GetElement2 (LIST2);//Error                          GetElement2 (LIST3);                        GetElement2 (LIST4); }//The upper limit of generics: generic at this time, must be a number type or a subclass of number type public static V OID GetElement1 (collection<? extendsNumber> coll) {}//The lower limit of generics: generic at this time, must be the number type or the parent of number type public static void G EtElement2 (collection<? Super Number> Coll) {}

Common Data Structures:

Stacks, queues, arrays, linked lists, and red-black trees.

Java overrides the use of ToString and generics

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.