gpt4 book ai didi

java - android访问扩展类中的对象

转载 作者:行者123 更新时间:2023-12-02 04:10:49 24 4
gpt4 key购买 nike

我在使用 this 时遇到一些问题关键词。如果我有几个类实现另一个类,我如何在不调用类本身的情况下使用它们的值?我解释一下。

//this is my first class
public class Foo extends FooHelper{
public int fooInt;
public String fooString;
//getter/setter below
}

//this is my second class
public class Foo2 extends FooHelper{
public double fooDouble;
public float fooFloat;
}

//this is my main method, i'm using it for calling the value.
//I omit all the thrash code before.
//This is how i want to call the method:
//imagine before there are onCreate, activity,...
Foo foo = new Foo().GetFooInt();

//this is the class extended from the firsts
public class FooHelper{
public void GetFooInt(){
//here is my problem, i need to call the Foo class and the fooInt value.
//I want also to be able to edit the Foo object, for example:
if(((Foo)this).getFooInt() == 0){
(Foo) this.setFooInt(5);
}
}
}

这就是我想要实现的目标,访问一个类,该类用唯一的this扩展另一个类。扩展类中的关键字。我该怎么做?

编辑:

我认为我解释得很糟糕。我的问题是我想访问 FooHelper 内的 Foo 对象,而不是 Foo 对象内的 FooHelper 方法。

示例:

使用此代码后:

Foo foo = new Foo();
foo.HelperClassMethod();

我需要(在 HelperClass 中)访问调用它的 Foo 对象。

public HelperClass<Foo> {
public void HelperClassMethod(){
//HERE i need to use the "foo" object which invoked this method
}
}

我添加了<Foo> ,可能是我错过了,这是正确的吗?我如何在辅助类的方法中使用这个 foo 对象?谢谢大家

EDIT2:我的问题完全失败了,我认为让我们忽略上面的代码,只检查下面的内容:

我必须访问扩展类方法内的对象。

我有这门课:

public class Foo extends FooToExtend{
public int fooInt;
}

扩展的类是这样的:

public class FooToExtend{
public void MethodOne(){
//HERE i need to access the calling object
}
}

现在,在我的主要 Activity 中,我想这样做:

Foo foo = new Foo();
foo.MethodOne();

我的疑问是如何访问foo我在我的 MethodOne 的 main 中创建的对象.

我必须更改我的 FooToExtend

public class<Foo> FooToExtend{
...
}

但我仍然不知道如何访问其中的 foo 对象。

最佳答案

我在这里看到两个问题,理解 this 关键字和扩展

关键字有问题

假设您有一个类,并且正在执行一些代码:关键字 this 指的是类本身,如果您的对象 this 相当于 。检查herehere更长的解释、示例和教程。

扩展的问题

此外,您还必须从顶部(接口(interface)或抽象类)扩展到底部(扩展)类并在底部实现:

//this is the PARENT (FIRST) class extended from the CHILDREN (SECOND)
public abstract class FooHelper{
public abstract void GetFooInt();
}

//this is the CHILD (SECOND!!!) class
public class Foo extends FooHelper{
public int fooInt;
public String fooString;

@Override
public void GetFooInt() {
// are you sure you getFooInt method can return a null???
if(this.getFooInt() == null){
this.setFooInt(5);
}

//getter/setter below
}
<小时/>

编辑1

Oh ok, this was useful. one more question, a way is to use abstract, as you said, but is there a way to do the same without implementing it all times? just for info, my objective is to use Foo.FooHelperMethod() and be able in "FooHelperMethod()" to access Foo class. I hope i explained it, i don't know how to do it.. if it's impossible i will use abstract as you suggested :)

当然,这是继承,只需不声明抽象父类,并实现其中的方法和属性,所有子类都会通过扩展父类来拥有此方法和属性。

让我们看看这个例子:

//this is the PARENT (FIRST) class extended from the CHILDREN (SECOND)
class FooHelper {
int theIntCommonValue;

public int getTheIntCommonValue() {
return theIntCommonValue;
}

public void setTheIntCommonValue(int theIntCommonValue) {
this.theIntCommonValue = theIntCommonValue;
}
}

// CHILDREN CLASS, look how calling this.getTheIntCommonValue() (the parent method)
// doesn't throw any error because is taking parent method implementation
class Foo extends FooHelper {
public void getFooInt() {
if (this.getTheIntCommonValue() == 0)
this.setTheIntCommonValue(5);
}
}
class Foo2 extends FooHelper {
public void getFooInt() {
if (this.getTheIntCommonValue() == 3)
this.setTheIntCommonValue(8);
}
}
<小时/>

编辑2:

My doubt is how i can access foo object i created in main inside my MethodOne.

答案:

将对象作为参数传递。但是,您需要静态类,而不是扩展类,让我们看看

示例:

Foo.java

public class Foo {
public int fooInt;
}

FooHelper.java

public static class FooHelper {
public static void methodOne(Foo foo){
//HERE i need to access the calling object

// for example, this?
if (foo.fooInt == 2)
}
}

现在,你如何执行它?

Main.java

public static void main(String[] args) throws Exception {
Foo foo = new Foo();
FooHelper.methodOne(foo);
}

注释

  • 按照约定,java 中的方法以 LOWECASE 开头,类名以 UPPERCASE 开头。
  • 您必须将这两个类放在单独的文件中才能允许静态公共(public)类

关于java - android访问扩展类中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33822266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com