使用嵌套的接口进行通信在Java / Android的两个类之间 [英] using nested Interfaces for communication between two classes in java / android

查看:577
本文介绍了使用嵌套的接口进行通信在Java / Android的两个类之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我,如果我是对还是错?我真的弄不清解决我的问题。

can anyone tell me if I am right or wrong? I am really getting confused in solving my problem.

我拥有的是(或者我想要做什么,或者我的想法是:)

What I have is (or what I want to do Or am thinking is:)

我有:

 Class B{

     ........
     ........
    interface I{
     ......
     ........
     }
   .......
   .......  
    } 

  Class A implements B.I{
      ........
      .......
      B b= new B();
       }

这是两个类B类和A类之间的通信的正确方法?
我应该如何使这项工作。
我想从A类的一些数据传递给B类进行进一步的操作。

Is it the right way of communication between two classes class B and Class A? how should i make this work. I want some data from class A passed to class B for further operations.

我应该怎么做,我将在得到从b调用它,当我需要的数据实现的方法?
在一个有接口相同的情况下一个简单的例子真的会帮助我。 Doea人对如何工作界面一个很好的解释?或应如何使用?

how should i make the methods that i will implement in A get called from B when i require the data? A simple example on an Interface having same scenario will really help me. Doea anyone have a good explanation on how interface work? or how should they be used?

我也将进一步想问落后于Android的接口工作的逻辑..?
背后是什么,我们在的OnClick听众回调方法的逻辑是什么?
因为这也进行使用接口?
为前:我们执行他们在我们班

I would also further like to ask logic behind working of interfaces in android..? what is the logic behind callback methods that we have in OnClick Listeners? because this also is carried out using interfaces? for ex: we implement them in our class

  class A implements View.OnClickListener

和处理的onclick事件为我们班的逻辑是什么?
所以,当他们是所谓的。(我知道当我们点击该特定视图,他们被称为)
我想它们是如何调用机制或实施

and provide the logic in our class for handling onClick events? So when are they called .(I know they are called when we click on that particular view) i want the mechanism or implementation of how they are called

也许我应该这样做使用抽象类?
我真的卡住了!
三江源

or maybe i should do this using abstract class ? i am really stuck! Thankyou

推荐答案

接口是为了定义一个类型在Java中的行为。如果一个类实现一个接口,这是令人欣慰的编译器,它可以做该接口的所有方法。

Interfaces are meant to define a type of behavior in Java. If a class implements an interface, it is reassuring the compiler that it can do all the methods in that interface.

例如,你可以有一个用对象所需的方法,可以打印的打印接口(如getStringRe presentation方法)。任何类实现打印必须实现所有的方法,所以你必须能够打印类的对象。

For example, you could have a Printable interface with methods required of objects that can be printed (e.g. a getStringRepresentation method). Any class that implements Printable must implement all its methods, and so you must be able to print objects of that class.

您可以阅读更多关于接口这里

You can read more about interfaces here.

如果你只是想从A类数据传递给B类,你不一定需要一个界面,你没有的多个类,可以做同样的事情的,这是当您可能需要使用一个接口来定义它们的共同行为。

If you just want to pass data from class A to class B, you don't necessarily need an interface as you don't have multiple class which can do the same thing, which is when you may need to define their common behavior by using an interface.

你为什么不能单从A类的数据传递到使用B的方法之一的参数B类的对象呢?

Why couldn't you just pass the data from class A to and object of class B using a parameter of one of B's methods?

例如

// somewhere in the methods of A
B b = new B();
b.giveData(theData);

这篇关于使用嵌套的接口进行通信在Java / Android的两个类之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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