【问题标题】:android accessing object inside extended classandroid访问扩展类中的对象
【发布时间】:2015-11-20 08:40:56
【问题描述】:

我在使用 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
    }
}

我添加了&lt;Foo&gt;,可能我错过了它,这是正确的吗?以及如何在辅助类的方法中使用这个 foo 对象?谢谢大家

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

我必须在扩展类的方法中访问一个对象。

我有这门课:

public class Foo extends FooToExtend{
    public int fooInt;
}

扩展的类是这样的:

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

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

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

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

我必须在

中更改我的 FooToExtend
public class<Foo> FooToExtend{
    ...
}

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

【问题讨论】:

  • Helper 可以用作静力学。也许这对你来说会做得更好?
  • ((Foo)this).getFooInt() == null 这行不通,你不能比较 intnull
  • 老实说,我对此一无所知,因为我不是这个/超级新手的专家。但这是从 Foo.extendedClassMethod 访问名为它的类的最佳方法?我的问题是,因为现在我必须通过我的助手类,我想使用的类(例如,想象我必须做 Foo.GetFooInt(Foo.class)。我想要这个 Foo.class 而不将它作为变量

标签: java android class this extends


【解决方案1】:

我在这里看到2个问题,理解this关键字和extending

this 关键字的问题

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

extend 的问题

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

//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

哦,好的,这很有用。还有一个问题,正如您所说,一种方法是使用抽象,但是有没有办法在不一直实现的情况下做同样的事情?仅供参考,我的目标是使用 Foo.FooHelperMethod() 并能够在“FooHelperMethod()”中访问 Foo 类。我希望我解释了它,我不知道该怎么做..如果不可能,我会按照你的建议使用抽象:)

当然,这是继承,只要不抽象父类,实现方法和属性,所有的子类通过继承父类都会有这个方法和属性。

让我们看看这个例子:

//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:

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

回答:

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

示例:

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 开头。
  • 您必须将这两个类放在单独的文件中才能允许static public class

【讨论】:

  • 哦,好吧,这很有用。还有一个问题,正如您所说,一种方法是使用抽象,但是有没有办法在不一直实现的情况下做同样的事情?仅供参考,我的目标是使用 Foo.FooHelperMethod() 并能够在“FooHelperMethod()”中访问 Foo 类。我希望我解释了它,我不知道该怎么做..如果不可能,我会按照你的建议使用抽象 :)
  • @PierGiorgioMisley 检查我的更新,如有必要,请随时询问更多信息
  • 抱歉,我的英语不太好,我在解释我的目标时遇到了一些问题。你真的很有帮助谢谢。我尝试编辑我的答案以获得更好的理解。也许现在更好,感谢您的帮助,如果我不清楚,请告诉我:)
  • uhmmmm.... 查看您的编辑,我认为您误解了 java 的工作原理,请检查使用this关键字的链接我在答案的第一部分链接
  • 好的,没通过我不能使用它。非常感谢您的耐心和真正的帮助:)
【解决方案2】:

我不确定我是否完全理解。但看起来好像您希望 GetFooInt 根据扩展它的类执行不同的操作。所以我觉得这里最好查看instanceof

public class FooHelper{
    public void GetFooInt(){
        if(this instanceof Foo)
        {
            ((Foo) this).fooInt = 5;
        }
    }
}

【讨论】:

    【解决方案3】:

    根据您想将一个类命名为“Helper”的情况,我假设您会将它用作helper-class

    public class Helper {
      public static int screenHeight = 500;
    }
    
    public class AnyOtherClass {
      testSomething() {
      System.out.println(Helper.screenHeight);
      Helper.screenHeight = 510;
      System.out.println(Helper.screenHeight);
     }
    }
    

    【讨论】:

    • 我可能选择了一个坏名字,我想将它与 extends 关键字一起使用,而不仅仅是作为外部类
    【解决方案4】:

    对于一些基本的理解:this 是您在非静态上下文中用于访问您当前所在对象的变量和方法的关键字。正确使用this例子:

    public class SomeClass {
        private int someInt;
    
        public void setSomeInt(int someInt) {
            this.someInt = someInt;
        }
    }
    

    在此示例中,this 是必需的,因为局部变量(/参数)someInt 与全局类变量 someInt 具有相同的名称。使用this,您可以访问您“所在”的对象的类变量。


    不必要的使用示例:

    public class SomeClass {
        private int someInt;
    
        public int squareSomeInt() {
            return this.someInt * this.someInt;
        }
    }
    

    这里不需要关键字this,因为没有名为someInt 的局部变量。


    另一方面,super 是一个关键字,它访问父类的变量和方法(该类,您的类派生自该类)。示例:

    public class SomeClass {
        private int someInt;
        public int squareSomeInt() {
            return someInt * someInt;
        }
    }
    

    派生类:

    public class Other extends SomeClass {
        public int squarePlusSquare() {
            return super.squareSomeInt() + super.squareSomeInt();
        }
    }
    

    【讨论】:

    • 感谢您提供这些信息,我很想念他们。但我仍然不知道如何实现我所需要的:我必须使用与“super”关键字完全相反的内容。我的目标是通过使用 new Other().squareSomeInt(); 调用 SomeClass 在“SomeClass”的方法中使用“Other”类;
    • SomeClass 对 Other 类一无所知。 Other 派生自 SomeClass... 除非您在 SomeClass 中有 Type other 的变量,否则您不能调用这些方法!
    • 尝试检查我的更新,也许我错过了其他类中的 键?
    • 现在是泛型......这与继承无关......我很抱歉,但我真的不明白你现在要做什么!跨度>
    猜你喜欢
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    相关资源
    最近更新 更多