加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 通过Composition(接口和类)进行类型转换

发布时间:2020-12-15 02:12:57 所属栏目:Java 来源:网络整理
导读:当我尝试使用包含泛型的类(其中两个类都实现接口)的方法时,我有一个主要的断开连接.我有一个SetInterface接口,其中包含我的Set类的方法.我的ProfileInterface接口和Profile类也是如此.在我的Profile类中,我有以下类型转换引用Set类: private SetProfileInte
当我尝试使用包含泛型的类(其中两个类都实现接口)的方法时,我有一个主要的断开连接.我有一个SetInterface接口,其中包含我的Set类的方法.我的ProfileInterface接口和Profile类也是如此.在我的Profile类中,我有以下类型转换引用Set类:

private Set<ProfileInterface> followBag = new Set<ProfileInterface>();

基本上,我们正在学习Data Structures类中的Array Bags,Linked Bags等.我的问题是我希望在我的Profile类中通过Composition使用Set Data Structure类的方法.我遇到的一个问题是我需要在我的Profile类中创建一个方法,该方法交叉引用给定数组中的项目以查看该Profile对象是否“跟随”另一个Profile,如果没有,则建议遵循该对象(任务是通过数据结构完成类似于Twitter或Facebook的任务).这是我到目前为止创建的方法以及我无法通过的错误(从顶部开始的第三行):

public ProfileInterface recommend(){
    ProfileInterface recommended;
    ProfileInterface thisProfile = new Profile(); //  <--Here is the question
    for(int index = 0; index < followBag.getCurrentSize(); index++){
        ProfileInterface follows = followBag[index];
        for(int followedFollowers = 0; followedFollowers < follows.getCurrentSize(); followedFollowers++) { // <--Question here also
            //if Profile's match,do nothing
            //if Profile's do not match,set recommended == the Profile
        }
    }
    return recommended;
}

请原谅我的伪代码,因为我还在努力.但是我不能继续使用这种方法,直到我完全理解我需要做什么来使第三次演员变得正确并理解我的其他关注点(第二个音符)是否可行.

我是第二类Java,我似乎无法通过这些问题.理想情况下,我希望一个成员或多个成员能够将其愚蠢地降低到5岁,这样我才能完全掌握它.我知道Profile类与我的Set类有一个“有一个”关系(Composition).我也知道,因为我按照我的方式输入了我的followBag,它将同时具有ProfileInterface和Set方法.但是,如果可能的话,我想用一些例子来解释如何在一个方法中正确地输入一个数组的项目,或者在这个例子中的我的Set中,并将它转换为给定的对象?我希望将此对象(在本例中为thisProfile)填充到给定索引以进行比较.但我需要澄清两件事:

A)对类型转换的对象会发生什么变化如下:
ProfileInterface thisProfile = new Profile();

既然我指的是接口和实现它的类?这意味着,如果我的Profile()类中有更多方法,那么thisProfile只能访问哪些方法;界面或类?

B)我应该简单地调用ProfileInterface thisProfile,然后将thisProfile的引用分配给我的内部for循环中的特定索引吗?

C)我的第二个注释:我在IntelliJ中遇到错误,声明它“无法解析方法getCurrentSize()”.我不明白,因为我创建了一个上面的一行.我认为ProfileInterface follow = followBag [index]会将给定的数组索引设置为等于稍后在我的代码中使用的索引.这个错误源于我上面的困惑.所以,一旦我提供了一些关于我做错的清晰度,我就是肯定的,我相信我会适当地解决它.

谢谢大家,我一如既往地期待您的回复!

解决方法

A) What happens to an object that is type-casted as follows: ProfileInterface thisProfile = new Profile();

首先,这是一个作业,而不是一个类型转换.

Since I’m referring to both the interface and the class that implements it? Meaning,if I have more methods within my Profile() class,which methods will this thisProfile only have access to; interface or class?

您只能访问ProfileInterface中的方法.如果这是一个问题,我建议您只需将ProfileInterface更改为Profile.私有变量的类型应该没有问题重构.

C) My second note: I’m getting an error in IntelliJ stating that it,“cannot resolve method getCurrentSize()”. I do not understand this since I created follows a line above that.

这意味着followBag是一个Set,并没有实现一个名为getCurrentSize()的方法.

I thought that ProfileInterface follows = followBag[index] would set the given Profile index of an array equal to that for use later in my code.

这与问题C的第一部分无关.followBag不是数组.我不确定你在这里尝试做什么,但如果你是C#程序员,Java不会使用相同的语法. []不能用于访问Collection的成员.您必须使用get()或相关方法.

我真的不确定你在问题B中想说的是什么.我会摆脱语法错误,然后再问一下,这里有些东西,我无法确切地知道你要去哪里.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读