zoukankan      html  css  js  c++  java
  • String类的valueOf以及动态绑定(多态)

    1、String类的valueOf

    Class String

    public static String valueOf(Object obj)

    Returns the string representation of the Object argument.
    Parameters:
    obj - an Object.
    Returns:
    if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
    See Also:
    Object.toString()

    2、动态绑定(多态)的3必要个条件:

    1)、继承(Date继承Object),2)、重写(toString),3)、父类的引用指向子类的对象 (见ValueOfObject类

    3、求整形数的大小

    //ValueOfObject.java
    
    
     1 class ValueOfObject {
     2     public static void main(String[] args) {
     3         Date d = new Date(2013, 8,11);
     4         
     5         //原型:public static String valueOf(Object obj)
     6         // 3、父类的引用指向子类的对象
     7         String str = String.valueOf(d); 
     8         System.out.println("value of date="+str);
     9         
    10         System.out.println("int 198 value of="+String.valueOf(198));
    11         System.out.println("double 198.98 value of="+String.valueOf(198.98));
    12         
    13         // 求整形数的长度(转换为String)
    14         int i = 123456789;
    15         String sNumber = String.valueOf(i);
    16         System.out.println("the length of i is:" + sNumber.length());
    17     }
    18 }
    19 
    20 class Date {
    21     int year;
    22     int month;
    23     int date;
    24     
    25     Date(int year, int month, int date) {
    26         this.year = year;
    27         this.month = month;
    28         this.date = date;
    29     }
    30     
    31     // override from the Object class
    32     // 动态绑定(多态)3个必要条件:1、继承(Date继承Object),2、重写(toString),3、父类的引用指向子类的对象 (见ValueOfObject类)
    33     public String toString() {
    34         return "year:month:date--"+year+"-"+month+"-"+date;
    35     }
    36     
    37 }

    补充:

    Class Object

    public String toString()
    Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

    The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

     getClass().getName() + '@' + Integer.toHexString(hashCode())
     
    Returns:
    a string representation of the object.

    结果:

    E:学习Java编程练习TestString>java ValueOfObject
    value of date=year:month:date--2013-8-11
    int 198 value of=198
    double 198.98 value of=198.98

    the length of i is:9

  • 相关阅读:
    8、【转载】python enhanced generator - coroutine
    7、【转载】python yield generator 详解
    7、利用SAX编写程序解析Yahoo的XML格式的天气预报,获取天气预报
    6、urllib.request.Request类
    5、urllib.request.urlopen()
    重载内核的一份代码的学习
    分析
    CVE-2014-0282
    IOS逆向【5】GDB调试helloworld
    IOS逆向【4】.ipa安装
  • 原文地址:https://www.cnblogs.com/wxdlut/p/3251095.html
Copyright © 2011-2022 走看看