简体   繁体   English

根据对象的投射类型访问阴影方法

[英]Access shadowed methods based on the casting type of the object

Method calls are always determined based on the runtime type of the object, however how can I call shadowed methods of base classes from an inherit instance? 方法调用总是根据对象的运行时类型确定的,但是如何从继承实例中调用基类的影子方法呢?

class Base {
    String str = "base";
    String str() { return str; }
}

class Impl extends Base {
    String str = "impl";
    String str() { return str; }
}

class Test {
    public static void main(String[] args) {
        System.out.println(((Base) new Impl()).str);   // print 'base'
        System.out.println(((Base) new Impl()).str()); // print 'impl'
    }
}

For example above, how can I call the str() method of Base class from an Impl instance, preferably without using reflection? 例如,上面的示例如何从Impl实例调用Base类的str()方法,最好不使用反射?

Using the Keyword super 使用关键字超级

Accessing Superclass Members 访问超一流成员

If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super . 如果您的方法覆盖了其超类的方法之一,则可以通过使用关键字super来调用覆盖的方法。 You can also use super to refer to a hidden field (although hiding fields is discouraged). 您也可以使用super引用隐藏字段(尽管不建议使用隐藏字段)。 an example below. 下面的例子。

public class Superclass {

    public void printMethod() {
        System.out.println("Printed in Superclass.");
    }
}

calling printMethod since child class 从子类开始调用printMethod

Here is a subclass, called Subclass, that overrides printMethod(): 这是一个名为Subclass的子类,它重写printMethod():

public class Subclass extends Superclass {

    // overrides printMethod in Superclass
    public void printMethod() {
        super.printMethod();
        System.out.println("Printed in Subclass");
    }
    public static void main(String[] args) {
        Subclass s = new Subclass();
        s.printMethod();    
    }
}

if you want to read more https://docs.oracle.com/javase/tutorial/java/IandI/super.html 如果您想阅读更多https://docs.oracle.com/javase/tutorial/java/IandI/super.html

铸造类型 Object 入列表<object><div id="text_translate"><p>远程调用 API 控制器(通过 RestTemplate)(即从一侧返回已知 object 的列表 ([]) 和未知类型的 ZA8CFDE6331BD59EB2AC96F8911BD59EB2AC96F8911C4B666Z&gt; 从远程端返回ResponseEntity&lt;Object&gt;通过restTemplate.getForEntity(url, Object.class)</p><p> 为了迭代/循环返回的 object 是否可以将返回的 object 转换为 List (假设它是一个 List )? 并使用 Java 的反射从每个 object 中获取想要的字段值? 否则如何访问 Object 类型来获取字段值?</p><p> 编辑:具有这种结构的 object(在 java 中是 Object 类型)</p><pre> {id=5, name=Jasmin, description=Room Jasmin, idType=0, addedAt=2020-06-16T17:20:00.617+0000, modifiedAt=null, deleted=true, images=[string], idBuilding=2, idFloor=4, idZone=3}</pre><p> 如何访问 id 字段值? 我试过Class&lt;?&gt; RoomClass = roomTuple.getClass(); 然后用getField()</p><p> 谢谢,</p></div></object> - Casting type Object into List<Object>

暂无
暂无

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

相关问题 在Java中转换对象的不同方法 - Different methods for casting an object in Java 将对象转换为类类型 - Casting Object to class type 从对象类型转换 - Type casting from an Object 类型对象的运行时转换 - Runtime Casting of the Type Object Java类型转换对象? - Java Casting Object to Type? Object 到泛型铸造 - Object to generic type casting 将类型对象转换为泛型 - casting a type Object to generic 类型转换`Object`类型变量 - Type casting the `Object` type variables 铸造类型 Object 入列表<object><div id="text_translate"><p>远程调用 API 控制器(通过 RestTemplate)(即从一侧返回已知 object 的列表 ([]) 和未知类型的 ZA8CFDE6331BD59EB2AC96F8911BD59EB2AC96F8911C4B666Z&gt; 从远程端返回ResponseEntity&lt;Object&gt;通过restTemplate.getForEntity(url, Object.class)</p><p> 为了迭代/循环返回的 object 是否可以将返回的 object 转换为 List (假设它是一个 List )? 并使用 Java 的反射从每个 object 中获取想要的字段值? 否则如何访问 Object 类型来获取字段值?</p><p> 编辑:具有这种结构的 object(在 java 中是 Object 类型)</p><pre> {id=5, name=Jasmin, description=Room Jasmin, idType=0, addedAt=2020-06-16T17:20:00.617+0000, modifiedAt=null, deleted=true, images=[string], idBuilding=2, idFloor=4, idZone=3}</pre><p> 如何访问 id 字段值? 我试过Class&lt;?&gt; RoomClass = roomTuple.getClass(); 然后用getField()</p><p> 谢谢,</p></div></object> - Casting type Object into List<Object> 根据传入的字符串转换为 object - casting in an object based on an incoming string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM