是否有来自不同类的引用的动态对象类型?

发布于 2024-12-11 05:18:33 字数 1139 浏览 0 评论 0原文

好的,我有两个课程,第一个课程名为 Class1,第二个课程名为 Class2。 所以我想做的是将 Class1 的引用发送到 Class2 中。为此,我需要以下类型的代码:

Class1:

public class Class1 extends Activity {
    /** Called when the activity is first created. */
    public int Var1 = 5;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new Class2(this));
    }
}

public class Class2 extends SurfaceView {
    Class1 Obj1;               //Here!
    public Class2(Class1 Obj){ //And here I don't want to
                               //Use Class1 as a type.
                               //Is there any Global type?
        this.Obj1 = Obj;
    }
    public void EditVar1(){
        Obj.Var1 = 10;
    }
}

要获取 Class1 的引用:我应该使用对象的类型作为 Class1。我想在多个项目中使用这些代码:所以我应该为每个项目编辑 Obj 的类型“Class1”,只要它们的主类名称不同。这里有可以使用的全局类型吗?我还尝试使用 Context 作为对象类型,但我无法到达 Var1。反正我也不知道是什么。如果有人能解释它是什么,我将不胜感激。感谢您的任何回复!

Ok I have two classes the first one is named Class1 and the second one is Class2.
So the thing I want to do is sending a reference of Class1 into the Class2. To do this I need these kind of codes:

Class1:

public class Class1 extends Activity {
    /** Called when the activity is first created. */
    public int Var1 = 5;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new Class2(this));
    }
}

public class Class2 extends SurfaceView {
    Class1 Obj1;               //Here!
    public Class2(Class1 Obj){ //And here I don't want to
                               //Use Class1 as a type.
                               //Is there any Global type?
        this.Obj1 = Obj;
    }
    public void EditVar1(){
        Obj.Var1 = 10;
    }
}

To take a reference of Class1: I should use Object's type as Class1. I want to use these codes in more than one projects: So I should edit the Obj's type "Class1" for each project as long as their main class name is different. Is there any global type to use here? Also I tried to use Context as object type with this I can't reach Var1. Anyway I don't know what is it too. If someone can explain what is it I'll be appreciative. Thank you for any responds!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-12-18 05:18:33

在这种情况下,它们最近的祖先是 java.lang.Object,就像所有 Java 类一样。
对于 Activity,它的父级是上下文,正如您尝试使用的那样。如果您想访问 Activity 具有的父 Activity 没有的特定属性和方法,则必须强制转换该对象。例如:

Class1 obj1;
public Class2(Activity obj) {
    this.obj1 = (Class1) obj;
}

如果保留对超类而不是派生类的引用,并将派生对象分配给超类对象,则会丢失派生类的特定属性。这称为对象切片

Their nearest ancestor in this case is java.lang.Object, like all Java classes.
For Activity, its parent is context, as you tried using. If you then want to access the specific properties and methods an Activity has, that its parent doesn't have, you have to cast the object. E.g.:

Class1 obj1;
public Class2(Activity obj) {
    this.obj1 = (Class1) obj;
}

If you keep a reference to the superclass instead of the derived class and you assign the derived object to the superclass object, you lose the specific properties of the derived. This is known as object slicing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文