简体   繁体   English

多态对象字符串

[英]Polymorphism Object String

I didn't get to understand the polymorphism.我没有理解多态性。

taking this example :以这个例子为例:

Object o = new String ("foo");

I can't do我做不到

o.substring (1,2)

can anyone explain this problem to me ?谁能向我解释这个问题?

You are confusing dynamic and static typing with polymorphism .您将动态和静态类型多态混淆了。

Java is a statically typed language so the compiler recognizes that Object doesn't have a substring() method and throws an error. Java 是一种静态类型语言,因此编译器会识别出Object没有substring()方法并抛出错误。 Polymorphism happens after the compilation, when the code is actually executed.多态发生在编译之后,当代码实际执行时。

First thing It's not polymorphism.首先,这不是多态性。

In Simple way, In your case o object will only call methods which are defined in Object class.以简单的方式,在您的情况下o object 只会调用 Object 类中定义的方法。 If String class has overridden any method of Object class then object o will execute that method of String class otherwise Object o will call Its own method.如果String类覆盖了Object类的任何方法,则 object o将执行String类的该方法,否则 Object o将调用其自己的方法。

For example :例如 :

substring() method is only defined in String class So In this case your code will throw exception, however if you call equals() , hashcode() , toString() methods (as these are defined in Object class ) then definition inside String class will get executed because String Class has overridden these methods. substring()方法仅在String类中定义所以在这种情况下,您的代码将抛出异常,但是如果您调用equals()hashcode()toString()方法(因为这些在Object类中定义)然后在String类中定义将被执行,因为String类已经覆盖了这些方法。 And if any of the methods from object Class has not been overridden in its child class then definition inside Object class will get executed.如果来自对象 Class 的任何方法没有在其子类中被覆盖,则将执行 Object 类中的定义。

substring is from the String class, not the Object class. substring来自String类,而不是Object类。 So the below code will work:所以下面的代码将起作用:

String o = new String ("foo");
o.substring(1,2);

This code will not work:此代码将不起作用:

Object o = new String ("foo");
o.substring(1,2);

Since String inherits from Object , String can call Object 's methods, such as toString .由于String继承自Object ,因此String可以调用Object的方法,例如toString However, Object does not inherit from String , so an Object cannot call String 's methods, such as substring .但是, Object不继承自String ,因此Object不能调用String的方法,例如substring

This is a consequence of the Liskov Substitution Principle , which states (summarized):这是Liskov 替换原则的结果,它指出(总结):

If S and T are objects, and T is a subtype of S, then T may be used where S is expected.如果 S 和 T 是对象,并且 T 是 S 的子类型,则可以在需要 S 的地方使用 T。

String is-a subtype of Object , so if your assignment operation expects Object , then it will happily accept Object or any of its subtypes. StringObject子类型,因此如果您的赋值操作需要Object ,那么它会很乐意接受Object或其任何子类型。

(Note: Object is not a String . All String s are Object s, but not all Object s are String s.) (注意: Object不是String 。所有String都是Object ,但并非所有Object都是String 。)

This doesn't mean you get access to any of the subtype's methods.这并不意味着您可以访问任何子类型的方法。 Given the inheritance hierarchy, an Object has no clue about any of its children's specific methods, nor can it - there is no way to inform an ancestor class of its descendant's capabilities.鉴于继承层次结构, Object不知道其任何子项的特定方法,也不能 - 没有办法通知祖先类其后代的能力。 Because Object has no substring method associated with it, your code correctly results in a compilation failure.因为Object没有与之关联的substring方法,所以您的代码正确地导致编译失败。

(And it should, given that Object is the ancestor of all classes. There's no guarantee that any given Object is a String .) (而且它应该,因为Object所有类的祖先。不能保证任何给定的ObjectString 。)

The standing advice is to not use an overly inspecific object type (as you go up the hierarchy chain, the capabilities become less specific - you lose functionality as you go up to Object ) to accomplish something specific to a more specific type.长期建议是不要使用过于不具体的对象类型(随着您在层次结构链上的上升,功能变得不那么具体——当您上升到Object ,您会失去功能)来完成特定于更具体类型的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM