参考。类的功能不可访问为什么? [英] Ref. Class's Function Is Not accessible Why?

查看:76
本文介绍了参考。类的功能不可访问为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考。类的功能不可访问为什么?



听到我的代码



  public   class  FieldShadowing_1 
{
public < span class =code-sdkkeyword> String name = FieldShadowing_1;
public String getName()
{
return Hello;
}


}





 < span class =code-keyword> public   class  FieldShadowing_2扩展FieldShadowing_1 
{



public void display()
{
System。 out .println( 检查);
}
}



  public   static   void  main( String  [] args)
{

FieldShadowing_1 b = new FieldShadowing_2();


系统。 out .println(b.display()); // display()无法访问听到O
}

解决方案

请参阅我对解决方案1和解决方案2的评论。他们没有完全解释发生了什么。



要理解它,你需要得到编译时和运行时类型的概念。



FieldShadowing_1 b = new FieldShadowing_2 (); 编译时类型 b FieldShadowing_1 ,但运行时类型是 FieldShadowing_2 。您可以这样想: b 提供对 FieldShadowing_2实例的引用 FieldShadowing_1 的角度来看。由于这些是对实例类型的引用,您可能需要引用不同的物理上指向内存中相同位置的类型。因此,如果你将 b 改为 FieldShadowing_2 ,那么会给你的访问显示。这不是一个好主意,因为在更一般的上下文中,您的编译时信息并不关心这是派生类型的信息。您宁愿需要使用动态强制转换,它实际上会询问实例是哪种类型的实例。



如果这些是接口参考,由于多重继承的弱形式(仅限于接口的多个),在问题的根源中情况会更复杂。可能存在不同接口类型的引用指向内存中不同位置的情况 - 某些类的相同运行时实例的不同位置



这一切都与阴影无关。



-SA


虽然下面是 FieldShadowing_2 ,但 FieldShadowing_2 假装是 FieldShadowing_1 ,它不能 display()



这是继承抽象的示例:

FieldShadowing_2 继承 FieldShadowing_1

变量 b 抽象 FieldShadowing_2 因为它存储为 FieldShadowing_1



这是一个重写版本,演示了更多有无错误的情况:



(我更改了类名,因为 FieldShadowing_1 不是一个非常具有描述性的类名,并且因为这段代码实际上并没有演示 shadowing ,这是其他的完全!)



  public   class  A 
{
public String getName() {返回 A; }
}
public class B1 extends A
{
public void display()
{
System.out.println( 检查);
}
}
public class B2 extends A
{
public void doNothing()
{

}
}
public static void main( String [] args)
{
B b1 = new B1();
B b2 = new B2();
A a1 = b1;
A a2 = b2;

System.out.println(b1.getName()); // 确定:B1具有getName()
System.out.println(b2.getName) ()); // 确定:B2有getName()

System.out.println (a1.getName()); // 确定:A有getName()
System.out.println(a2.getName) ()); // 确定:A有getName()

b1.display() ; // OK:B1有display()
b2.display(); // 错误:B2没有显示()

a1.display (); // 错误:A没有display()
a2.display(); // 错误:A没有显示()

b1.doNothing (); // 错误:B1没有doNothing()
b2.doNothing(); // 确定:B2有doNothing()

a1.doNothing() ; // 错误:A没有doNothing()
a2.doNothing(); // 错误:A没有doNothing()


}


Quote:

公共类FieldShadowing_2扩展了FieldShadowing_1



这意味着 FieldShadowing_2 具有 FieldShadowing_1 的所有功能,但不是其他方式周围!

通过向下转发 FieldShadowing_2 您还会删除它添加的扩展程序...


Ref. Class's Function Is Not accessible Why?

hear is my code

public class FieldShadowing_1
{
    public  String name = "FieldShadowing_1";
    public   String getName()
    {
        return "Hello";
    }


}



public class FieldShadowing_2 extends FieldShadowing_1
{



    public  void display()
    {
        System.out.println("Check");
    }
 }


public static void main(String[] args)
   {

FieldShadowing_1 b = new FieldShadowing_2();

       
        System.out.println(b.display()); //display() is not accesible hear O"
}

解决方案

Please see my comment to Solution 1 and Solution 2. They do not completely explain what's going on.

To understand it, you need to get the concept of compile-time and runtime types.

In FieldShadowing_1 b = new FieldShadowing_2(); compile-time type of b is FieldShadowing_1, but runtime type is FieldShadowing_2. You can think of it this way: b provides a reference to the instance of FieldShadowing_2 from the point of view of FieldShadowing_1. As those are references to instance type, you could have to references of different types which are physically pointing to the same place in memory. So, if you down-case b to FieldShadowing_2, it will give your the access to display. This is not a good idea, because in more general context, your compile-time information does not care information that this is the derived type. You would rather need to use dynamic cast which actually asks an instance which type is it, actually.

If those were the interface reference, the situation would be more complex, in the root of the problem, due to the weak form of multiple inheritance (multiple for interfaces only). The it is possible to have the situation where references of different interface types point to different place in memory — different places of the same runtime instance of some class.

And it all has nothing to do with shadowing.

—SA


Although it is really a FieldShadowing_2 underneath, the FieldShadowing_2 is pretending to be a FieldShadowing_1, which cannot display().

This is an example of inheritance and abstraction:
FieldShadowing_2 inherits FieldShadowing_1.
The variable b is an abstraction of FieldShadowing_2 because it is stored as a FieldShadowing_1.

Here is a rewritten version demonstrating more situations that do and don't have errors:

(I have changed the class names because FieldShadowing_1 is not a very descriptive class name, and because this code does not actually demonstrate shadowing, which is something else entirely!)

public class A
{
    public String getName() { return "A"; }
}
public class B1 extends A
{
    public void display()
    {
        System.out.println("Check");
    }
}
public class B2 extends A
{
    public void doNothing()
    {

    }
}
public static void main(String[] args)
{
    B b1 = new B1();
    B b2 = new B2();
    A a1 = b1;
    A a2 = b2;

    System.out.println(b1.getName()); // OK: B1 has getName()
    System.out.println(b2.getName()); // OK: B2 has getName()    

    System.out.println(a1.getName()); // OK: A has getName()
    System.out.println(a2.getName()); // OK: A has getName()

    b1.display(); // OK:    B1 has display()    
    b2.display(); // ERROR: B2 does not have display()

    a1.display(); // ERROR: A does not have display()
    a2.display(); // ERROR: A does not have display()

    b1.doNothing(); // ERROR: B1 does not have doNothing()    
    b2.doNothing(); // OK:    B2 has doNothing()

    a1.doNothing(); // ERROR: A does not have doNothing()
    a2.doNothing(); // ERROR: A does not have doNothing()

    
}


Quote:

public class FieldShadowing_2 extends FieldShadowing_1


It means that FieldShadowing_2 has all the capabilities of FieldShadowing_1 but not the other way around!
By downcasting FieldShadowing_2 you also remove the extensions it adds...


这篇关于参考。类的功能不可访问为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆