指针接口和类引用指向对象的区别?

huangapple 未分类评论47阅读模式
英文:

Difference between pointing Interface and class ref to object?

问题

请阅读完整问题,这与此类问题不同
https://stackoverflow.com/questions/22962856/difference-between-interface-and-class-objects

示例:1

实现接口的类

    public interface a{
      void foo();
    }
    public class b implements a{
      @Override
      void foo(){}
      void bar(){}
    }


工作行为

    a asd=new b(); 只能调用 foo()
    b asf=new b(); 可以调用 foo() 和 bar()

我希望到这里清楚。


示例2:

现在有一个类 `CheckingPhase` 和 `IdentifyCheckingPhase`

    class IdentifyCheckingPhase {
    
    	private static a getPhase() {
    
    		return a;
    	}
    
    	private static void matchPhase(){
     
     
     (CheckingPhase)IdentifyCheckingPhase.getPhase().bar();
     
     }
    
    }
    
    class CheckingPhase implements a {
    
    	@Override
    	void foo() {
    	}
    
    	void bar(){   
     }
    
    }


在 `示例1` 中,接口实例只能调用类中实现的自己的方法,而类实例可以调用所有方法(类本身和接口的方法)。如果是这种情况,我确定编译器在内部维护了一些不同之处,这就是为什么它能区分开来。

疑问1:是否正确说接口和类引用始终指向同一类的不同类型实例?我猜是的,这就是它们能够调用自己方法的原因。(接口只能调用自己的方法,但类引用可以调用所有方法)

如果不是这样的话,在第二个示例中,从 `getPhase()` 方法返回的 `a`,不应该被允许替换为 `CheckingPhase`,并在 `matchPhase()` 中调用它的类实例方法。因为 `a` 只能调用 `foo`,而 `CheckingPhase` 可以调用 `foo` 和 `bar`。

疑问2:我在想,从 `getPhase()` 方法到 `matchPhase()` 是否使用 `CheckingPhase` 而不是 `a` 是语法上正确的?

我希望我的问题表达清楚了。如果有不清楚的地方,请告诉我。(这更多地涉及 Java 如何在上述用例中使用语法。)
英文:

Please read full question, Its different from this type of question
https://stackoverflow.com/questions/22962856/difference-between-interface-and-class-objects

Example: 1

Class implementing Interface

public interface a{
  void foo();
}
public class b implements a{
  @Override
  void foo(){}
  void bar(){}
}

Working behaviour

a asd=new b(); can call only foo()
b asf=new b(); can call both foo() and bar()

I hope upto here its clear.

Example 2:

Now there is one class CheckingPhase and IdentifyCheckingPhase

class IdentifyCheckingPhase {

	private static a getPhase() {

		return a;
	}

	private static void matchPhase(){
 
 
 (CheckingPhase)IdentifyCheckingPhase.getPhase().bar();
 
 }

}

class CheckingPhase implements a {

	@Override
	void foo() {
	}

	void bar(){   
 }

}

In Example 1. Interface instance only able to call its own implemented method in class and class instance able to all methods (class itself and Interface too). If that's a case, am sure something different being maintanied in compiler side that's why its able to differentiate.

Doubt First, Its correct to say that Interface and Class ref always points to different types instances of same class ? I guess yes, that's they are able to call their own methods. (Interface only its own methods but class ref can call all)

If not, Then In second example, a returned from getPhase(), should not be allowed to replace with CheckingPhase in matchPhase() and call its class instance method. Because a allowed to call only foo and CheckingPhase can call foo and bar both.

Doubt 2, I'm wondering, Is it syntactically correct using CheckingPhase instead of a while coming from method getPhase() to matchPhase() ?

I hope its clear what am trying to ask. Please let me know if may qyestion is not clear. (Its more about how java is using Syntax for above use case)

huangapple
  • 本文由 发表于 2020年4月6日 16:53:32
  • 转载请务必保留本文链接:https://java.coder-hub.com/61056117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定