[英]android accessing object inside extended class
我在使用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
}
}
现在,在我的主要活动中,我想这样做:
Foo foo = new Foo();
foo.MethodOne();
我的疑问是如何访问在MethodOne
内部main中创建的foo
对象。
我必须更改我的FooToExtend
public class<Foo> FooToExtend{
...
}
但我仍然不知道如何访问其中的foo对象。
我在这里看到2个问题,了解this
关键字并extending
this
关键字存在的问题
想象一下,你有一个类,你正在执行一些代码:关键字this
指的是类本身,如果该对象this
将是等同于me
。 在此处和此处查看更长的说明,示例和教程。
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
}
哦,好的,这很有用。 还有一个问题,一种方法就是使用摘要,就像您所说的那样,但是是否有一种方法可以始终执行而不执行呢? 仅出于信息目的,我的目标是使用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);
}
}
我的疑问是如何访问在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);
}
笔记
LOWECASE
,类名以UPPERCASE
开头。 static public class
我不确定我是否完全理解。 但是,似乎您希望GetFooInt
根据扩展它的类来执行不同的操作。 所以我认为最好在这里检查instanceof
。
public class FooHelper{
public void GetFooInt(){
if(this instanceof Foo)
{
((Foo) this).fooInt = 5;
}
}
}
根据您要命名一个类为“ Helper ”的情况,我想您将把它用作Helper类 。
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);
}
}
对于一些基本的了解: this
是您在非静态上下文中使用的关键字,用于访问您当前位于其中的Object的变量和方法。 正确使用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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.